Knowledge Walls
John Peter
Pune, Maharashtra, India
What are all ways to use super keyword in java with example
2924 Views
Hints 
They are three purpose to use super keyword.

1. To call super class constructors from sub class constructors.
    super();
    super(10,20);

2. To access super class member variables. This is not accessible by sub class If sub class has the same variable name otherwise super.
   super.variable_name = 10;

3. To call super class methods. This is not accessible by sub class If sub class has the same method name which is in super class.
   super.display();
Example
class Cricket {
    int runs;
    int over;
    
    public Cricket(){
        over = 99;
    }
    
    public void display(){
        System.out.println("Runs: "+runs+";Over: "+over);
    }
}

class CricketCounter extends Cricket{
    public CricketCounter(){
        super();
    }
    public void display(){
        super.runs = 82;
        super.display();
    }
}

class Solution {
    public static void main(String args[])throws Exception{
        CricketCounter cric = new CricketCounter();
        cric.display();
    }
}
Output 
Runs: 82;Over: 99
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details