by Dinesh Thakur

The process of defining methods with same name but with different functionalities is termed method overloading. For example, an overloaded draw() method can be used to draw anything, from a circle to an image. Methods with same name, namely, draw, but with different arguments can be used for all cases.

 
by Dinesh Thakur

When in a class, we have more then one method with similar name but with different type signatures i.e. with different number of parameters or with different types of parameters, then we say that the method is overloaded. Compiler will recognize which method to execute on the basis of the type and number of parameters used while calling the method.

 
by Dinesh Thakur

In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it certainly refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden.

 
by Dinesh Thakur

Inheritance is one the most powerful concepts in an object-oriented language. Through inheritance   the code developed for one class can be used in another class. That is, the data members made in a class can be used in another class. Inheritance is done by creating new classes that are extensions of other classes. The new class is known as a subclass. The original class is known as a superclass. The subclass has all the attributes of the superclass, and in addition has attributes that it defines itself. A class can have only one superclass. This is known as single inheritance. A superclass can have multiple subclasses.

 
by Dinesh Thakur

Variable-Length Arguments Method in short varargs methods, Such methods usefulness comes into picture when you have a method that can accept a variety of arbitrary data types.



 
by Dinesh Thakur

A static nested class or a top-level class is a class that is declared inside another class with a static modifier. Like static methods, a static nested class can only refer directly to static members of the enclosing classes, even if those members are private.

 
by Dinesh Thakur

Anonymous inner classes are the local inner classes that are declared without a name. All of the code for the anonymous class is coded within the method where we need to create an instance of the anonymous class. Since anonymous inner classes do not have a name so you cannot use the new keyword in the usual way to create an instance of the class. In fact, anonymous inner classes are declared and instantiated at the same time. 

 
by Dinesh Thakur

A local inner class or simply a local class is an inner class that is declared inside a method or constructor or initialization block of the enclosing class. Like regular inner classes, the local classes are associated with a containing instance and can access any member, including the private members of the enclosing class. In addition, it can also access local variables in the method and parameters passed to the method only if the variables are declared final. Just like local variables, the inner class cannot be accessed outside the code block. It is useful when a computation in a method requires the use of specialized class that is not used elsewhere.

 
by Dinesh Thakur

A regular inner class is a nested class that only exists within an instance of enclosing class. In other words, you cannot instantiate an object of an inner class without first having an object of the outer class. An inner class can use all the methods and fields of the outer class even the private ones directly. The methods and fields of outer class are used as if they were part of the inner class.

 
by Dinesh Thakur

Unlike other OOP languages like C++ that include the concept of multiple inheritance (where class directly inherits two or more classes), Java only supports single inheritance. The use of multiple inheritances makes the programming language for more complex to use, learn and implement. The designers of Java wanted to make the language simpler and free from any complication and ambiguity. So the Java designers decided they could do without it. But while working with programs in Java, we often come across situations where we want multiple inheritances.



 
by Dinesh Thakur

When one interface inherits from another interface, that sub-interface inherits all the methods and constants that its super interface declared. In addition, it can also declare new abstract methods and constants. To extend an interface, you use the extends keyword just as you do in the class definition. Unlike a subclass which can directly extend only one subclass, an interface can directly extend multiple interfaces. This can be done using the following syntax

[public] interface InterfaceName

 
by Dinesh Thakur

A class can implement more than one interface. For this, you write the names of all interfaces that the class implement separated by commas following the implements keyword and the class body must provide implementations for all of the methods specified in the interfaces. The syntax for implementing multiple interfaces is

 
by Dinesh Thakur

An interface is just defined like a class but a keyword interface is used instead of the keyword class. It can contain either constants (final fields) or abstract method declarations or both.

All the methods in an interface are public and abstract by default and all the fields are public, static and final by default. Its syntax is .

 
by Dinesh Thakur

Sometimes you may want to prevent a subclass from overriding a method in your class. To do this, simply add the keyword final at the start of the method declaration in a superclass. Any attempt to override a final method will result in a compiler error.

 
by Dinesh Thakur

An abstract method is a method prototype (i.e. return type, method name, list of parameters and optionally throws clause) without any implementation. Its implementation is provided by the subclass(es) of the class in which it is declared. To create an abstract method, simply specify the modifier abstract followed by the method declaration and replace the method body by a semicolon. For example, The abstract method area () of the Shape superclass will be written as

 
by Dinesh Thakur

Sometimes it is useful to know the type of the object at runtime. For example, if you try to make a cast that is invalid, an exception will be thrown. You can overcome this problem if you are able to verify that the object is of the type you expect before you make the cast. This is possible using the instanceof operator. The general from of instanceof operator is objRef instanceof type

 
by Dinesh Thakur

You can cast an object to another class type provided a class is a subclass of other i.e. casting takes place within an inheritance hierarchy, so that the source and destination are within the same hierarchy.

 
by Dinesh Thakur

As we know that in order to declare a variable that references an object, we use the following

syntax.

ClassName variableName;

Here, variableName is the name of the reference variable and ClassName is the name of its class. Thus, variablename can reference any object of class ClassName. However, it can also reference any object whose class is a subclass of ClassName. For example: If a class A is a superclass of class B and class B is a superclass of class C then in that case, variable of class A can reference any object derived from that class (i.e. object of class B and class c). This is possible because each subclass object is an object of its superclass but not vice versa.

 
by Dinesh Thakur

All the classes in Java that you have defined so far are subclasses by default whether or not you have specified the superclass. The class which all classes in Java are descendent of (directly or indirectly) is java .lang. Object class. So each class inherits the instance methods of Object class. It is important to be familiar with the methods provided by the Object class so that you can use them in your class.



 
by Dinesh Thakur

Inheritance is suitable only when classes are in a relationship in which subclass is a (kind of) superclass. For example: A Car is a Vehicle so the class Car has all the features of class Vehicle in addition to the features of its own class. However, we cannot always have. is a relationship between objects of different classes. For example: A car is not a kind of engine. To represent such a relationship, we have an alternative to inheritance known as composition. It is applied when classes are in a relationship in which subclass has a (part of) superclass.

 
by Dinesh Thakur

In Our Example illustrates Multilevel Inheritance, Here Class B is derived from superclass A which itself acts as a superclass for the subclass C. The class C inherits the members of Class B directly as it is explicitly derived from it, whereas the members of class A are inherited indirectly into class c (via class B). So the class B acts as a direct superclass and A acts as a indirect superclass for class C.

 
by Dinesh Thakur

A subclass inherits methods from a superclass. However in certain situations, the subclass need to modify the implementation (code) of a method defined in the superclass without changing the parameter list. This is achieved by overriding or redefining the method in the subclass.

 
by Dinesh Thakur

A subclass inherits the accessible data fields and methods from its superclass, but the constructors of the superclass are not inherited in the subclass. They can only be invoked from constructors of the subclass( es) using the keyword super.

 
by Dinesh Thakur

A protected field or method in a public class can be accessed directly by all classes within the same package and its subclasses even if the subclasses are in different packages. It is more restrictive than default (or package) access.

 
by Dinesh Thakur

Inheritance is a mechanism of creating a new class from an existing class by inheriting the features of existing class and adding additional features of its own. When a class is derived from an existing class, all the members of the superclass are automatically inherited in the subclass. However, it is also possible to restrict access to fields and method of the superclass in the subclass. This is possible by applying the access Specifiers to the member of the superclass. If you do not want a subclass to access a superclass member, give that member private access. The private members of the superclass remain private (accessible within the superclass only) in the superclass and hence are not accessible directly to the members of the subclass. However, the subclass can access them indirectly through the inherited accessible methods of the superclass.

 
by Dinesh Thakur

Inheriting a superclass gives the ability to a subclass to define only those aspects that differ from or extend the functionality of the superclass. The syntax for creating a subclass is simple. At the beginning of your class definition, use the extends keyword after the class name followed by the name class being extended (i.e. superclass). A subclass can only inherit directly from one superclass. Unlike C++, Java does not support multiple inheritances.



 
by Dinesh Thakur

An interface is a way of describing what classes should do, without specifying how they should do it. A class can implement more than one interface. In Java, an interface is not a class but a set of requirements for the class that we want to conform to the interface. All the methods of an interface are by default public. So, it is not required to use the keyword public when declaring a method in an interface. Interfaces can also have more than one method. Interfaces can also define constants but do not implement methods. An interface is defined like a class. Its general form is:



 
by Dinesh Thakur

The final keyword has the following uses in the inheritance.

        If you want the method must not be overridden then defined that method as final.

 
by Dinesh Thakur

Some time there is a situation in which you feel the need of a superclass that has only declaration of few or all methods, with definition of few or none methods. But it is necessary that it must not be a completely defined or implemented class. The methods are declared only and specified by abstract type modifier called abstract method. It is the responsibility of the concrete subclass to implement all abstract method of superclass. To declare any method as abstract use the following form: abstract accessmodifer retumtype methodName( <parameterlist>);

 
by Dinesh Thakur

Another important feature of the Java is polymorphism. A famous phrase 'one interface, multiple methods' or 'once instance, multiple forms' is for polymorphism. General classes of actions are defined, and according to the exact nature of situation the specific action is called. A set of related activities are designed in a generic interface, and same interface is used to specify a general class of action. To select the specific action is the responsibility of the compiler's job. Polymorphism allows you to create a sensible, readable, clean, and resilient code.



 
by Dinesh Thakur

Java supports multilevel inheritance. In multiple, multilevel class hierarchies contain the layers of inheritance. But at each layer, a class is a subclass of the superc1ass of another, except the last layer. One pictorial representation of such concept is given below.



 
by Dinesh Thakur

The class member attributes (fields) and methods are bounded with some accessibility modifier, which defines the access scope of the member. In this section, we'll study how to access the data members (attributes) of the class.

 
by Dinesh Thakur

 For the reusability of existing classes, inheritance came into use, and java supports only two types of inheritance, in which a class extends another class.



 
by Dinesh Thakur

Inheritance is one of the most dominant and vital feature of the object oriented programming because it supports the hierarchical classifications, reusability of class; and defined to specialization. Java is a language that supports inheritance but with some additional advantages and features.