Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The super keyword is used in 3 cases:-
- 1. To refer instance variable of immediate parent class
- 2. To invoke custructor of immediate parent class
- 3. To invoke a method of immediate parent class
- */
- class SuperClass{
- int a = 1; //instance variable of parent class
- SuperClass(){
- System.out.println("\nControl is in super class Constructor.");
- }
- void SuperClassMethod(){
- System.out.println("\nControl is in super class Method.");
- }
- }
- class SubClass extends SuperClass{
- int a = 2; //instance variable of subclass
- SubClass(){
- super(); //To call the immediate parent's constructor
- System.out.println("\nControl is in sub class Constructor.");
- }
- void SubClassMethod(){
- System.out.println("\nControl is in sub class Method");
- super.SuperClassMethod(); //To invoke a method of immediate parent class
- System.out.println("\na(super class): "+super.a);//To refer to class variable of immediate paraent class
- System.out.println("\na(sub class): "+a);
- }
- }
- class SuperTest{
- public static void main(String args[]){
- new SubClass().SubClassMethod();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement