by Dinesh Thakur Category: Inheritance

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 Category: Inheritance

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 Category: Inheritance

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 Category: Inheritance

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 Category: Inheritance

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 Category: Inheritance

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 Category: Inheritance

In a class hierarchy, A subclass can contain a method with the same signature and return type as in its superclass, then the method in the subclass is said to override the method in the 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 Category: Inheritance

The super() keyword is used to reference objects for the immediate parent class. 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 Category: Inheritance

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 Category: Inheritance

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 Category: Inheritance

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 Category: Inheritance

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 Category: Inheritance

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 Category: Inheritance

An abstract class is one whose header contains the reserved keyword, abstract. An abstract class is distinguishable from other classes by the fact that it is not possible to use the new operator to construct objects from them directly. Each abstract class may have at least zero abstract methods.

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.

 
by Dinesh Thakur Category: Inheritance

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 Category: Inheritance

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 Category: Inheritance

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 Category: Inheritance

 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 Category: Inheritance

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.

 

Page 2 of 2



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles