Follow us on Facebook

Header Ads

Welcome to JAVA POint

Super Keyword in Java With Example(JAVA)

                                     Super Keyword in Java  With Example

The super watchword alludes to the objects of prompt parent class. Prior to learning super catchphrase you should have the information on legacy in Java so you can comprehend the models given in this guide.



The Use of  Super Keyword:

1) To get to the information individuals from parent class when both parent and youngster class have part with same name 

2) To expressly consider the no-arg and defined constructor of parent class 

3) To get to the technique for parent class when youngster class has superseded that strategy. 

Presently lets examine them in detail with the assistance of models:


1) How to utilize super catchphrase to get to the factors of parent class 


At the point when you have a variable in kid class which is as of now present in the parent class then to get to the variable of parent class, you need to utilize the super catchphrase. 


Lets take a guide to get this: In the accompanying system, we have an information part num pronounced in the kid class, the part with a similar name is as of now present in the parent class. Its absolutely impossible you can get to the num variable of parent class without utilizing super catchphrase. . 


/Parent class or Superclass or base class 


class Superclass 



int num = 100; 



/Child class or subclass or inferred class 


class Subclass expands Superclass 



/* a similar variable num is pronounced in the Subclass 


* which is now present in the Superclass 


*/ 


int num = 110; 


void printNumber(){ 


System.out.println(num); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printNumber(); 




Yield: 


110 


Getting to the num variable of parent class: 


By calling a variable like this, we can get to the variable of parent class if both the classes (parent and kid) have same variable. 


super.variable_name 


How about we take the very model that we have seen over, this time on paper explanation we are passing super.num rather than num. 


class Superclass 



int num = 100; 



class Subclass broadens Superclass 



int num = 110; 


void printNumber(){ 


/* Note that as opposed to composing num we are 


* composing super.num in the print explanation 


* this alludes to the num variable of Superclass 


*/ 


System.out.println(super.num); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printNumber(); 




Yield: 


100 


As you can see by utilizing super.num we got to the num variable of parent class. 


2) Use of super catchphrase to conjure constructor of parent class 


At the point when we make the object of sub class, the new catchphrase conjures the constructor of kid class, which certainly summons the constructor of parent class. So the request to execution when we make the object of kid class is: parent class constructor is executed first and afterward the kid class constructor is executed. It happens on the grounds that compiler itself adds super()(this conjures the no-arg constructor of parent class) as the primary assertion in the constructor of youngster class. 


How about we see a guide to comprehend what I have clarified previously: 


class Parentclass 



Parentclass(){ 


System.out.println("Constructor of parent class"); 




class Subclass expands Parentclass 



Subclass(){ 


/* Compile certainly adds super() here as the 


* first proclamation of this constructor. 


*/ 


System.out.println("Constructor of kid class"); 



Subclass(int num){ 


/* Even however it is a defined constructor. 


* The compiler actually adds the no-arg super() here 


*/ 


System.out.println("arg constructor of youngster class"); 



void display(){ 


System.out.println("Hello!"); 



public static void main(String args[]){ 


/* Creating object utilizing default constructor. This 


* will summon kid class constructor, which will 


* conjure parent class constructor 


*/ 


Subclass obj= new Subclass(); 


/Calling sub class strategy 


obj.display(); 


/* Creating second item utilizing arg constructor 


* it will conjure arg constructor of youngster class which will 


* conjure no-arg constructor of parent class naturally 


*/ 


Subclass obj2= new Subclass(10); 


obj2.display(); 




Yield: 


Constructor of parent class 


Constructor of kid class 


Hi! 


Constructor of parent class 


arg constructor of kid class 


Hi! 


Defined super() call to conjure defined constructor of parent class 


We can call super() expressly in the constructor of youngster class, however it would not bode well since it would be excess. It resembles unequivocally accomplishing something which would be certainly done something else. 


Anyway when we have a constructor in parent class that takes contentions then we can utilize defined super, as super(100); to conjure defined constructor of parent class from the constructor of kid class. 


We should see a guide to get this: 


class Parentclass 



/no-arg constructor 


Parentclass(){ 


System.out.println("no-arg constructor of parent class"); 



/arg or defined constructor 


Parentclass(String str){ 


System.out.println("parameterized constructor of parent class"); 




class Subclass expands Parentclass 



Subclass(){ 


/* super() should be added to the main assertion of constructor 


* else you will get an aggregation blunder. Another significant 


* highlight note is that when we expressly utilize very in constructor 


* the compiler doesn't conjure the parent constructor naturally. 


*/ 


super("Hahaha"); 


System.out.println("Constructor of kid class"); 



void display(){ 


System.out.println("Hello"); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.display(); 




Yield: 


defined constructor of parent class 


Constructor of youngster class 


Hi 


There are not many significant focuses to note in this model: 


1) super()(or defined very should be the primary assertion in constructor else you will get the accumulation mistake: "Constructor call should be the principal articulation in a constructor" 


2) When we expressly positioned very in the constructor, the java compiler didn't consider the default no-arg constructor of parent class. 


3) How to utilize super watchword if there should be an occurrence of strategy abrogating 


At the point when a kid class proclaims an equivalent technique which is as of now present in the parent class then this is called strategy abrogating. We will learn technique abrogating in the following instructional exercises of this arrangement. For the time being you simply need to recollect this: When a kid class supersedes a strategy for parent class, at that point the call to the technique from kid class object consistently call the youngster class variant of the technique. Anyway by utilizing super catchphrase like this: super.method_name you can call the technique for parent class (the strategy which is superseded). If there should arise an occurrence of technique abrogating, these phrasings are utilized: Overridden strategy: The strategy for parent class Overriding technique: The technique for youngster class Lets take a guide to comprehend this idea: 


class Parentclass 



/Overridden strategy 


void display(){ 


System.out.println("Parent class strategy"); 




class Subclass expands Parentclass 



/Overriding strategy 


void display(){ 


System.out.println("Child class strategy"); 



void printMsg(){ 


/This would call Overriding strategy 


show(); 


/This would call Overridden strategy 


super.display(); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printMsg(); 




Yield: 


Youngster class technique 


Parent class technique 


Consider the possibility that the youngster class isn't abrogating any technique: No need of super 


At the point when kid class doesn't supersede the parent class technique then we don't have to utilize the super catchphrase to call the parent class strategy. This is on the grounds that for this situation we have just a single adaptation of every strategy and youngster class approaches the parent class techniques so we can straightforwardly call the strategies for parent class without utilizing super. 


class Parentclass 



void display(){ 


System.out.println("Parent class strategy"); 




class Subclass expands Parentclass 



void printMsg(){ 


/* This would call strategy for parent class, 


* no compelling reason to utilize super catchphrase in light of the fact that no other 


* technique with a similar name is available in this class 


*/ 


show(); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printMsg(); 




Yield: 


Parent class strategy1) How to utilize super catchphrase to get to the factors of parent class 


At the point when you have a variable in kid class which is as of now present in the parent class then to get to the variable of parent class, you need to utilize the super catchphrase. 


Lets take a guide to get this: In the accompanying system, we have an information part num pronounced in the kid class, the part with a similar name is as of now present in the parent class. Its absolutely impossible you can get to the num variable of parent class without utilizing super catchphrase. . 


/Parent class or Superclass or base class 


class Superclass 



int num = 100; 



/Child class or subclass or inferred class 


class Subclass expands Superclass 



/* a similar variable num is pronounced in the Subclass 


* which is now present in the Superclass 


*/ 


int num = 110; 


void printNumber(){ 


System.out.println(num); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printNumber(); 




Yield: 


110 


Getting to the num variable of parent class: 


By calling a variable like this, we can get to the variable of parent class if both the classes (parent and kid) have same variable. 


super.variable_name 


How about we take the very model that we have seen over, this time on paper explanation we are passing super.num rather than num. 


class Superclass 



int num = 100; 



class Subclass broadens Superclass 



int num = 110; 


void printNumber(){ 


/* Note that as opposed to composing num we are 


* composing super.num in the print explanation 


* this alludes to the num variable of Superclass 


*/ 


System.out.println(super.num); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printNumber(); 




Yield: 


100 


As you can see by utilizing super.num we got to the num variable of parent class. 


2) Use of super catchphrase to conjure constructor of parent class 


At the point when we make the object of sub class, the new catchphrase conjures the constructor of kid class, which certainly summons the constructor of parent class. So the request to execution when we make the object of kid class is: parent class constructor is executed first and afterward the kid class constructor is executed. It happens on the grounds that compiler itself adds super()(this conjures the no-arg constructor of parent class) as the primary assertion in the constructor of youngster class. 


How about we see a guide to comprehend what I have clarified previously: 


class Parentclass 



Parentclass(){ 


System.out.println("Constructor of parent class"); 




class Subclass expands Parentclass 



Subclass(){ 


/* Compile certainly adds super() here as the 


* first proclamation of this constructor. 


*/ 


System.out.println("Constructor of kid class"); 



Subclass(int num){ 


/* Even however it is a defined constructor. 


* The compiler actually adds the no-arg super() here 


*/ 


System.out.println("arg constructor of youngster class"); 



void display(){ 


System.out.println("Hello!"); 



public static void main(String args[]){ 


/* Creating object utilizing default constructor. This 


* will summon kid class constructor, which will 


* conjure parent class constructor 


*/ 


Subclass obj= new Subclass(); 


/Calling sub class strategy 


obj.display(); 


/* Creating second item utilizing arg constructor 


* it will conjure arg constructor of youngster class which will 


* conjure no-arg constructor of parent class naturally 


*/ 


Subclass obj2= new Subclass(10); 


obj2.display(); 




Yield: 


Constructor of parent class 


Constructor of kid class 


Hi! 


Constructor of parent class 


arg constructor of kid class 


Hi! 


Defined super() call to conjure defined constructor of parent class 


We can call super() expressly in the constructor of youngster class, however it would not bode well since it would be excess. It resembles unequivocally accomplishing something which would be certainly done something else. 


Anyway when we have a constructor in parent class that takes contentions then we can utilize defined super, as super(100); to conjure defined constructor of parent class from the constructor of kid class. 


We should see a guide to get this: 


class Parentclass 



/no-arg constructor 


Parentclass(){ 


System.out.println("no-arg constructor of parent class"); 



/arg or defined constructor 


Parentclass(String str){ 


System.out.println("parameterized constructor of parent class"); 




class Subclass expands Parentclass 



Subclass(){ 


/* super() should be added to the main assertion of constructor 


* else you will get an aggregation blunder. Another significant 


* highlight note is that when we expressly utilize very in constructor 


* the compiler doesn't conjure the parent constructor naturally. 


*/ 


super("Hahaha"); 


System.out.println("Constructor of kid class"); 



void display(){ 


System.out.println("Hello"); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.display(); 




Yield: 


defined constructor of parent class 


Constructor of youngster class 


Hi 


There are not many significant focuses to note in this model: 


1) super()(or defined very should be the primary assertion in constructor else you will get the accumulation mistake: "Constructor call should be the principal articulation in a constructor" 


2) When we expressly positioned very in the constructor, the java compiler didn't consider the default no-arg constructor of parent class. 


3) How to utilize super watchword if there should be an occurrence of strategy abrogating 


At the point when a kid class proclaims an equivalent technique which is as of now present in the parent class then this is called strategy abrogating. We will learn technique abrogating in the following instructional exercises of this arrangement. For the time being you simply need to recollect this: When a kid class supersedes a strategy for parent class, at that point the call to the technique from kid class object consistently call the youngster class variant of the technique. Anyway by utilizing super catchphrase like this: super.method_name you can call the technique for parent class (the strategy which is superseded). If there should arise an occurrence of technique abrogating, these phrasings are utilized: Overridden strategy: The strategy for parent class Overriding technique: The technique for youngster class Lets take a guide to comprehend this idea: 


class Parentclass 



/Overridden strategy 


void display(){ 


System.out.println("Parent class strategy"); 




class Subclass expands Parentclass 



/Overriding strategy 


void display(){ 


System.out.println("Child class strategy"); 



void printMsg(){ 


/This would call Overriding strategy 


show(); 


/This would call Overridden strategy 


super.display(); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printMsg(); 




Yield: 


Youngster class technique 


Parent class technique 


Consider the possibility that the youngster class isn't abrogating any technique: No need of super 


At the point when kid class doesn't supersede the parent class technique then we don't have to utilize the super catchphrase to call the parent class strategy. This is on the grounds that for this situation we have just a single adaptation of every strategy and youngster class approaches the parent class techniques so we can straightforwardly call the strategies for parent class without utilizing super. 


class Parentclass 



void display(){ 


System.out.println("Parent class strategy"); 




class Subclass expands Parentclass 



void printMsg(){ 


/* This would call strategy for parent class, 


* no compelling reason to utilize super catchphrase in light of the fact that no other 


* technique with a similar name is available in this class 


*/ 


show(); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printMsg(); 




Yield: 


Parent class strategy1) How to utilize super catchphrase to get to the factors of parent class 


At the point when you have a variable in kid class which is as of now present in the parent class then to get to the variable of parent class, you need to utilize the super catchphrase. 


Lets take a guide to get this: In the accompanying system, we have an information part num pronounced in the kid class, the part with a similar name is as of now present in the parent class. Its absolutely impossible you can get to the num variable of parent class without utilizing super catchphrase. . 


/Parent class or Superclass or base class 


class Superclass 



int num = 100; 



/Child class or subclass or inferred class 


class Subclass expands Superclass 



/* a similar variable num is pronounced in the Subclass 


* which is now present in the Superclass 


*/ 


int num = 110; 


void printNumber(){ 


System.out.println(num); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printNumber(); 




Yield: 


110 


Getting to the num variable of parent class: 


By calling a variable like this, we can get to the variable of parent class if both the classes (parent and kid) have same variable. 


super.variable_name 


How about we take the very model that we have seen over, this time on paper explanation we are passing super.num rather than num. 


class Superclass 



int num = 100; 



class Subclass broadens Superclass 



int num = 110; 


void printNumber(){ 


/* Note that as opposed to composing num we are 


* composing super.num in the print explanation 


* this alludes to the num variable of Superclass 


*/ 


System.out.println(super.num); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printNumber(); 




Yield: 


100 


As you can see by utilizing super.num we got to the num variable of parent class. 


2) Use of super catchphrase to conjure constructor of parent class 


At the point when we make the object of sub class, the new catchphrase conjures the constructor of kid class, which certainly summons the constructor of parent class. So the request to execution when we make the object of kid class is: parent class constructor is executed first and afterward the kid class constructor is executed. It happens on the grounds that compiler itself adds super()(this conjures the no-arg constructor of parent class) as the primary assertion in the constructor of youngster class. 


How about we see a guide to comprehend what I have clarified previously: 


class Parentclass 



Parentclass(){ 


System.out.println("Constructor of parent class"); 




class Subclass expands Parentclass 



Subclass(){ 


/* Compile certainly adds super() here as the 


* first proclamation of this constructor. 


*/ 


System.out.println("Constructor of kid class"); 



Subclass(int num){ 


/* Even however it is a defined constructor. 


* The compiler actually adds the no-arg super() here 


*/ 


System.out.println("arg constructor of youngster class"); 



void display(){ 


System.out.println("Hello!"); 



public static void main(String args[]){ 


/* Creating object utilizing default constructor. This 


* will summon kid class constructor, which will 


* conjure parent class constructor 


*/ 


Subclass obj= new Subclass(); 


/Calling sub class strategy 


obj.display(); 


/* Creating second item utilizing arg constructor 


* it will conjure arg constructor of youngster class which will 


* conjure no-arg constructor of parent class naturally 


*/ 


Subclass obj2= new Subclass(10); 


obj2.display(); 




Yield: 


Constructor of parent class 


Constructor of kid class 


Hi! 


Constructor of parent class 


arg constructor of kid class 


Hi! 


Defined super() call to conjure defined constructor of parent class 


We can call super() expressly in the constructor of youngster class, however it would not bode well since it would be excess. It resembles unequivocally accomplishing something which would be certainly done something else. 


Anyway when we have a constructor in parent class that takes contentions then we can utilize defined super, as super(100); to conjure defined constructor of parent class from the constructor of kid class. 


We should see a guide to get this: 


class Parentclass 



/no-arg constructor 


Parentclass(){ 


System.out.println("no-arg constructor of parent class"); 



/arg or defined constructor 


Parentclass(String str){ 


System.out.println("parameterized constructor of parent class"); 




class Subclass expands Parentclass 



Subclass(){ 


/* super() should be added to the main assertion of constructor 


* else you will get an aggregation blunder. Another significant 


* highlight note is that when we expressly utilize very in constructor 


* the compiler doesn't conjure the parent constructor naturally. 


*/ 


super("Hahaha"); 


System.out.println("Constructor of kid class"); 



void display(){ 


System.out.println("Hello"); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.display(); 




Yield: 


defined constructor of parent class 


Constructor of youngster class 


Hi 


There are not many significant focuses to note in this model: 


1) super()(or defined very should be the primary assertion in constructor else you will get the accumulation mistake: "Constructor call should be the principal articulation in a constructor" 


2) When we expressly positioned very in the constructor, the java compiler didn't consider the default no-arg constructor of parent class. 


3) How to utilize super watchword if there should be an occurrence of strategy abrogating 


At the point when a kid class proclaims an equivalent technique which is as of now present in the parent class then this is called strategy abrogating. We will learn technique abrogating in the following instructional exercises of this arrangement. For the time being you simply need to recollect this: When a kid class supersedes a strategy for parent class, at that point the call to the technique from kid class object consistently call the youngster class variant of the technique. Anyway by utilizing super catchphrase like this: super.method_name you can call the technique for parent class (the strategy which is superseded). If there should arise an occurrence of technique abrogating, these phrasings are utilized: Overridden strategy: The strategy for parent class Overriding technique: The technique for youngster class Lets take a guide to comprehend this idea: 


class Parentclass 



/Overridden strategy 


void display(){ 


System.out.println("Parent class strategy"); 




class Subclass expands Parentclass 



/Overriding strategy 


void display(){ 


System.out.println("Child class strategy"); 



void printMsg(){ 


/This would call Overriding strategy 


show(); 


/This would call Overridden strategy 


super.display(); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printMsg(); 




Yield: 


Youngster class technique 


Parent class technique 


Consider the possibility that the youngster class isn't abrogating any technique: No need of super 


At the point when kid class doesn't supersede the parent class technique then we don't have to utilize the super catchphrase to call the parent class strategy. This is on the grounds that for this situation we have just a single adaptation of every strategy and youngster class approaches the parent class techniques so we can straightforwardly call the strategies for parent class without utilizing super. 


class Parentclass 



void display(){ 


System.out.println("Parent class strategy"); 




class Subclass expands Parentclass 



void printMsg(){ 


/* This would call strategy for parent class, 


* no compelling reason to utilize super catchphrase in light of the fact that no other 


* technique with a similar name is available in this class 


*/ 


show(); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printMsg(); 




Yield: 


Pa1) How to utilize super catchphrase to get to the factors of parent class 


At the point when you have a variable in kid class which is as of now present in the parent class then to get to the variable of parent class, you need to utilize the super catchphrase. 


Lets take a guide to get this: In the accompanying system, we have an information part num pronounced in the kid class, the part with a similar name is as of now present in the parent class. Its absolutely impossible you can get to the num variable of parent class without utilizing super catchphrase. . 


/Parent class or Superclass or base class 


class Superclass 



int num = 100; 



/Child class or subclass or inferred class 


class Subclass expands Superclass 



/* a similar variable num is pronounced in the Subclass 


* which is now present in the Superclass 


*/ 


int num = 110; 


void printNumber(){ 


System.out.println(num); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printNumber(); 




Yield: 


110 


Getting to the num variable of parent class: 


By calling a variable like this, we can get to the variable of parent class if both the classes (parent and kid) have same variable. 


super.variable_name 


How about we take the very model that we have seen over, this time on paper explanation we are passing super.num rather than num. 


class Superclass 



int num = 100; 



class Subclass broadens Superclass 



int num = 110; 


void printNumber(){ 


/* Note that as opposed to composing num we are 


* composing super.num in the print explanation 


* this alludes to the num variable of Superclass 


*/ 


System.out.println(super.num); 



public static void main(String args[]){ 


Subclass obj= new Subclass(); 


obj.printNumber(); 




Yield: 


100 


As you can see by utilizing super.num we got to the num variable of parent class. 


2) Use of super catchphrase to conjure constructor of parent class 


At the point when we make the object of sub class, the new catchphrase conjures the constructor of kid class, which certainly summons the constructor of parent class. So the request to execution when we make the object of kid class is: parent class constructor is executed first and afterward the kid class constructor is executed. It happens on the grounds that compiler itself adds super()(this conjures the no-arg constructor of parent class) as the primary assertion in the constructor of youngster class. 


How about we see a guide to comprehend what I have clarified previously: 


class Parentclass 



Parentclass(){ 


System.out.println("Constructor of parent class"); 




class Subclass expands Parentclass 



Subclass(){ 


/* Compile certainly adds super() here as the 


* first proclamation of this constructor. 


*/ 


System.out.println("Constructor of kid class"); 



Subclass(int num){ 


/* Even however it is a defined constructor. 


* The compiler actually adds the no-arg super() here 


*/ 


System.out.println("arg constructor of youngster class"); 



void display(){ 


System.out.println("Hello!"); 



public static void main(String args[]){ 


/* Creating object utilizing default constructor. This 


* will summon kid class constructor, which will 


* conjure parent class constructor 


*/ 


Subclass obj= new Subclass(); 


/Calling sub class strategy 


obj.display(); 


/* Creating second item utilizing arg constructor 


* it will conjure arg constructor of youngster class which will 

* conjure no-arg constructor of parent class naturally 

*/ 

Subclass obj2= new Subclass(10); 

obj2.display(); 

Output: 

Constructor of parent class 

Constructor of kid class 

Hi! 

Constructor of parent class 

arg constructor of kid class 

Hi! 

Defined super() call to conjure defined constructor of parent class 


We can call super() expressly in the constructor of youngster class, however it would not bode well since it would be excess. It resembles unequivocally accomplishing something which would be certainly done something else. 

Anyway when we have a constructor in parent class that takes contentions then we can utilize defined super, as super(100); to conjure defined constructor of parent class from the constructor of kid class. 

We should see a guide to get this: 

class Parentclass 

/no-arg constructor 

Parentclass(){ 

System.out.println("no-arg constructor of parent class"); 


/arg or defined constructor 

Parentclass(String str){ 

System.out.println("parameterized constructor of parent class"); 

class Subclass expands Parentclass 

Subclass(){ 

/* super() should be added to the main assertion of constructor 

* else you will get an aggregation blunder. Another significant 

* highlight note is that when we expressly utilize very in constructor 

* the compiler doesn't conjure the parent constructor naturally. 


*/ 


super("Hahaha"); 

System.out.println("Constructor of kid class"); 

void display(){ 

System.out.println("Hello"); 

}

public static void main(String args[]){ 

Subclass obj= new Subclass(); 

obj.display(); 


Yield: 

defined constructor of parent class 

Constructor of youngster class 

Hi 


There are not many significant focuses to note in this model: 


1) super()(or defined very should be the primary assertion in constructor else you will get the accumulation mistake: "Constructor call should be the principal articulation in a constructor" 


2) When we expressly positioned very in the constructor, the java compiler didn't consider the default no-arg constructor of parent class. 


3) How to utilize super watchword if there should be an occurrence of strategy abrogating 


At the point when a kid class proclaims an equivalent technique which is as of now present in the parent class then this is called strategy abrogating. We will learn technique abrogating in the following instructional exercises of this arrangement. For the time being you simply need to recollect this: When a kid class supersedes a strategy for parent class, at that point the call to the technique from kid class object consistently call the youngster class variant of the technique. Anyway by utilizing super catchphrase like this: super.method_name you can call the technique for parent class (the strategy which is superseded). If there should arise an occurrence of technique abrogating, these phrasings are utilized: Overridden strategy: The strategy for parent class Overriding technique: The technique for youngster class Lets take a guide to comprehend this idea: 

class Parentclass 

/Overridden strategy 

void display(){ 

System.out.println("Parent class strategy"); 

class Subclass expands Parentclass 

/Overriding strategy 

void display(){ 

System.out.println("Child class strategy"); 

void printMsg(){ 

/This would call Overriding strategy 

show(); 

/This would call Overridden strategy 

super.display(); 

public static void main(String args[]){ 

Subclass obj= new Subclass(); 

obj.printMsg(); 

}

Yield: 

Youngster class technique 

Parent class technique 

Consider the possibility that the youngster class isn't abrogating any technique: No need of super 


At the point when kid class doesn't supersede the parent class technique then we don't have to utilize the super catchphrase to call the parent class strategy. This is on the grounds that for this situation we have just a single adaptation of every strategy and youngster class approaches the parent class techniques so we can straightforwardly call the strategies for parent class without utilizing super. 

class Parentclass 

void display(){ 

System.out.println("Parent class strategy"); 

class Subclass expands Parentclass 

void printMsg(){ 

/* This would call strategy for parent class, 

* no compelling reason to utilize super catchphrase in light of the fact that no other 

* technique with a similar name is available in this class 

*/ 

show(); 

public static void main(String args[]){ 

Subclass obj= new Subclass(); 

obj.printMsg(); 


Yield: 

Parent class strategyrent class strategy

0 Comments:

Post a Comment

C language and C++ progaming