Follow us on Facebook

Header Ads

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Welcome to JAVA POint

Static and Dynamic building in JAVA

                             Static and Dynamic building in JAVA


Relationship of strategy call to the technique body is known as restricting. There are two kinds of restricting: Static Binding that occurs at arrange time and Dynamic Binding that occurs at runtime. Before I clarify static and dynamic restricting in java, lets see not many terms that will assist you with understanding this idea better. 



What is reference and article? 


class Human{ 


.... 


} 


class Boy expands Human{ 


public static void primary( String args[]) { 


/*This articulation essentially makes an object of class 


*Boy and doles out a reference of Boy to it*/ 


Kid obj1 = new Boy(); 


/* Since Boy expands Human class. The article creation 


* should be possible thusly. Parent class reference 


* can have kid class reference appointed to it 


*/ 


Human obj2 = new Boy(); 

} 

} 


Static and Dynamic Binding in Java 


As referenced above, relationship of strategy definition to the technique call is known as restricting. There are two kinds of restricting: Static restricting and dynamic restricting. Lets examine them. 


Static Binding or Early Binding 


The limiting which can be settled at arrange time by compiler is known as static or early restricting. The limiting of static, private and last strategies is assemble time. Why? The explanation is that the these strategy can't be superseded and the kind of the class is resolved at the assemble time. Lets see a guide to get this: 


Static restricting model 


Here we have two classes Human and Boy. Both the classes have same strategy walk() however the technique is static, which implies it can't be overriden so despite the fact that I have utilized the object of Boy class while making object obj, the parent class strategy is called by it. Since the reference is of Human kind (parent class). So at whatever point a limiting of static, private and last techniques occur, kind of the class is dictated by the compiler at assemble time and the limiting happens without further ado. 

class Human{ 

public static void walk() 

{ 

System.out.println("Human strolls"); 

} 

} 

class Boy expands Human{ 

public static void walk(){ 

System.out.println("Boy strolls"); 

} 


public static void primary( String args[]) { 

/* Reference is of Human sort and item is 


* Boy type 


*/ 

Human obj = new Boy(); 

/* Reference is of HUman kind and article is 

* of Human sort. 

*/ 

Human obj2 = new Human(); 

obj.walk(); 

obj2.walk();

} 

} 

Yield: 

Human strolls 

Human strolls 

Dynamic Binding or Late Binding 


At the point when compiler can't resolve the call/restricting at incorporate time, such restricting is known as Dynamic or late Binding. Technique Overriding is an ideal illustration of dynamic restricting as in abrogating both parent and youngster classes have same strategy and for this situation the kind of the article figures out which strategy is to be executed. The kind of article is resolved at the run time so this is known as powerful restricting. 

Dynamic restricting model 


This is the very model that we have seen previously. The lone contrast here is that in this model, abrogating is really occurring since these strategies are not static, private and last. If there should arise an occurrence of superseding the call to the overriden technique is resolved at runtime by the sort of article along these lines late restricting occurs. Lets see a guide to get this: 

class Human{ 

/Overridden Method 

public void walk() 

{ 

System.out.println("Human strolls"); 

} 

} 

class Demo expands Human{ 

/Overriding Method 

public void walk(){ 

System.out.println("Boy strolls"); 

} 

public static void primary( String args[]) { 

/* Reference is of Human sort and item is 


* Boy type 


*/ 


Human obj = new Demo(); 


/* Reference is of HUman sort and item is 


* of Human sort. 


*/ 


Human obj2 = new Human(); 


obj.walk(); 


obj2.walk(); 


} 


} 


Yield: 


Kid strolls 


Human strolls 


As you can see that the yield is unique in relation to what we found in the static restricting model, in light of the fact that for this situation while formation of article obj the sort of the item is resolved as a Boy type so strategy for Boy class is called. Recollect the kind of the item is resolved at the runtime. 


Static Binding versus Dynamic Binding 


Lets talk about the contrast among static and dynamic restricting in Java. 


Static restricting occurs at arrange time while dynamic restricting occurs at runtime. 


Restricting of private, static and last techniques consistently occur at assemble time since these strategies can't be superseded. At the point when the technique abrogating is really occurring and the reference of parent type is appointed to the object of youngster class type at that point such restricting is settled during runtime. 


The limiting of over-burden techniques is static and the limiting of abrogated strategies is dynamic.

Types of polymorphism in java-Runtime and compile time polymorphism.

            Types of polymorphism in java-Runtime and compile time polymorphism.


In the last instructional exercise we talked about Polymorphism in Java. In this guide we will see sorts of polymorphism. There are two kinds of polymorphism in java: 

1) Static Polymorphism otherwise called gather time polymorphism 

2) Dynamic Polymorphism otherwise called runtime polymorphism

Compile time Polymorphism (or Static polymorphism) 

Polymorphism that is settled during compiler time is known as static polymorphism. Technique over-burdening is an illustration of aggregate time polymorphism. 



Technique Overloading: This permits us to have more than one strategy having a similar name, if the boundaries of strategies are distinctive in number, arrangement and information sorts of boundaries. We have effectively talked about Method over-burdening here: If you didn't peruse that direct, allude: Method Overloading in Java 

Illustration of static Polymorphism 

Strategy over-burdening is one of the manner in which java upholds static polymorphism. Here we have two meanings of a similar strategy add() which add technique would be called is dictated by the boundary list at the aggregate time. That is the explanation this is otherwise called order time polymorphism. 

class SimpleCalculator 

{ 

int add(int a, int b) 

{ 

return a+b; 

} 

int add(int a, int b, int c) 

{ 

return a+b+c; 

} 

} 

public class Demo 

{ 

public static void main(String args[]) 

{ 

SimpleCalculator obj = new SimpleCalculator(); 

System.out.println(obj.add(10, 20)); 

System.out.println(obj.add(10, 20, 30)); 

} 

} 


Ouput: 

30 

60 


Runtime Polymorphism (or Dynamic polymorphism) 

It is otherwise called Dynamic Method Dispatch. Dynamic polymorphism is an interaction where a call to an abrogated technique is settled at runtime, that is the reason it is called runtime polymorphism. I have effectively talked about strategy abrogating in detail in a different instructional exercise, allude it: Method Overriding in Java. 

Model :

In this model we have two classes ABC and XYZ. ABC is a parent class and XYZ is a kid class. The kid class is superseding the strategy myMethod() of parent class. In this model we have kid class object doled out to the parent class reference so to figure out which strategy would be called, the sort of the article would be resolved at run-time. It is the kind of article that figures out which form of the technique would be called (not the sort of reference). 

To cmprehend the idea of abrogating, you ought to have the fundamental information on legacy in Java. 

class ABC{ 

public void myMethod(){ 

System.out.println("Overridden Method"); 

} 

} 

public class XYZ expands ABC{ 

public void myMethod(){ 

System.out.println("Overriding Method"); 

} 

public static void main(String args[]){ 

ABC obj = new XYZ(); 

obj.myMethod(); 

} 

} 


Out: 

Superseding Method 


At the point when a superseded technique is called through a reference of parent class, at that point sort of the item figures out which strategy is to be executed. Along these lines, this assurance is made at run time. 

Since both the classes, kid class and parent class have a similar strategy animalSound. Which form of the method(child class or parent class) will be called is resolved at runtime by JVM. 

Barely any additional superseding models: 

ABC obj = new ABC(); 

obj.myMethod(); 

/This would call the myMethod() of parent class ABC 

XYZ obj = new XYZ(); 

obj.myMethod(); 

/This would call the myMethod() of youngster class XYZ 

ABC obj = new XYZ(); 

obj.myMethod(); 

/This would call the myMethod() of youngster class XYZ 

In the third case the strategy for youngster class is to be executed in light of the fact that which technique is to be executed is controlled by the kind of item and since the article has a place with the kid class, the kid class rendition of myMethod() is called.

Polymorphism in Java With Example.

                                 Polymorphism in Java With Example.

Polymorphism is one of the OOPs highlight that permits us to play out a solitary activity in an unexpected way. For instance, lets say we have a class Animal that has a strategy sound(). Since this is a conventional class so we can't give it an execution like: Roar, Meow, Oink and so forth We needed to give a nonexclusive message. 



public class Animal { 

... 

public void sound(){ 

System.out.println("Animal is making a sound"); 

} 

} 

Presently lets say we two subclasses of Animal class: Horse and Cat that expands (see Inheritance) Animal class. We can give the execution to a similar strategy like this: 


public class Horse broadens Animal{ 

... 

@Override 

public void sound(){ 

System.out.println("Neigh"); 

} 

} 

what's more, 

public class Cat expands Animal{ 

... 

@Override 

public void sound(){ 

System.out.println("Meow"); 

} 

} 

As you can see that in spite of the fact that we had the regular activity for all subclasses sound() yet there were various approaches to do a similar activity. This is an ideal illustration of polymorphism (highlight that permits us to play out a solitary activity in an unexpected way). It would not bode well to simply call the nonexclusive sound() strategy as every Animal has an alternate sound. Along these lines we can say that the activity this technique performs depends on the kind of item.


                                What is polymorphism in programming? 

Polymorphism is the capacity of a technique to do various things dependent on the article that it is following up on. As such, polymorphism permits you characterize one interface and have numerous executions. As we have found in the above model that we have characterized the technique sound() and have the numerous executions of it in the diverse 2 sub classes. 

Which sound() strategy will be called is resolved at runtime so the model we gave above is a runtime polymorphism model. 

Sorts of polymorphism and technique over-burdening and abrogating are shrouded in the different instructional exercises. You can allude them here: 

1. Technique Overloading in Java – This is an illustration of aggregate time (or static polymorphism).

2. Technique Overriding in Java – This is an illustration of runtime time (or dynamic polymorphism) 


3. Kinds of Polymorphism – Runtime and assemble time – This is our next instructional exercise where we have covered the sorts of polymorphism in detail. I would prescribe you to go however strategy over-burdening and abrogating prior to going however this subject. 

Lets record its total code:

Model 1: Polymorphism in Java 

Runtime Polymorphism model: 

Animal.java 

public class Animal{ 

public void sound(){ 

System.out.println("Animal is making a sound"); 

} 

} 

Horse.java 

class Horse broadens Animal{ 

@Override 

public void sound(){ 

System.out.println("Neigh"); 

} 

public static void main(String args[]){ 

Creature obj = new Horse(); 

obj.sound(); 

} 

} 

Yield: 

Neigh 

Cat.java 

public class Cat broadens Animal{ 

@Override 

public void sound(){ 

System.out.println("Meow"); 

} 

public static void main(String args[]){ 

Creature obj = new Cat(); 

obj.sound(); 

} 

} 


Yield: 

Howl 

Model 2: Compile time Polymorphism 

Technique Overloading then again is an arrange time polymorphism model. 

class Overload 

{ 

void demo (int a) 

{ 

System.out.println ("a: " + a); 

} 

void demo (int a, int b) 

{ 

System.out.println ("an and b: " + a + "," + b); 

} 

twofold demo(double a) { 

System.out.println("double a: " + a); 

return a*a; 

} 

} 

class MethodOverloading 

{ 

public static void fundamental (String args []) 

{ 

Over-burden Obj = new Overload(); 

twofold outcome; 

Obj .demo(10); 

Obj .demo(10, 20); 

result = Obj .demo(5.5); 

System.out.println("O/P : " + result); 

} 

} 


Here the technique demo() is over-burden multiple times: first strategy has 1 int boundary, second strategy has 2 int boundaries and third one is having twofold boundary. Which technique is to be called is dictated by the contentions we pass while calling strategies. This occurs at runtime arrange time so this sort of polymorphism is known as aggregate time polymorphism. 


Output: 

a: 10 

an and b: 10,20 

twofold a: 5.5 

O/P : 30.25

Difference Between Method Overloading and overriding in Java(in java)

                     Difference Between Method Overloading and overriding in Java

                                Overloading Vs Overriding in JAVA


1: Over-burdening occurs at order time while Overriding occurs at runtime: The limiting of over-burden strategy call to its definition has occurs at accumulate time anyway restricting of superseded technique call to its definition occurs at runtime. 


2: Static techniques can be over-burden which implies a class can have more than one static strategy for same name. Static techniques can't be superseded, regardless of whether you proclaim an equivalent static strategy in youngster class it has nothing to do with a similar technique for parent class. 


3: The most fundamental contrast is that over-burdening is being done in a similar class while for superseding base and youngster classes are required. Superseding is tied in with giving a particular execution to the acquired technique for parent class. 


4: Static restricting is being utilized for over-burden techniques and dynamic restricting is being utilized for abrogated/superseding strategies. 


5: Execution: Overloading gives better execution contrasted with abrogating. The explanation is that the limiting of abrogated techniques is being done at runtime. 


6: private and last techniques can be over-burden yet they can't be superseded. It implies a class can have more than one private/last strategies for same name yet a youngster class can't supersede the private/last techniques for their base class. 


7: Return kind of strategy doesn't make any difference if there should arise an occurrence of technique over-burdening, it very well may be same or extraordinary. Anyway if there should arise an occurrence of strategy abrogating the superseding technique can have more explicit return type (allude this). 


8: Contention rundown ought to appear as something else while doing strategy over-burdening. Contention rundown ought to be same in technique Overriding.



Overloading Example:

/A class for adding upto 5 numbers 


class Sum 

{ 

int add(int n1, int n2) 

{ 

return n1+n2; 

} 

int add(int n1, int n2, int n3) 

{ 

return n1+n2+n3; 

} 

int add(int n1, int n2, int n3, int n4) 

{ 

return n1+n2+n3+n4; 

} 

int add(int n1, int n2, int n3, int n4, int n5) 

{ 

return n1+n2+n3+n4+n5; 

} 

public static void main(String args[]) 

{ 

Total obj = new Sum(); 

System.out.println("Sum of two numbers: "+obj.add(20, 21)); 

System.out.println("Sum of three numbers: "+obj.add(20, 21, 22)); 

System.out.println("Sum of four numbers: "+obj.add(20, 21, 22, 23)); 

System.out.println("Sum of five numbers: "+obj.add(20, 21, 22, 23, 24)); 

} 

} 


Output: 

Amount of two numbers: 41 

Amount of three numbers: 63 

Amount of four numbers: 86 

Amount of five numbers: 110 

Here we have 4 renditions of same technique add. We are over-burdening the technique add() here. 


Superseding model 


bundle beginners

class CarClass 

{ 

public int speedLimit() 

{ 

bring 100 back; 

} 

} 

class Ford expands CarClass 

{ 

public int speedLimit() 

{ 

return 150; 

} 

public static void main(String args[]) 

{ 

CarClass obj = new Ford(); 

int num= obj.speedLimit(); 

System.out.println("Speed Limit is: "+num); 

} 

} 

Output: 

Speed Limit is: 150 

Here speedLimit() strategy for class Ford is superseding the speedLimit() technique for class CarClass.


Method Overriding With Example(JAVA)

                                         Method Overriding With Example

Proclaiming a technique in sub class which is as of now present in parent class is known as strategy superseding. Abrogating is done so a youngster class can give its own execution to a strategy which is now given by the parent class. For this situation the strategy in parent class is called abrogated technique and the strategy in youngster class is called superseding strategy. In this guide, we will perceive what is technique abrogating in Java and why we use it. 



Strategy Overriding Example 


Lets take a basic guide to get this. We have two classes: A youngster class Boy and a parent class Human. The Boy class expands Human class. Both the classes have a typical technique void eat(). Kid class is giving its own execution to the eat() strategy or as such it is superseding the eat() technique. 


The motivation behind Method Overriding is clear here. Kid class needs to give its own execution so when it calls this strategy, it prints Boy is eating rather than Human is eating. 


class Human{ 


/Overridden technique 


public void eat() 


{ 


System.out.println("Human is eating"); 


} 


} 


class Boy broadens Human{ 


/Overriding technique 


public void eat(){ 


System.out.println("Boy is eating"); 


} 


public static void principle( String args[]) { 


Kid obj = new Boy(); 


/This will call the kid class variant of eat() 


obj.eat(); 


} 


} 


Yield: 


Kid is eating 


Benefit of technique superseding 


The fundamental benefit of technique abrogating is that the class can give its own particular execution to an acquired strategy without altering the parent class code. 


This is useful when a class has a few kid classes, so if a kid class needs to utilize the parent class technique, it can utilize it and different classes that need to have diverse execution can utilize superseding highlight to make changes without contacting the parent class code. 

Strategy Overriding and Dynamic Method Dispatch 


Strategy Overriding is an illustration of runtime polymorphism. At the point when a parent class reference focuses to the kid class object then the call to the abrogated technique is resolved at runtime, on the grounds that during strategy call which method(parent class or youngster class) is to be executed is controlled by the kind of item. This interaction wherein call to the superseded technique is settled at runtime is known as unique strategy dispatch. Lets see a guide to get this: 

class ABC{ 

/Overridden technique 

public void disp() 

{ 

System.out.println("disp() strategy for parent class"); 

}

} 

class Demo expands ABC{ 

/Overriding strategy 

public void disp(){ 

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

} 

public void newMethod(){ 

System.out.println("new strategy for kid class"); 

} 

public static void primary( String args[]) { 

/* When Parent class reference alludes to the parent class object 

* at that point for this situation superseded strategy (the technique for parent class) 

* is called. 

*/ 

ABC obj = new ABC(); 

obj.disp(); 

/* When parent class reference alludes to the kid class object 

* at that point the superseding strategy (technique for youngster class) is called. 

* This is called dynamic technique dispatch and runtime polymorphism 

*/ 

ABC obj2 = new Demo(); 

obj2.disp(); 

} 

} 


Yield: 


disp() strategy for parent class 


disp() strategy for Child class 


In the above model the call to the disp() technique utilizing second article (obj2) is runtime polymorphism (or dynamic strategy dispatch). 


Note: In powerful strategy dispatch the item can call the abrogating strategies for kid class and all the non-superseded techniques for base class however it can't call the strategies which are recently proclaimed in the kid class. In the above model the item obj2 is calling the disp(). Be that as it may in the event that you attempt to call the newMethod() technique (which has been recently proclaimed in Demo class) utilizing obj2 then you would give assemblage mistake with the accompanying message: 

Exemption in string "principle" java.lang.Error: Unresolved assemblage 

issue: The technique xyz() is unclear for the kind ABC 

Rules of strategy superseding in Java 


Contention list: The contention rundown of superseding technique (strategy for kid class) should coordinate with the Overridden method(the strategy for parent class). The information sorts of the contentions and their succession ought to precisely coordinate. 


Access Modifier of the abrogating technique (strategy for subclass) can't be more prohibitive than the superseded technique for parent class. For example on the off chance that the Access Modifier of parent class strategy is public, the superseding technique (kid class technique ) can't have private, ensured and default Access modifier,because these three access modifiers are more prohibitive than public. 


For example This isn't permitted as kid class disp strategy is more restrictive(protected) than base class(public) 

class MyBaseClass{ 

public void disp() 

{ 


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

} 

} 


class MyChildClass expands MyBaseClass{ 

secured void disp(){ 

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

} 

public static void primary( String args[]) { 

MyChildClass obj = new MyChildClass(); 

obj.disp(); 

} 

} 


Yield: 

Special case in string "primary" java.lang.Error: Unresolved aggregation 

issue: Cannot lessen the perceivability of the acquired strategy from MyBaseClass 

Anyway this is totally legitimate situation as open is less prohibitive than secured. Same access modifier is additionally a legitimate one. 

class MyBaseClass{ 

ensured void disp() 

{ 

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

} 

} 

class MyChildClass broadens MyBaseClass{ 

public void disp(){ 

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

} 

public static void primary( String args[]) { 

MyChildClass obj = new MyChildClass(); 

obj.disp(); 

} 

} 


Output: 


Kid class strategy 


private, static and last strategies can't be superseded as they are nearby to the class. Anyway static strategies can be re-pronounced in the sub class, for this situation the sub-class strategy would act contrastingly and will have nothing to do with a similar static technique for parent class. 


Superseding strategy (technique for youngster class) can toss unchecked special cases, whether or not the abrogated method(method of parent class) tosses any exemption or not. Anyway the superseding technique ought not toss checked special cases that are new or more extensive than the ones announced by the abrogated strategy. We will talk about this in detail with model in the impending instructional exercise. 

Restricting of abrogated strategies occur at runtime which is known as unique restricting. 

Assuming a class is broadening a theoretical class or executing an interface, it needs to abrogate every one of the theoretical strategies except if the actual class is a theoretical class. 

Super catchphrase in Method Overriding 


The super catchphrase is utilized for calling the parent class strategy/constructor. super.myMethod() calls the myMethod() technique for base class while super() calls the constructor of base class. How about we see the utilization of very in technique Overriding. 

As we realize that we abrogate a technique in youngster class, at that point call to the strategy utilizing kid class object calls the superseded technique. By utilizng super we can call the superseded technique as demonstrated in the model underneath: 

class ABC{ 

public void myMethod() 

{ 

System.out.println("Overridden technique"); 

} 

} 

class Demo broadens ABC{ 

public void myMethod(){ 

/This will call the myMethod() of parent class 

super.myMethod(); 

System.out.println("Overriding technique"); 

} 

public static void fundamental( String args[]) { 

Demo obj = new Demo(); 

obj.myMethod(); 

} 

} 

Yield: 

Class ABC: mymethod() 

Class Test: mymethod() 

As you see utilizing super catchphrase, we can get to the overriden technique.

Method Overloading in Java With Examples(JAVA)

                                 Method  Overloading  in Java  With Examples

Strategy Overloading is a component that permits a class to have more than one technique having a similar name, if their contention records are extraordinary. It is like constructor over-burdening in Java, that permits a class to have more than one constructor having distinctive contention records. 

we should return direct, when I say contention show it implies the boundaries that a technique has: For instance the contention rundown of a strategy add(int a, int b) having two boundaries is not the same as the contention rundown of the technique add(int a, int b, int c) having three boundaries.



Three Ways to Method Overload:

To over-burden a strategy, the contention arrangements of the techniques should contrast in both of these: 


1. Number of boundaries. 

For instance: This is a substantial instance of over-burdening 

add(int, int) 

add(int, int, int) 

2. Information kind of boundaries. 

For instance: 

add(int, int) 

add(int, glide) 

3. Grouping of Data kind of boundaries. 


For instance: 

add(int, glide) 

add(float, int) 

Invalid instance of technique over-burdening: 


At the point when I say contention list, I am not discussing return kind of the strategy, for instance if two techniques have same name, same boundaries and have distinctive return type, at that point this is certifiably not a substantial strategy over-burdening model. This will toss accumulation blunder. 


int add(int, int) 

skim add(int, int) 

Technique over-burdening is an illustration of Static Polymorphism. We will examine polymorphism and kinds of it in a different instructional exercise.

Method overLoading Example

As examined in the start of thiss guide, strategy over-burdening is finished by announcing same technique with various boundaries. The boundaries should be diverse in both of these: number, grouping or sorts of boundaries (or contentions). Lets see instances of every one of these cases. 

Contention list is otherwise called boundary list

Example 1: Overloading- Differen numbers Parameters in Argument List  

This Example of Show how method Overloading is done by having different Number of Parametrs

class DisplayOverloading 

{ 

public void disp(char c) 

{ 

System.out.println(c); 

} 

public void disp(char c, int num) 

{ 

System.out.println(c + " "+num); 

} 

} 

class Sample 

{ 

public static void main(String args[]) 

{ 

DisplayOverloading obj = new DisplayOverloading(); 

obj.disp('a'); 

obj.disp('a',10); 

} 

} 

Output: 

a 

a 10 

In the above model – technique disp() is over-burden dependent on the quantity of boundaries – We have two strategies with the name disp however the boundaries they have are extraordinary. Both are having diverse number of boundaries.

Model 2: Overloading – Difference in information kind of boundaries 


In this model, strategy disp() is over-burden dependent on the information kind of boundaries – We have two techniques with the name disp(), one with boundary of singe type and another technique with the boundary of int type. 

class DisplayOverloading2 

{ 

public void disp(char c) 

{ 

System.out.println(c); 

} 

public void disp(int c) 

{ 

System.out.println(c ); 

} 

} 

class Sample2 

{ 

public static void main(String args[]) 

{ 


DisplayOverloading2 obj = new DisplayOverloading2(); 

obj.disp('a'); 

obj.disp(5); 

} 

} 


Yield: 

a 

5 


Example3: Overloading – Sequence of information kind of contentions 


Here technique disp() is over-burden dependent on grouping of information sort of boundaries – Both the strategies have distinctive arrangement of information type in contention list. First strategy is having contention list as (burn, int) and second is having (int, singe). Since the arrangement is unique, the strategy can be over-burden with no issues. 

class DisplayOverloading3 

{ 

public void disp(char c, int num) 

{ 

System.out.println("I'm the primary meaning of technique disp"); 

} 

public void disp(int num, singe c) 

{ 

System.out.println("I'm the second meaning of technique disp" ); 

} 

} 

class Sample3 

{ 

public static void main(String args[]) 

{ 

DisplayOverloading3 obj = new DisplayOverloading3(); 

obj.disp('x', 51 ); 

obj.disp(52, 'y'); 

} 

} 

Yield: 

I'm the main meaning of strategy disp 

I'm the second meaning of strategy disp 

Strategy Overloading and Type Promotion 


At the point when an information sort of more modest size is elevated to the information kind of greater size than this is called type advancement, for instance: byte information type can be elevated to short, a short information type can be elevated to int, long, twofold and so on 

What it has to do with strategy over-burdening? 

Indeed, it is vital to comprehend type advancement else you will believe that the program will toss gathering mistake however truth be told that program will run fine due to type advancement. 

Lets take a guide to perceive what I am talking here: 

class Demo{ 

void disp(int a, twofold b){ 

System.out.println("Method A"); 

} 

void disp(int a, twofold b, twofold c){ 

System.out.println("Method B"); 

} 

public static void main(String args[]){ 

Demo obj = new Demo(); 


/* I am passing buoy esteem as a subsequent contention yet 

* it got elevated to the sort twofold, in light of the fact that there 

* wasn't any technique having arg list as (int, skim)

*/ 

obj.disp(100, 20.67f); 

} 

} 


Yield: 


Technique A 


As you can see that I have passed the buoy esteem while calling the disp() strategy however it got elevated to the twofold sort as there wasn't any technique with contention list as (int, glide) 

Yet, this sort advancement doesn't generally occur, lets see another model: 

class Demo{ 

void disp(int a, twofold b){ 

System.out.println("Method A"); 

} 

void disp(int a, twofold b, twofold c){ 

System.out.println("Method B"); 

} 

void disp(int a, glide b){ 

System.out.println("Method C"); 

} 

public static void main(String args[]){ 

Demo obj = new Demo(); 


/* This time advancement will not occur as there is 


* a technique with arg list as (int, drift) 

*/ 

obj.disp(100, 20.67f); 

} 

} 


Yield: 

Technique C 


As you see that this time type advancement didn't occur in light of the fact that there was a technique with coordinating with contention type. 

Type Promotion table: 

The information type on the left side can be elevated to the any of the information type present in the correct side of it. 

byte → short → int → long 

short → int → long 

int → long → skim → twofold 

glide → twofold 

long → glide → twofold 

Lets see not many Valid/invalid instances of technique over-burdening 

Case 1: 

inft mymethod(int a, int b, glide c)


int mymethod(int var1, int var2, glide var3) 

Result: Compile time mistake. Contention records are by and large same. The two techniques are having same number, information types and same arrangement of information types. 

Case 2: 

int mymethod(int a, int b) 

int mymethod(float var1, skim var2) 

Result: Perfectly fine. Substantial instance of over-burdening. Here information sorts of contentions are extraordinary. 

Case 3: 

int mymethod(int a, int b) 

int mymethod(int num) 

Result: Perfectly fine. Substantial instance of over-burdening. Here number of contentions are unique. 

Case 4: 

drift mymethod(int a, skim b) 

drift mymethod(float var1, int var2) 

Result: Perfectly fine. Legitimate instance of over-burdening. Grouping of the information kinds of boundaries are extraordinary, first strategy is having (int, buoy) and second is having (drift, int). 

Case 5: 

int mymethod(int a, int b) 

glide mymethod(int var1, int var2) 

Result: Compile time mistake. Contention records are actually same. Despite the fact that return kind of techniques are unique, it's anything but a legitimate case. Since return sort of strategy doesn't make any difference while over-burdening a technique. 

Supposition the appropriate responses prior to checking it toward the finish of projects: 

Question 1 – return type, technique name and contention list same. 

class Demo 

{ 

public int myMethod(int num1, int num2) 

{ 

System.out.println("First myMethod of class Demo"); 

return num1+num2; 

} 

public int myMethod(int var1, int var2) 

{ 

System.out.println("Second myMethod of class Demo"); 

return var1-var2; 

} 

} 

class Sample4 

{ 

public static void main(String args[]) 

{ 

Demo obj1= new Demo(); 

obj1.myMethod(10,10); 

obj1.myMethod(20,12); 

} 

}

Answer: 

It will toss an aggregation mistake: More than one technique with same name and contention list can't be characterized in an equivalent class. 

Question 2 – return type is extraordinary. Technique name and contention list same. 

class Demo2 

{ 

public twofold myMethod(int num1, int num2) 

{ 

System.out.println("First myMethod of class Demo"); 

return num1+num2; 

} 

public int myMethod(int var1, int var2) 

{ 

System.out.println("Second myMethod of class Demo"); 

return var1-var2; 

} 

} 

class Sample5 

{ 

public static void main(String args[]) 

{ 

Demo2 obj2= new Demo2(); 

obj2.myMethod(10,10); 

obj2.myMethod(20,12); 

} 

}

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

C language and C++ progaming