Super Keyword in Java

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";  
  6. void printColor(){  
  7. System.out.println(color);//prints color of Dog class  
  8. System.out.println(super.color);//prints color of Animal class  
  9. }  
  10. }  
  11. class TestSuper1{  
  12. public static void main(String args[]){  
  13. Dog d=new Dog();  
  14. d.printColor();  
  15. }}  
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...
In the above example Animal and Dog both classes have eat() method if we call eat() method from Dog class, it will call the eat() method of Dog class by default because priority is given to local.
To call the parent class method, we need to use super keyword.

3) super is used to invoke parent class constructor.

The super keyword can also be used to invoke the parent class constructor. Let's see a simple example:
  1. class Animal{  
  2. Animal(){System.out.println("animal is created");}  
  3. }  
  4. class Dog extends Animal{  
  5. Dog(){  
  6. super();  
  7. System.out.println("dog is created");  
  8. }  
  9. }  
  10. class TestSuper3{  
  11. public static void main(String args[]){  
  12. Dog d=new Dog();  
  13. }}  
Test it Now
Output:
animal is created
dog is created

Note: super() is added in each class constructor automatically by compiler if there is no super() or this().

java super
As we know well that default constructor is provided by compiler automatically if there is no constructor. But, it also adds super() as the first statement.
Another example of super keyword where super() is provided by the compiler implicitly.
  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. }}  
Test it Now
Output:
1 ankit 45000

compareTo in Java

Java String compareTo()
The java string compareTo() method compares the given string with current string lexicographically. It returns positive number, negative number or 0.
It compares strings on the basis of Unicode value of each character in the strings.
If first string is lexicographically greater than second string, it returns positive number (difference of character value). If first string is less than second string lexicographically, it returns negative number and if first string is lexicographically equal to second string, it returns 0.
  1. if s1 > s2, it returns positive number  
  2. if s1 < s2, it returns negative number  
  3. if s1 == s2, it returns 0  
Signature
  1. public int compareTo(String anotherString)  
  2. Parameters
anotherString: represents string that is to be compared with current string
Returns
an integer value
Java String compareTo() method example
  1. public class CompareToExample{  
  2. public static void main(String args[]){  
  3. String s1="hello";  
  4. String s2="hello";  
  5. String s3="meklo";  
  6. String s4="hemlo";  
  7. String s5="flag";  
  8. System.out.println(s1.compareTo(s2));//0 because both are equal  
  9. System.out.println(s1.compareTo(s3));//-5 because "h" is 5 times lower than "m"  
  10. System.out.println(s1.compareTo(s4));//-1 because "l" is 1 times lower than "m"  
  11. System.out.println(s1.compareTo(s5));//2 because "h" is 2 times greater than "f"  
  12. }}  

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...