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

class Book
{
  int cost = 50;
}

public class Journal extends Book
{
  int cost = 20;

  public void getCost()
  {
    System.out.println("Journal cost Rs." + cost);
    System.out.println("Book cost Rs." + super.cost);
    System.out.println("Cost of Book and Journal Rs." + (cost + super.cost));
  }
  public static void main(String args[])
  {
    Journal j1 = new Journal();
    j1.getCost();
  }
}

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.

class Hyderabad
{
  public void hotel()
  {
    System.out.println("Bawarchi Hotel is famous for Biryani");
  }
}
public class Secunderabad extends Hyderabad
{
  public void hotel()
  {
    System.out.println("Paradise Hotel is popular for Biryani");
  }
  public void show()
  {
    hotel();
    super.hotel();
  }
  public static void main(String args[])
  {
    Secunderabad s1 = new Secunderabad();
    s1.show();
  }
}

super vs final Java

public void show()
{
hotel();
super.hotel();
}

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.

public class Demo
{
  public static void main(String args[])
  {
    int x = 10;                    // non-final variable
    System.out.println("x value before reassigned: " + x);
    x = 20;                        // can be reassigned
    System.out.println("x value after reassigned: " + x);

    final int y = 100;             // final variable
    System.out.println("y value: " + y);
    // y = 200;		           // compilation error
  }
}

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.

class Doctor
{
  public final void massage()       // cannot be overridden by subclass
  {
    System.out.println("Use Olive oil for massage");
  }
}
public class Massagist extends Doctor
{
  public static void main(String args[])
  {
    Massagist m1 = new Massagist();
    m1.massage();
  }
}

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.

final class God
{
  public void pray()
  {
    System.out.println("Pray God with selfless heart");
  }
}
public class Man		    // cannot extend God
{
  public static void main(String args[])
  {
    God g1 = new God();
    g1.pray();
  }
}     

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 Comment

Your email address will not be published.