Difference between super and final Java

Difference between super and final Java

super vs final Java are keywords of Java but doing very different jobs.
  1. super keyword is used to access super class variables and methods by subclass object when they are overridden by subclass.
    1. final is used in three places in Java with different jobs.

    2. final variable cannot be reassigned (like const of C/C++)
    3. final method cannot be overridden
    4. final class cannot be extended by other classes

Let us explain one-by-one programmatically.

I) Usage of super Keyword in Java

super keyword is used by the Programmer to call super class variables and methods by subclass object when they overridden by subclass. What does it mean? See this code.

a) super with variables

super vs final Java

In the above code, cost variable exists in two classes – super class Book and subclass Journal. That is, cost variable of super class is overridden by subclass. If overridden, the subclass Journal object cannot call super class Book cost variable in general circumstances. To call super class cost variable, subclass uses super keyword.

System.out.println("Journal cost Rs." + cost);
System.out.println("Book cost Rs." + super.cost);

In the above code, cost prints Journal cost and super.cost prints Book cost.

b) super with methods

Similar to variable overriding, a method also can be overridden. If the super class and subclass have the same method (with same parameters and return type), we say super class method is overridden by subclass. If overridden, the subclass object will call its own method. To call super class method also, the subclass uses super keyword.

In the following code, the hotel() method is overridden.

super vs final Java

public void show()

In the above code, from show() method of subclass Secunderabad, hotel() is called. It calls Secunderabad hotel() method. Call to super.hotel(), calls super class Hyderabad hotel() method.

Note: super cannot be used from static context. That is, super cannot be used from static methods including main().

II) Usage of final Keyword in Java

As stated earlier, the final keyword works differently when called with variables, methods and classes. But final conveys a meaning that something cannot happen.

a) final with variables

In the following code, x variable is declared as non-final and y as final variable. As per rule, the non-final x can be reassigned and final y cannot. Java replaces const keyword of C/C++ with final. That is, a final variable works as a constant that cannot be reassigned.

super vs final Java

// y = 200;

If the comments are removed, compiler raises error (as y is declared final).

b) final with methods

Sometimes, the super class may think of that its methods cannot be overridden by subclass. Then, super class declares those methods as final. With non-final methods, the subclass is at liberty to override or not.

super vs final Java

In the above code, massage() method is declared as final by super class Doctor. The subclass Massagist cannot override but still can call with its object m1.

c) final with classes

If the Programmer does not like a class not to be inherited (or extended) by other classes, then declares it as final. That is, a final class can be inherited.

super vs final Java

final class God

class God is declared as final so that subclass Man cannot extend it. But still, Man can use final God methods by composition.

What is composition or has-a relationship?

Creating other class object in our class and calling other class methods is known as composition. We say, our class has-a object of other class.

View All for Java Differences on 90 Topics

2 thoughts on “Difference between super and final Java

Leave a Reply

Your email address will not be published. Required fields are marked *