Inheritance is a process where sub class have same properties then its super class. In this process sub class acquire all the characteristics of its super class.
By using inheritance we can manage information in hierarchical manner.
The class which take properties of other class is called subclass and the one whose properties has been taken is called super class.
By using extends keyword properties of a class get inherit .
Syntax
class car {
.....
.....
}
class maruti extends car {
.....
.....
}
eg:-
class BaseClass
{
public void show()
{
System.out.println("HELLO");
}
}
public class SubClass extends BaseClass {
public void print()
{
System.out.println("HI");
}
public static void main(String[] args)
{
SubClass obj = new SubClass();
obj.print(); //method of Child class
obj.show(); //method of Parent class
}
}
OUTPUT
HI
HELLO
0 Comment(s)