qwertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfgh
jklzxcvbnmqwertyuiopasdfghjklzxcvb
nmqwertyuiopasdfghjklzxcvbnmqwer
tyuiopasdfghjklzxcvbnmqwertyuiopas
dfghjklzxcvbnmqwertyuiopasdfghjklzx
cvbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyuio
pasdfghjklzxcvbnmqwertyuiopasdfghj
klzxcvbnmqwertyuiopasdfghjklzxcvbn
mqwertyuiopasdfghjklzxcvbnmqwerty
uiopasdfghjklzxcvbnmqwertyuiopasdf
ghjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmrty
uiopasdfghjklzxcvbnmqwertyuiopasdf
ghjklzxcvbnmqwertyuiopasdfghjklzxc
Super keyword
Method overloading and
method overriding
Super keyword:
The super is a reference variable that is used to refer immediate parent
class objects.
Use:
1. Super() is used to refer the immediate parent class instance
variables.
2. Super() is used to invoke immediate parent class constructor.
3. Super() is used to invoke immediate parent class methods.
Example:
ClassVehicle
{
Int speed=100;
}
ClassBike extendsvehicle
{
Int speed=50;
Voiddisplay()
{
System.out.println(“speed=”+super.speed);
}
Publicstaticvoidmain(Stringarr[])
{
Bike b= new Bike();
b.display();
}
}
Output:speed=100
Example:the followingprogram show the use of superfor immediate parent class constructor
invocation
classBox
{
int height,width;
publicBox(inth,intw)
{
height=h;
width=w;
}
}
classBox1 extendsBox
{
intlength;
publicBox1(inth,intw,intl)
{
super(h,w);
length=l;
}
publicvoiddisplay()
{
System.out.println("height=t"+height+"
width=t"+width+"length=t"+length);
}
}
classBox3 extendsBox1
{
intweight;
publicBox3(inth,intw,intl,intw1)
{
super(h,w,l);
weight=w1;
}
publicvoid display1()
{
intdim=height*width*length*weight;
System.out.println("dimensionsare=t"+dim);
}
}
classBox2
{
publicstaticvoidmain(Stringarr[])
{
Box3 a=new Box3(10,20,20,70);
a.display();
a.display1();
}
}
Note:to access constructor with in second class i.e constructor chainingwe use super.
To access constructor within a class we use this keyword.
Example:Use ofsuper for constructor chaining with in two classes
classcommon
{
intl ,b;
publiccommon(intx,inty)
{
l=x;
b=y;
}
voiddisplay()
publicvoiddisplay()
{
super.display();
System.out.println("height="+h);
}
publicintvol()
{
returnl*b*h;
}
}
classcubetest
{
{
System.out.println("length="+l);
System.out.println("Breadth="+b);
}
}
classRectangle extendscommon
{
publicRectangle(intx ,inty)
{
super(x,y);
}
publicintarea()
{
returnl*b;
}
}
classcube extendsRectangle
{
inth;
publiccube(int x,inty,intz)
{
super(x,y);
h=z;
}
publicstaticvoidmain(Stringarr[])
{
cube c = new cube(40,20,30);
//Rectangle r=new Rectangle(30,20);
c.display();
System.out.println("areais
equal="+c.area());
System.out.println("areais
equal="+c.vol());
}}
Method overriding:
Methodoverriding occurs whenasubclass defines amethodthat has the
same signature and return type as method insuper class.
Note: method with different signature is overloaded not overridden
ClassA ClassB extendsA
{
Int I,j;
A(inta,intb)
{
I=a;
J=b;
}
Voidshow()
{
System.out.println(“I&j”+i+””+j);
}
}
{
Int k;
B(inta,intb,intc)
{
Super(a,b);
K=c;
}
Voidshow()
{
System.out.println(“k===”+k);
}
}
Classoverload
{
Publicstaticvoidmain(Stringarr[])
{
B obj=new B(1,2,3);
Obj.show();
}
}
Output:subclassshow() overriddensuperclassshow()
K===3
What is importance of method overriding;
Overriddenmethodallowsjavatosupportrun-time polymorphism
Whenan overriddenmethodis calledthrough a superclass reference.Whichversionof
methodis executed.
The versionof an overriddenmethodthatisexecutedisdeterminedbythe type of the object
beingreferencedtoatthe time of call.Thisdeterminationismade atrun time.
Defaultconstructorisprovidedby
compilerisclassisempty:
class A
{
publicA()
{
System.out.println("i amA");
}
}
classB extendsA
{
class A extendsObject
{
publicA()
{
Super();
System.out.println("i amA");
}
}
classB extendsA
{
VoidB()
}
classC extendsB
{
publicC()
{
System.out.println("i amin c");
}
publicstaticvoidmain(Stringarr[])
{
A x= newA();
B y=newB();
C z=newC();
}
}
Output:
I am inA
I am inA
I am inA
I am inc
{
Super();
}
}
classC extendsB
{
publicC()
{
Super();
System.out.println("i amin c");
}
publicstaticvoidmain(Stringarr[])
{
A x= new A();
B y=new B();
C z=new C();
}
}
Explanation:
In javaeach classis a director indirectsubclassof java.lang.Object;
At the time of compilationcompileradds extendsObjectto definitionofeachindependent
class.
In case of inheritance eachsubclass inheritsdata memberfrom its superclassconstructor.
Compilerdo the following:
1. If a userdefinedclassdoesnothave aconstructor thencompileraddsadefault
constructorand writesSuperkeywordinit.
2. If a userdefinedclasshasconstructor thencompilerwritessuperkeywordtoeachof
the constructor whichdoesnotcontainsThisor superkeywordasitsfirststatement.
Difference between method overloading and method
overriding.
Overloading overriding
1. Method overloading is used Method overriding is used to
to increase the readability of
the program
provide the specific
implementation of the method
that is already provided by its
super class
2. Method overloading is
performed with in a class
Method overriding occurs in the
class that has IS-A relationship
3. In case of method
overloading parameter must
be different.
In case of method overriding
parameter must be same
Question: why we cannot override static method.
Ans: because static method is bound with class whereas
instance method is bound to object. Static belongs to class
area and instance belongs to heap area.

Super keyword