Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Super Keyword in Java, Schemes and Mind Maps of Java Programming

The super keyword in Java is a reference variable which is used to refer immediate parent class object. Whenever you create the instance of subclass, ...

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 09/27/2022

mariners
mariners 🇺🇸

4.5

(15)

247 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Super Keyword in Java
The super keyword in Java is a reference variable which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is
referred by super reference variable.
Usage of Java super Keyword
1. super can be used to refer immediate parent class instance variable.
2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.
1) super is used to refer immediate parent class instance
variable.
We can use super keyword to access the data member or field of parent class. It is used if parent class
and child class have same fields.
1. class Animal{
2. String color="white";
3. }
4. class Dog extends Animal{
5. String color="black";
pf3
pf4

Partial preview of the text

Download Super Keyword in Java and more Schemes and Mind Maps Java Programming in PDF only on Docsity!

Super Keyword in Java

The super keyword in Java is a reference variable which is used to refer immediate parent class object. Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.

Usage of Java super Keyword

  1. super can be used to refer immediate parent class instance variable.
  2. super can be used to invoke immediate parent class method.
  3. super() can be used to invoke immediate parent class constructor.

1) super is used to refer immediate parent class instance

variable.

We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.

  1. class Animal{
  2. String color="white";
  3. }
  4. class Dog extends Animal{
  5. String color="black";
  1. void printColor(){
  2. System.out.println(color);//prints color of Dog class
  3. System.out.println( super .color);//prints color of Animal class
  4. }
  5. }
  6. class TestSuper1{
  7. public static void main(String args[]){
  8. Dog d= new Dog();
  9. d.printColor();
  10. }} Test it Now Output: black white In the above example, Animal and Dog both classes have a common property color. If we print color property, it will print the color of current class by default. To access the parent property, we need to use super keyword.

2) super can be used to invoke parent class method

The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden.

  1. class Animal{
  2. void eat(){System.out.println("eating...");}
  3. }
  4. class Dog extends Animal{
  5. void eat(){System.out.println("eating bread...");}
  6. void bark(){System.out.println("barking...");}
  7. void work(){
  8. super .eat();
  9. bark();
  10. }
  11. }
  12. class TestSuper2{
  13. public static void main(String args[]){
  14. Dog d= new Dog();
  15. d.work();
  16. }} Test it Now Output: eating... barking...
  1. class Animal{
  2. Animal(){System.out.println("animal is created");}
  3. }
  4. class Dog extends Animal{
  5. Dog(){
  6. System.out.println("dog is created");
  7. }
  8. }
  9. class TestSuper4{
  10. public static void main(String args[]){
  11. Dog d= new Dog();
  12. }} Test it Now Output: animal is created dog is created

super example: real use

Let's see the real use of super keyword. Here, Emp class inherits Person class so all the properties of Person will be inherited to Emp by default. To initialize all the property, we are using parent class constructor from child class. In such way, we are reusing the parent class constructor.

  1. class Person{
  2. int id;
  3. String name;
  4. Person( int id,String name){
  5. this .id=id;
  6. this .name=name;
  7. }
  8. }
  9. class Emp extends Person{
  10. float salary;
  11. Emp( int id,String name, float salary){
  12. super (id,name);//reusing parent constructor
  13. this .salary=salary;
  14. }
  15. void display(){System.out.println(id+" "+name+" "+salary);}
  16. }
  17. class TestSuper5{
  18. public static void main(String[] args){
  19. Emp e1= new Emp( 1 ,"ankit",45000f);
  20. e1.display();
  21. }}

Output:1 ankit 45000