Wednesday 29 February 2012

What is inheritance


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
inheritance in java, single inheritance







class Class1
{
//variables and methods declaration
}
class Class2 extends Class1
{
//variables and methods declaration
}
2)      Hierarchical  inheritance

inheritance in java







class Class1
{
//variables and methods declaration
}
class Class2 extends Class1
{
//variables and methods declaration
}
class Class3 extends Class1
{
 //variables and methods declaration
}
3)      Multilevel Inheritance

inheritance in java







class Class1
{
//variables and methods declaration
}
class Class2 extends Class1
{
//variables and methods declaration
}
class Class3 extends Class2
{
 //variables and methods declaration
}
4)      Multiple Inheritance

inheritance in java

3





class Class1
{
//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

Related Posts Plugin for WordPress, Blogger...