Interview Questions, Answers and Tutorials

10 Class and Methods

10 Class and Methods

10.1 | Class

A class is a template from which objects are created. A class declaration typically contains a set of attributes (instance variables) and functions (methods).
 
Class
 


10.2 | Methods

In Java, functions, and procedures are called methods. Methods can include zero or more input parameters and zero or one return parameter. The following code shows some method declarations. An arithmetic class has two methods printSum and getSum. method printSum has two input parameters both of integer type and no return parameter. while method getSum has two integer type input parameters and one integer type return parameter.

 
 
 
 

10.2.1 | Method Overloading

In Java, method overloading occurs when two or more methods in the same class have the same name but different parameters. Two methods can be considered overloaded if any of the below conditions are true:

  • The number of parameters is different for the methods.
  • The parameter types (input or return) are different.
Class ArithmeticClass below has two methods with the same name getSum. these two methods are overloaded as the number of input parameters is different of both.
 
Arithmetic Class
 
 
 
 
10.2.2 | Method Overriding
 
Overriding a method involves defining a method in a subclass that has the same signature (input and return parameter) as a method in a superclass. Then, when that method is called, the method in the subclass is found and executed instead of the one in the superclass. The following code overriding method print. The code below shows the classes SuperClass and SubClass. SubClass overrides the print method of its superclass. MainClass shows different ways of calling the overridden method of a subclass as well as the original method of the superclass.
 
Super Class
 
 
 
10.2.3 | Variable and Method Scope
 
Java has reserved keywords to define the scope of variables methods and classes. 
 
private keyword is used to declare variables and methods that are to be accessible only within the class. 
 
protected keyword is used to declare variables and methods that are to be accessible only within the class or any class that extends to this class.
 
public keyword is used to declare variables and methods that are to be accessible within the class and as well as outside of class.

9 thoughts on “10 Class and Methods

  1. AWS Training in Bangalore – Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.

    IOT Training in Bangalore – Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

  2. Hadoop concepts, Applying modelling through R programming using Machine learning algorithms and illustrate impeccable Data Visualization by leveraging on 'R' capabilities.With companies across industries striving to bring their research and analysis (R&A) departments up to speed, the demand for qualified data scientists is rising.
    data science training in bangalore

    Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it h
    as fewer syntactical constructions than other languages.python interview questions and answers

  3. I really enjoyed this post. I appreciate your work on this gives in-depth information. I am really happy with the quality of learning tips for Beginners in this article. Thanks for sharing.

Comments are closed.