Power of super in Java

Java

Java is a High Level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.

Inheritance

In object-oriented programming, inheritance is the mechanism of bas…


This content originally appeared on DEV Community and was authored by Rahul kumar

Java

Java is a High Level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.

Inheritance

In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object or class, retaining similar implementation. Also defined as deriving new classes from existing ones such as super class or base class and then forming them into a hierarchy of classes.

Subclass and Superclass

In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:

  • subclass (child) - the class that inherits from another class
  • superclass (parent) - the class being inherited from To inherit from a class, use the extends keyword.

super keyword

The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.

Uses

Suppose you have a class A which have a member int top. You have another class B which inherits A and it also has a member top. Now, if you are initiating an instance of B and you want to get the top member of A from B instance. Do you think you can get this without super?

Let's see:

class A{
    int a;
    int top=90;
    A(int a){
        this.a = a;
    }
}
class B extends A{
    int b;
    int top = 40;
    B(int a, int b){
        super(a);
        this.b = b;
    }
}

You have the class A and B. Let's create an object.

B obj = new B(4,5);

obj.a // 4
obj.b // 5
obj.top // 40

Now, you want to get the top member of A. For this you need to use super keyword.

Modify B as needed

class B extends A{
    int b;
    int top = 40;
    B(int a, int b){
        super(a);
        this.b = b;
    }

    int getATop() {
        return super.top;
    }
}

Call the member:

B obj = new B(4,5);
obj.top // 40
obj.getATop() // 90

Note

  • super can access members from any level of hierarchy
  • if inside hierarchy any member or method overriden by any class, then calling super will get the member or method from the class just one level up.

Thanku


This content originally appeared on DEV Community and was authored by Rahul kumar


Print Share Comment Cite Upload Translate Updates
APA

Rahul kumar | Sciencx (2021-05-15T15:13:16+00:00) Power of super in Java. Retrieved from https://www.scien.cx/2021/05/15/power-of-super-in-java/

MLA
" » Power of super in Java." Rahul kumar | Sciencx - Saturday May 15, 2021, https://www.scien.cx/2021/05/15/power-of-super-in-java/
HARVARD
Rahul kumar | Sciencx Saturday May 15, 2021 » Power of super in Java., viewed ,<https://www.scien.cx/2021/05/15/power-of-super-in-java/>
VANCOUVER
Rahul kumar | Sciencx - » Power of super in Java. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/15/power-of-super-in-java/
CHICAGO
" » Power of super in Java." Rahul kumar | Sciencx - Accessed . https://www.scien.cx/2021/05/15/power-of-super-in-java/
IEEE
" » Power of super in Java." Rahul kumar | Sciencx [Online]. Available: https://www.scien.cx/2021/05/15/power-of-super-in-java/. [Accessed: ]
rf:citation
» Power of super in Java | Rahul kumar | Sciencx | https://www.scien.cx/2021/05/15/power-of-super-in-java/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.