This is always nice if we could reuse something that already exist rather than creating the same all over again. This is done by creating a new classes , reusing the properties of existing ones. This mechanism of deriving a new class from an old one is called inheritance . The old class is known as base class or parent class or super class and new class is known as derived class or child class.
Inheritance facilitates a class to acquire the properties and functionality of another class . the new class depicts the acquired properties and behavior of the existing class as well as its own unique properties and behavior.
The syntax to define subclass
Class sub_class extends super_class
{
//variables and methods declaration
}
Types of inheritance :-
1) Single inheritance
class Class1
{
//variables and methods declaration
}
class Class2 extends Class1
{
//variables and methods declaration
}
2) Hierarchical inheritance
{
//variables and methods declaration
}
class Class2 extends Class1
{
//variables and methods declaration
}
class Class3 extends Class1
{
//variables and methods declaration
}
3) Multilevel Inheritance
{
//variables and methods declaration
}
class Class2 extends Class1
{
//variables and methods declaration
}
class Class3 extends Class2
{
//variables and methods declaration
}
4) Multiple Inheritance
{
//variables and methods declaration
}
class Class2
{
//variables and methods declaration
}
class Class3 extends Class2 extends Class1
{
//variables and methods declaration
}
No comments:
Post a Comment