- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Can super class reference variable hold sub class's object in java?
Yes, the super class reference variable can hold the sub class object actually, it is widening in case of objects (Conversion of lower datatype to a higher datatype).
But, using this reference you can access the members of super class only, if you try to access the sub class members a compile time error will be generated.
Example
In the following Java example, we have two classes namely Person and Student. The Person class has two instance variables name and age and one instance method displayPerson() which displays the name and age.
The Student extends the person class and in addition to the inherited name and age it has two more variables branch and student_id. It has a method displayData() which displays all four values.
In the main method, we are assigning the subclass object with the super class reference variable
class Person{ private String name; private int age; public Person(String name, int age){ this.name = name; this.age = age; } public void displayPerson() { System.out.println("Data of the Person class: "); System.out.println("Name: "+this.name); System.out.println("Age: "+this.age); } } public class Student extends Person { public String branch; public int Student_id; public Student(String name, int age, String branch, int Student_id){ super(name, age); this.branch = branch; this.Student_id = Student_id; } public void displayStudent() { System.out.println("Data of the Student class: "); System.out.println("Name: "+this.name); System.out.println("Age: "+this.age); System.out.println("Branch: "+this.branch); System.out.println("Student ID: "+this.Student_id); } public static void main(String[] args) { Person person = new Student("Krishna", 20, "IT", 1256); person.displayPerson(); } }
Output
Data of the Person class: Name: Krishna Age: 20
Accessing the sub class methods
When you assign the sub class object to the super class reference variable and, using this reference if you try to access the members of the sub class a compile time error will be generated.
Example
In this case, if you assign a Student object to reference variable of Person class as −
Person person = new Student("Krishna", 20, "IT", 1256);
Using this reference, you can access the method of the super class only i.e. displayPerson(). Instead if you try to access the sub class method i.e. displayStudent() a compile time error is generated.
Therefore, if you replace the main method of the previous program with the following a compile time error will be generated.
public static void main(String[] args) { Person person = new Student("Krishna", 20, "IT", 1256); person.displayStudent(); }
Compile time error
Student.java:33: error: cannot find symbol person.dispalyStudent(); ^ symbol: method dispalyStudent() location: variable person of type Person 1 error
- Related Articles
- How to convert a sub class variable into a super class type in Java?
- How to convert a super class variable into a sub class type in Java
- Variable in subclass hides the variable in the super class in Java
- Get super class of an object in Java
- Why Object class is the super class for all classes in Java?
- How static class Object is created without reference of outer class in java?
- What is the super class of every class in Java?
- How to ensure that child class overrides a super class method in java?
- Object class in Java
- Call a JavaScript class object with variable?
- Using predefined class name as Class or Variable name in Java
- Object class in java programming
- Object and class in Java
- Can we throw an object of generic class in java?
- How can we use this and super keywords in method reference in Java?
