- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Referencing Subclass objects with Subclass vs Superclass reference
In java inheritance some of the basic rules includes −
Object relation of Superclass (parent) to Subclass (child) exists while child to parent object relation never exists.This means that reference of parent class could hold the child object while child reference could not hold the parent object.
In case of overriding of non static method the runtime object would evaluate that which method would be executed of subclass or of superclass.While execution of static method depends on the type of reference that object holds.
Other basic rule of inheritance is related to static and non static method overriding that static method in java could not be overridden while non static method can be.However subclass can define static method of same static method signature as superclass have hut that would not be consider as overriding while known as hiding of static method of superclass.
On the basis of above rules we would observe different scenarios with the help of program.
Example
class Parent { public void parentPrint() { System.out.println("parent print called"); } public static void staticMethod() { System.out.println("parent static method called"); } } public class SubclassReference extends Parent { public void parentPrint() { System.out.println("child print called"); } public static void staticMethod() { System.out.println("child static method called"); } public static void main(String[] args) { //SubclassReference invalid = new Parent();//Type mismatch: cannot convert from Parent to SubclassReference Parent obj = new SubclassReference(); obj.parentPrint(); //method of subclass would execute as subclass object at runtime. obj.staticMethod(); //method of superclass would execute as reference of superclass. Parent obj1 = new Parent(); obj1.parentPrint(); //method of superclass would execute as superclass object at runtime. obj1.staticMethod(); //method of superclass would execute as reference of superclass. SubclassReference obj3 = new SubclassReference(); obj3.parentPrint(); //method of subclass would execute as subclass object at runtime. obj3.staticMethod(); //method of subclass would execute as reference of subclass. } }
Output
child print called parent static method called parent print called parent static method called child print called child static method called
So difference between referencing using superclass reference and referencing using subclass reference is use superclass referencing can holds object of subclass and could only access the methods which are defined/overridden by subclass while use subclass referencing can not hold object of superclass and could access the methods of both superclass and subclass.
- Related Articles
- What happens when a subclass object is assigned to a superclass object in Java?
- Why subclass doesn't inherit the private instance variables of superclass in Java?
- Can we call Superclass’s static method from subclass in Java?
- How do I make a subclass from a super class in Python?
- Variable in subclass hides the variable in the super class in Java
- What happens if the subclass does not override abstract methods in java?
- While overriding can the subclass choose not to throw an exception in java?
- Determine if a class is a subclass of a second class in Python
- How can a Python subclass control what data is stored in an immutable instance?
- Determine if the type in the first argument is a subclass of second in Python
- How do we check if a class is a subclass of the given super class in Python?
- OSI vs. TCP/IP Reference Model
- Value parameters vs Reference parameters vs Output Parameters in C#
- Value Type vs Reference Type in C#
- Pass by reference vs value in Python
