Inheritance?

https://www.computersprofessor.com/2017/04/inheritance.html
Inheritance:
|
Re usability is
an important aspect of object oriented programming java classes can be reused
in several ways. This is basically done by creating new classes, re using the
properties of existing ones. The mechanism of deriving a new class from an
old one is called inheritance. The old class is known as the base class or
super class or parent class and the new one is called the subclass or derived
class or child class.
The inheritance
allows subclasses to inherit all the variables and methods of their parent
classes. Inheritance may take different forms.
1. single
inheritance [only one super class]
Ex:
2. multiple
inheritance [several super classes]
Ex:
3.
Hierarichial inheritance:[one super class and many
sub]
Ex:
4. Multilevel
inheritance :[derive from a derived class]
Ex:
Java doesn’t
directly implement multiple inheritance however this concept is implemented
using a secondary inheritance card in the form of interfaces.
Defining a
subclass: A class is defined as follows
Syntax:
class subclassname extends superclassname
{
Variable
declaration;
Methods
declaration;
}
The keyword
extends signifies that the properties of super class are extended to the
super class name. The subclass will now contain its own variables and methods as well those
of super class. This kind of situation accursed whenever we want to add some
more properties to an existing class without actually modifying it.
Example:
class Room
{
int length;
int breadth;
room(int x,
int y)
{
length=x;
breadth=y;
}
int area( )
{
Return(length*
breadth);
}
}
class Bedroom
extends Room
{
int height;
Bedroom(int x,
int y, int z)
{
super(x, y)
height=z;
}
int volume( )
{
return(length
* breadth * )
}
}
class Inherit
{
public static
void main(String args[ ] )
{
Bedroom room1=new Bedroom(14, 12, 10);
int area1=room1. area( );
int volume1=room1.volume ();
System.out.println(“area
1=”+area1);
System.out.println(“volume=”+volume);
}
}
Subclass
constructor:
A subclass
constructor is used to construct the instance variables of both subclass and
superclass. The subclass constructor uses the keyword super to invoke the
constructor of superclass.
® The
call to superclass constructor must appears as first statement with in the
subclass constructor.
® The
parameters in the subclass must match the order and type of the instance variable declared in the super class.
® Superclass
default constructor is available to
subclass automatically.
®
First of all superclass constructor is executed then only subclass
constructor.
®
Superclass parameterized constructor is not available to subclass
automatically.
Single
inheritance:
In single
inheritance the desired class uses the properties of only one base class.
class Adm
{
int rno;
String name;
void getData(int x, string y)
{
rno=x;
name=y;
}
void putData(
)
{
System.out.println(“roll
no is”+rno);
System.out.println(“name
is”+name);
}
}
class Marks
extends Adm
{
float m1,
m2, m3;
getMarks(float
x, float y, float z)
{
m1=x;
m2=y;
m3=z;
}
putMarks ( )
{
System.out.println(“marks
obtained+”m1+” ”m2+” ”+m3);
}
}
class Admin
{
public static
void main(String args[ ] )
{
Marks m=new
Marks( );
m.getData(101,
“raju”);
m.getMarks(90,
99, 95);
m.putData( );
m.putMarks( );
}
}
The above
example admin base class and class marks is desired from it. Now m is an
object of desired class marks, so m contains the members of Marks and Adm.
Multilevel inheritance:
In this
inheritance a class is derived from another derived class. this concept
allows us to built chain of classes.
The class A
serves as a base class for a derived class B which in turn serves as a base
class for the derived class C. the chain ABC is known as inheritance path.
A derived
class with multilevel base classes is declared as follows.
class A
{
-------
-------
}
class B
extends A
{
----------
-----------
}
class C
extends B
{
--------
----------
}
This process
may be extended to any number of
levels. The class C can inherit the members of both A and B.
In the
previous example the class marks is
derived from Adm.
In the
following example the desired class grade is desired from class marks(another
desired class)
Ex:
class Adm
{
int rno;
string name;
void getData(int x, string y)
{
rno=x;
name=y;
}
void putData(
)
{
System.out.println(“roll
no is”+rno);
System.out.println(“name
is”+name);
}
}
class Marks
extends Adm
{
float m1,
m2, m3;
void
getMarks(float x, float y, float z)
{
m1=x;
m2=y;
m3=z;
}
void
putMarks( )
{
System.out.println(“marks
obtained “+m1+” ”+ m2+” ”+m3);
}
}
class Grade
extends Marks
{
float tot,
avg;
char g;
void getGrade( )
{
tot=m1+m2+m3;
avg=tot/3;
if(avg>=90)
g=A*;
else
g=A;
}
void putGrade( )
{
putData( );
putMarks( );
System.out.println(“total
is”+tot);
System.out.println(“average
is”+avg);
System.out.println(“grade
is”+g);
}
}
class Admin
{
public static
void main (String args[ ] )
{
Grade obj=new Grade( );
obj.getData(999,
“raja”);
obj.getMarks(99,100,99);
obj.getGrade(
);
obj.putGrade(
) ;
}
}
Hierarchical inheritance:
Another
interesting application of inheritance is to be use it as a support to the
hierarchical design of a program. Many programming problems can be cast into a
hierarchy where certain features of one level are shared by many others below
the level. Below figure shows a hierarchical
classification of accounts in a commercial bank. This is possible
because all the accounts processes certain common features.
|