Super keyword

vishalhim 37 views 7 slides Feb 03, 2022
Slide 1
Slide 1 of 7
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7

About This Presentation

SUPER KEYWORD IN JAVA


Slide Content

qwertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfgh
jklzxcvbnmqwertyuiopasdfghjklzxcvb
nmqwertyuiopasdfghjklzxcvbnmqwer
tyuiopasdfghjklzxcvbnmqwertyuiopas
dfghjklzxcvbnmqwertyuiopasdfghjklzx
cvbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyuio
pasdfghjklzxcvbnmqwertyuiopasdfghj
klzxcvbnmqwertyuiopasdfghjklzxcvbn
mqwertyuiopasdfghjklzxcvbnmqwerty
uiopasdfghjklzxcvbnmqwertyuiopasdf
ghjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmrty
uiopasdfghjklzxcvbnmqwertyuiopasdf
ghjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmqw



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:
Class Vehicle
{
Int speed=100;
}
Class Bike extends vehicle
{
Int speed=50;

Void display()
{
System.out.println(“speed=”+super.speed);
}
Public static void main(String arr[])
{
Bike b= new Bike();
b.display();
}
}

Output: speed=100
Example: the following program show the use of super for immediate parent class constructor
invocation

class Box
{
int height,width;

public Box(int h,int w)
{
height=h;
width=w;
}

}
class Box1 extends Box
{
int length;
public Box1(int h,int w,int l)
{
super(h,w);
length=l;
}
public void display()
{
System.out.println("height=\t"+height+"
width=\t"+width+"length=\t"+length);
}
}

class Box3 extends Box1
{
int weight;
public Box3(int h,int w,int l,int w1)
{
super(h,w,l);
weight=w1;
}
public void display1()
{
int dim=height*width*length*weight;
System.out.println("dimensions are=\t"+dim);
}
}
class Box2
{
public static void main(String arr[])
{
Box3 a=new Box3(10,20,20,70);
a.display();
a.display1();
}
}

Note: to access constructor with in second class i.e constructor chaining we use super.
To access constructor with in a class we use this keyword.
Example:Use of super for constructor chaining with in two classes

class common
{

int l ,b;

public common(int x,int y)
{

l=x;
b=y;
}

void display()
public void display()
{
super.display();
System.out.println("height="+h);
}

public int vol()
{
return l*b*h;
}
}

class cubetest
{

{
System.out.println("length="+l);
System.out.println("Breadth="+b);
}
}

class Rectangle extends common
{

public Rectangle(int x ,int y)
{
super(x,y);

}
public int area()
{
return l*b;
}
}
class cube extends Rectangle
{
int h;
public cube(int x,int y,int z)
{

super(x,y);
h=z;
}



public static void main(String arr[])
{
cube c = new cube(40,20,30);
//Rectangle r=new Rectangle(30,20);
c.display();
System.out.println("area is
equal="+c.area());
System.out.println("area is
equal="+c.vol());
}}




Method overriding:
Method overriding occurs when a subclass defines a method that has the
same signature and return type as method in super class.
Note: method with different signature is overloaded not overridden
Class A Class B extends A

{
Int I,j;
A(int a,int b)
{
I=a;
J=b;
}
Void show()
{
System.out.println(“I &j”+i+””+j);
}
}

{
Int k;
B(int a,int b,int c)
{
Super(a,b);
K=c;
}
Void show()
{
System.out.println(“k===”+k);
}
}
Class overload
{
Public static void main(String arr[])
{
B obj=new B(1,2,3);
Obj.show();
}
}

Output: subclass show() overridden super class show()
K===3
What is importance of method overriding;
Overridden method allows java to support run-time polymorphism
When an overridden method is called through a super class reference. Which version of
method is executed.
The version of an overridden method that is executed is determined by the type of the object
being referenced to at the time of call. This determination is made at run time.

Default constructor is provided by
compiler is class is empty:
class A
{
public A()
{
System.out.println("i am A");
}
}
class B extends A
{
class A extends Object
{
public A()
{
Super();
System.out.println("i am A");
}
}
class B extends A
{
Void B()

}
class C extends B
{
public C()
{
System.out.println(" i am in c");
}
public static void main(String arr[])
{
A x= new A();
B y=new B();
C z=new C();
}
}
Output:
I am in A
I am in A
I am in A
I am in c

{
Super();
}
}
class C extends B
{
public C()
{
Super();
System.out.println(" i am in c");
}
public static void main(String arr[])
{
A x= new A();
B y=new B();
C z=new C();
}

}
Explanation:
In java each class is a direct or indirect subclass of java.lang.Object;
At the time of compilation compiler adds extends Object to definition of each independent
class.
In case of inheritance each subclass inherits data member from its superclass constructor.
Compiler do the following:
1. If a user defined class does not have a constructor then compiler adds a default
constructor and writes Super keyword in it.
2. If a user defined class has constructor then compiler writes super keyword to each of
the constructor which does not contains This or super keyword as its first statement.

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.