Powered by Blogger.

Super Keyword in Java



Super Keyword in Java

Super Keyword in Java: is used to refer parent class object. It is used inside a sub-class method definition to call a method defined in the super class.

Super keyword in java

Problem without super:

class Vehicle
{
int speed=50;
}
class Bike3 extends Vehicle
{
int speed=100;
void display(){
System.out.println(speed);//will print speed of Bike
}
public static void main(String args[])
{
Bike3 b=new Bike3();
b.display();
}
}

Output:

100

Solution with super:

Example of super keyword


class Vehicle
{
int speed=50;
}

class Bike4 extends Vehicle
{
int speed=100;

void display(){
System.out.println(super.speed);//will print speed of Vehicle now
}
public static void main(String args[])
{
Bike4 b=new Bike4();
b.display();
}
}

Output:

50



facebook
google-plus
twitter
pinterest

No comments :

Post a Comment

HTML Tutorial

images

About Sitesbay

This blog is related to Java Tutorial, C++ Tutorial, C Tutorial, Online Earning tips and more in very simple and easy way. Here i will try to give complete idea related to all new technology.

Java Tutorial

Java is Object oriented programming language, It is more secure and high performance language.

C++ Tutorial

C++ is also Object oriented programming language, It is more simple and easy to learn.

 
https://sitesbay.blogspot.com/?utm_medium=d2898efa59afcc0bc411921036e22b767ca37b09