Inheritance
The
mechanism of deriving a new class from an old one is called as INHERITANCE.
The existing classes are called as base/parent classes where as the new
classes are called as child/derived classes. The derived class inherits
all the capabilities of the base class and can add refinements and extensions
of its own.
Defining
derived classes
A
derived class can be defined by specifying its relationship with the base class
in addition to its own details. The general form of defining a derived class
is:
class subclassname extends superclassname
{
variable
declaration;
method
declaration;
}
Java
supports the following types of Inheritance
a)
Single
b)
Multilevel
c)
Hierarchical
Single
inheritance:
Derivation of a class from only one base class is called single inheritance.
class Dept
{
int dno;
String dname;
String loc;
void
setDetails(int x,String y,String z)
{
dno=x;
dname=y;
loc=z;
}
}
class emp extends Dept
{
int eno;
String ename;
String job;
void setemp(int
x,String y,String z)
{
eno=x;
ename=y;
job=z;
}
void
showdetails()
{
System.out.println("Dept
Details="+dno+"\t\t"+dname+"\t\t"+loc);
System.out.println("Emp
Details="+eno+"\t\t"+ename+"\t\t"+job);
}
}
class single
{
public static
void main(String []args)
{
emp
e=new emp();
e.setDetails(10,"sales","vizag");
e.setemp(101,"Smith","manager");
e.showdetails();
}
}
Output:
C:\>javac single.java
C:\>java single
Dept Details=10
sales vizag
Emp Details=101
Smith manager
Hierarchical
inheritance:
Derivation of several classes from a single base class i.e. the traits of one
class may be inherited by more than one class is called hierarchical
inheritance. We can achieve runtime polymorphism with the help of method
overriding.
Example:
class common
{
void assign() { }
void show( ) { }
}
class prodinfo extends common
{
int pno;
String pname;
public void
assign()
{
pno=100;
pname="Lux";
}
public void
show()
{
System.out.println("Product
Details");
System.out.println(pno+"\t\t"+pname);
}
}
class custinfo extends common
{
int cno;
String cname;
public void
assign()
{
cno=10;
cname="Gayatri";
}
public void
show()
{
System.out.println("Customer
Details");
System.out.println(cno+"\t\t"+cname);
}
}
class hierar
{
public static
void main(String []args)
{
common
c; //reference variable
if(args[0].equals("product"))
c=new
prodinfo();
else
c=new
custinfo();
c.assign();
c.show();
}
}
Output:
C:\>javac hierar.java
C:\>java hierar opo
Customer Details
10 Gayatri
C:\>java hierar product
Product Details
100 Lux
Multilevel
inheritance:
Derivation of a class from another derived class is called multilevel
inheritance.
Example:
class student
{
int rno;
void get_no(int a)
{
rno=a;
}
void put_no()
{
System.out.println("Rno="+rno);
}
}
class test extends student
{
float sub1,sub2;
void get_marks(float sub1, float sub2)
{
this.sub1=sub1;
this.sub2=sub2;
}
void put_marks()
{
System.out.println("Sub1="+sub1);
System.out.println("Sub2="+sub2);
}
}
class result extends test
{
float total;
void display()
{
total=sub1+sub2;
System.out.println("Total="+total);
}
}
class multi
{
public static
void main(String []args)
{
result
r=new result();
r.get_no(9);
r.get_marks(56.7f,78.9f);
r.put_no();
r.put_marks();
r.display();
}
}
Output:
C:\>javac multi.java
C:\>java multi
Rno=9
Sub1=56.7
Sub2=78.9
Total=135.6