Prerequisite : Inheritance
In Java, all non-static methods are based on the runtime type of the underlying object rather than the type of the reference that points to that object. Therefore, it doesn’t matter which type you use in the declaration of the object, the behavior will be the same.
How to Refer a subclass object
There are two approaches to refer a subclass object. Both have some advantages/disadvantages over the other. The declaration affect is seen on methods that are visible at compile-time.
- First approach (Referencing using Superclass reference): A reference variable of a superclass can be used to a refer any subclass object derived from that superclass. If the methods are present in SuperClass, but overridden by SubClass, it will be the overridden method that will be executed.
- Second approach (Referencing using subclass reference) : A subclass reference can be used to refer its object.
Consider an example explaining both the approaches.
seat height of first bicycle is 25 No of gears are 3 speed of bicycle is 100 seat height is 25 No of gears are 4 speed of bicycle is 200 seat height is 20
Explanation of above program :
- The object of MountainBike class is created which is referred by using subclass reference ‘mb1’. Using this reference we will have access both parts(methods and variables) of the object defined by the superclass or subclass. See below image for clear understanding.
MountainBike mb1 = new MountainBike(3, 100, 25);
Now we again create object of MountainBike class but this time it is referred by using superclass Bicycle reference ‘mb2’. Using this reference we will have access only to those parts(methods and variables) of the object defined by the superclass.
Bicycle mb2 = new MountainBike(4, 200, 20);
- Since the reference ‘mb1’ have access to field ‘seatHeight’, so we print this on console.
System.out.println("seat height of first bicycle is " + mb1.seatHeight);
If there are methods present in super class, but overridden by subclass, and if object of subclass is created, then whatever reference we use(either subclass or superclass), it will always be the overridden method in subclass that will be executed. So below two statements will call toString() method of MountainBike class.
Since the reference made by ‘mb2’ is of type Bicycle , so we will get compile time error in below statement.
System.out.println("seat height of second bicycle is " + mb2.seatHeight);
- Again the reference made by ‘mb2’ is of type Bicycle , so we will get compile time error in below statement.
Use of type casting
In above example, we have seen that by using reference ‘mb2’ of type Bicycle, we are unable to call subclass specific methods or access subclass fields. This problem can be solved using type casting in java. For example, we can declare another reference say ‘mb3’ of type MountainBike and assign it to ‘mb2’ using typecasting.
// declaring MountainBike reference MountainBike mb3; // assigning mb3 to mb2 using typecasting. mb3 = (MountainBike)mb2;
So, now the following statements are valid.
System.out.println("seat height of second bicycle is " + mb3.seatHeight); mb3.setHeight(21);
When to go for first approach (Referencing using superclass reference)
If we don’t know exact runtime type of an object, then we should use this approach. For example, consider an ArrayList containing different objects at different indices. Now when we try to get elements of arraylist using ArrayList.get(int index) method then we must use Object reference, as in this case, we don’t know exact runtime type of an object. For example :
Advantage : We can use superclass reference to hold any subclass object derived from it.
Disadvantage : By using superclass reference, we will have access only to those parts(methods and variables) of the object defined by the superclass. For example, we can not access seatHeight variable or call setHeight(int newValue) method using Bicycle reference in above first example. This is because they are defined in subclass not in the superclass.
When to go for second approach (Referencing using subclass reference)
If we know the exact runtime type of an object, then this approach is better. Using this approach, we can also call a particular object specific methods. For example :
GeeksForGeeks for java archives 13 java
Advantage : By using subclass reference, we will have access to both parts(methods and variables) of the object defined by the superclass or subclass. For example, we can call setHeight(int newValue) method or speedUp(int increment) method using MountainBike reference in above first example.
Disadvantage : We can use subclass reference to hold only for that particular subclass objects only.
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.