- 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
How to convert a super class variable into a sub class type in Java
Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −
public class A extends B{
}
The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.
In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.
Converting a super class reference variable into a sub class type
You can try to convert the super class variable to the sub class type by simply using the cast operator. But, first of all you need to create the super class reference using the sub class object and then, convert this (super) reference type to sub class type using the cast operator.
Example
class Person{ public String name; public 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 Sample extends Person { public String branch; public int Student_id; public Sample(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: "+super.name); System.out.println("Age: "+super.age); System.out.println("Branch: "+this.branch); System.out.println("Student ID: "+this.Student_id); } public static void main(String[] args) { Person person = new Sample("Krishna", 20, "IT", 1256); //Converting super class variable to sub class type Sample obj = (Sample) person; obj.displayPerson(); obj.displayStudent(); } }
Output
Data of the Person class: Name: Krishna Age: 20 Data of the Student class: Name: Krishna Age: 20 Branch: IT Student ID: 1256
Example
class Super{ public Super(){ System.out.println("Constructor of the super class"); } public void superMethod() { System.out.println("Method of the super class "); } } public class Test extends Super { public Test(){ System.out.println("Constructor of the sub class"); } public void subMethod() { System.out.println("Method of the sub class "); } public static void main(String[] args) { Super sup = new Test(); //Converting super class variable to sub class type Test obj = (Test) sup; obj.superMethod(); obj.subMethod(); } }
Output
Constructor of the super class Constructor of the sub class Method of the super class Method of the sub class
- Related Articles
- How to convert a sub class variable into a super class type in Java?
- Can super class reference variable hold sub class's object in java?
- How to convert a class to another class type in C++?
- Variable in subclass hides the variable in the super class in Java
- How to ensure that child class overrides a super class method in java?
- Java Program to create String to super class type mapping
- How can we restrict Generics (type parameter) to sub classes of a particular class in Java?
- What is the super class of every class in Java?
- How to convert primitive data into wrapper class using Java?
- Why Object class is the super class for all classes in Java?
- Get super class of an object in Java
- How do we check if a class is a subclass of the given super class in Python?
- How to access Static variable of Outer class from Static Inner class in java?
- How do I make a subclass from a super class in Python?
- Using predefined class name as Class or Variable name in Java
