Java-Superclass Variable Can Reference a Subclass Object

Superclass Variable Can Reference a Subclass Object A reference variable of a superclass can be assigned a reference to any subclass derived from that superclass. In the following program, obj is a reference to NewData object, Since NewData is a subclass of Data, it is permissible to assign obj a reference to the NewData object. When … Read more…

Java-Multilevel Inheritance

Multilevel Inheritance Java supports multilevel hierarchy, it means that, you can build hierarchies that contain as many layers of inheritance as you want. For example, given three class called DataV1,  DataV2 and  DataV3. The class DataV1 serves as a base class for the derived class DataV2 as which in turn serves as a base class for the … Read more…

Java-Method Overloading

Method Overloading Method overriding occurs only when the names and the types signatures of the two methods are identical. If they are not, then the two methods are simply overloaded. Program Program Source class Data { int data1; int data2; void dataValues() { System.out.println(“data1 = “+data1); System.out.println(“data2 = “+data2); } } class NewData extends Data … Read more…

Java-Runtime Polymorphism or Dynamic Method Dispatch

Runtime Polymorphism or Dynamic Method Dispatch Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time. A superclass reference variable can refer to a subclass object. Java uses this fact to resolve calls to overridden methods at run time. Here is how. When an overridden method … Read more…

Java-Abstract Classes

Abstract Classes A class for which cannot create objects is called an abstract class. An abstract class is to be extended and then instantiated. Some of the methods in an abstract class may be declared as abstract methods. Such an abstract method consists of a declaration without any body, as shown below: All abstract methods … Read more…

Java-Final to Prevent Overriding

Final to Prevent Overriding All methods and variables can be overridden by default in subclasses. If we wish to prevent the subclasses from overriding the members of the superclass, we can declare them as final using the keyword final as a modifier. If any attempt to override a final method in a subclass results in … Read more…