Professional Documents
Culture Documents
Inheritance-Super Class and Sub Class
Inheritance-Super Class and Sub Class
Inheritance can be defined as the process where one class acquires the properties
(methods and fields) of another. With the use of inheritance the information is made
manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child
class) and the class whose properties are inherited is known as superclass (base class,
parent class).
extends Keyword
extends is the keyword used to inherit the properties of a class. Following is the syntax of
extends keyword.
Inheritance
Syntax
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance (Not supported in Java)
4. Hierarchical Inheritance
5. Hybrid Inheritance
Inheritance
Inheritance
Inheritance
Example for Single Inheritance
class Teacher {
Teacher
String designation = "Teacher";
(Super Class)
String collegeName = "Beginnersbook";
void does(){
System.out.println("Teaching");
}
}
The base class is Teacher and it is inherited by class Physics Teacher. The methods and
variables of class Teacher can be accessed by object of class Physics Teacher, because we
applied inheritance. So create an object for sub class instead of super class.
Inheritance
Example for Multilevel Inheritance
The above program is example of multilevel inheritance in java. The class Teacher is
inherited by class Physics Teacher. In turn the class Physics Teacher is inherited by class
Chemistry Teacher. The inheritance contain more than one level, since it is called multilevel
inheritance.