INTRODUCTION:
Hey folks..in this post I will tell you what inheritance
actually mean..!!Lets jump to the post..
INHERITANCE… A SIMPLE RELATIONSHIP
Consider X’s dad as an example.. X’s dad will have
properties like
color – white
hair- black etc and actions like,
Smartness
Diabetes
So, the structure will be
Class Dad
{
String color = “white”;
String haricolor= “black”;
public void Smartness( )
{
System.out.println(“ I am dad and I am smart ;) “);
}
public void Diabetes( )
{
System.out.println(“ Ooops.. I am dad and I have diabetes”
);
}
}
Now look at the above example..
X’s dad has properties like haircolor black and tone color
as white , Actions like Smartness and Diabetes etc.
X’s Dad is soo happy when X was born . He saw his son’s
unique characteristics like..” tall “ and
he was even more surprised when X realized that his son has his properties and
actions ..
He want to express this to his relatives that his son has
his properties and actions inherited..
For that.. X’s dad wrote a structure and it was as follows..
Class Dad
{
String color = “white”;
String haricolor= “black”;
public void Smartness( )
{
System.out.println(“ I am dad and I am smart ;) “);
}
public void Diabetes( )
{
System.out.println(“ Ooops.. I am dad and I have diabetes”
);
}
}
Class Son extends Dad
{
String Height = “tall”
public void swimming ( )
{
System.out.printlln(“Hieeee..I am the son and I know
swimming “);
}
}
EXPLANATION:
Look at the above example here.. Class Dad is the super
Class which means it is the top class and class Son is the sub class meaning
this class is inheriting the properties of the super class.
So, the Class son has properties like height and the son
does swimming..
Ok fine .. but where is the dad specifying that the Son is
inheriting his properties ??
This is done in the SON SUBCLASS..
Extends SUPERCLASS
NAME ( DAD) ------ >>> “is what
telling everybody that my son is extending my characteristics , behavior,
actions and in addition my son is having some unique characteristics and those
will be listed under his class (Son Class) ..
So, this is what INHERITANCE IS.. If we want a class to have
same methods and variables of another class and in addition you want some
unique methods and variables for the sub class then you can extend
super class so that the sub class will contain all the variables , methods and
functions of the super class and in addition will contain its own methods,
variables etc.
HOW TO INVOKE A MEMBER FUNCTION OF A SUB CLASS..?
It can be invoked by creating object for the sub class.
DO WE NEED TO CREATE SEPARATE OBJECT FOR THE SUPER CLASS..??
Definitely not.. !! We are supposed to create object for the sub class and using that sub class object even the super class variables and
methods can be accessed.
Example:
Class Dad
{
String color = “white”;
String haricolor= “black”;
public void Smartness( )
{
System.out.println(“ I am dad and I am smart ;) “);
}
public void Diabetes( )
{
System.out.println(“ Ooops.. I am dad and I have diabetes”
);
}
}
Class Son extends Dad
{
String Height = “tall”;
public void swimming ( )
{
System.out.printlln(“Hieeee..I am the son and I know
swimming “);
}
public static void main(String args[])
{
Son Sonobj = new Son ( ) ;
Sonobj.swimming( );
Sonobj.Smartness( );
}
}
OUTPUT:
Hieeee..I am the son and I know swimming
I am dad and I am smart ;)
EXPLANATION:
Look at the example.. we are creating object for the sub
class.. we are calling a method “swimming” and using that same object we are
calling the method of the SUPER CLASS“Smartness” .
This is because.. the SON
sub class is inheriting all the methods and variables of the super class so,
when we create the object for the sub class , it will have access to all the
member functions of the DAD class.
SON has got inherited characteristics and also some unique
characteristics..
WILL THE CODE RUN IF WE CREATE OBJECT FOR THE SUPER
CLASS..??
Of course yes.. !! We can create separate object for the
super class and invoke its member functions(variables and methods ) or we can
create an object for the sub class and can invoke from that.!!
SO WHY DO WE CREATE OBJECT FOR THE SUB CLASS AND CALL THE
SUPER CLASS USING THAT OBJECT??
The answer is pretty simple.. !! Its just efficiency !! When
we create separate object for the super class then separate MEMORY ALLOCATION
will be done and when we create separate object for the sub class then another
memory also will get allocated for that object..
Its just unnecessary waste of memory !!
Pity machine has to handle everything.. let’s help it by avoiding
this unnecessary memory allocation..!!
CONCLUSION:
So.. that’s it about this post.. !! Email me for any
queries..Please hit LIKE button…drop your valuable feedbacks and help me post
more..!! Request for the post
you wish and suggestions for the post is also welcomed.. !!
Stay tuned for more posts!! Have a pleasant day :)
No comments :
Post a Comment