Inheritance
Inheritance
Deriving a new class from existing class is class Inheritance. The main use of Inheritance is reusability.
Syntax:
Class newclass-name extends existing class
{
Datatype variable1,variable2,??
returntype method()
{
----------
----------
}
}
Example :
class CD
{
String title="inheritance";
String author="javayom";
void display()
{
System.out.println ("title is:"+ title);
System.out.println ("author is:"+author);
}
}
class VCD extends CD
{
String newfeature="JAVAYOM BLOG";
void show()
{
System.out.println ("new feature is: "+newfeature);
}
}
class Demo
{
public static void main(String as[])
{
VCD c = new VCD ();
c.show ();
c.display ();
}
}
Output :
new feature is: JAVAYOM BLOG
title is: inheritance
author is: javayom
Explanation :
» When you extend any class all the superclass members becomes members of
subclass.
» With subclass object we can call subclass members as well as superclass
» With subclass object we can call subclass members as well as superclass
members also.
» With superclass object we can call only superclass members. We can't call
» With superclass object we can call only superclass members. We can't call
subclass members.
Types of Inheritance :
Types of Inheritance :
1. Simple Inheritance
2. Multilevel Inheritance
3. Hierarchal Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
1.Simple Inheritance :
In this process we can have only one super class and one subclass.
2.Multilevel Inheritance :
The process of deriving of new class from already derived class is called multilevel inheritance.In this ,A is a superclass and two subclasses B,C. C is a subclass and has superclass called A and B.
3. Hierarchal Inheritance :
In this process we have onesuperclass and many subclasses. In this one superclass and many subclasses. In the following diagram, A is superclass and B, C are subclasses.
4.Multiple Inheritance : (not allowed in java)
In this process we have one subclass and many super classes.
Note:
» Java doesn't support multiple Inheritance using classes.
» In this one subclass can have many direct super classes.
5.Hybrid Inheritance : (not allowed in java)
» Java doesn't support multiple Inheritance using classes.
» In this one subclass can have many direct super classes.
5.Hybrid Inheritance : (not allowed in java)
It is combination of multilevel and multiple and herarical inheritance.
Note:
» Java doesn't support hybrid Inheritance using classes.
» Java doesn't support hybrid Inheritance using classes.
No comments:
Post a Comment