INHERITANCE
-----------
IIQ)What is inheritance?
Deriving new classes from existing classes such that the new classes acquire all the features of existing classes is called inheritance.
The syntax of inheritance is:
class subclass extends superclass
IIQ)Why super class members are available to sub class?
Because,the sub class object contains a copy of super class object.
IIQ)What is the advantage of inheritance?
In inheritance,a programmer reuses the super class code without rewriting it,in creation of sub classes.So,developing the classes becomes very easy.Hence,the programmer's productivity is increased.
ex:- (program)
super:- (keyword)
-----
Actually we create sub class object.Because by using this object we can access both sub class members and super class memebers.
But some times,the super class members and sub class members are same names.In that case,by default only subclass members are accessible.In such case,'super' keyword has been invented.
'super' refers to superclass members from a sub class.
ex:-
1.super can be used to refer to super class variables,as:
super.variable;
2.super can be used to refer to super class methods are:
super.method();
3.super can be used to refer to super class constructor.
We need not call the default construtor of the super class,as it is by default available to sub class.
To call the parametereized constructor,we can write:
super(values);
ex:-(program)
Protected specifier:-
--------------------
The private members of super class are not available to sub classes directly.But some times,there may be a need to access the data of super class in the sub class.For this purpose,'protected' specifier is used.
protected is commonly used in super class to make the members of super class available directly in its sub classes.
We can think that the protected specifier works like public with respect to sub classes.
ex:-
Types of inheritance:-
---------------------
There are two types of inheritance:
1.Single inheritance 2.Multiple inheritance.
1.single inheritance:-
Producing sub classes from single super class is called single inheritance.
In this,a single super class will be there.there can be one or more sub classes.
diagram:-
2.Multiple inheritance:-
Producing sub classes from multiple super classes is called multiple inheritance.
In this,there will be more than one super class and there can be one or more sub classes.
diagram:-
=>Only single inheritance is available in JAVA.There is no multiple inheritance in java.
The following are the reasons:
-----------------------------
1.Multiple inheritance leads to confusion for the programmer.
ex:- class A has got a member x,and class B also got a member x.When another class C extends both the classes,then there is confusion regarding which copy of x is available in C.
2.Multiple inheritance is available in C++,which is not available in Java.This leads to disappointment in Java programmers.A java programmer wishes to use multiple inheritance in some cases.Fortunately,JavaSoft people have provided interface concept,expecting the programmers to achieve multiple inheritance by using multiple interfaces.
ex:- we can write,
class Myclass implements interface1,interface2,....
*achieving multiple inhertance by using interfaces is not useful.
3.The programmer feels achieving multiple inheritance by using classes would be practically useful to him.The way to achieve multiple inheritance is by repeating the use of single inheritace.
ex:- class B extends A
class C extends B
H.W:-
1)create Rectangle class from square class and display the area of both the Rectangle and square class.
No comments:
Post a Comment