Using super class

java super constructor
super() python
super keyword in java in hindi
super.super in java
super class in java
super and super() in java
abstract class in java
this keyword in java

We know when a base class is inherited by a derived class that is called inheritance and when a derived class contains same method name, return type and argument list as like as base class's method that is called method overriding. And we also know that The super keyword can be used to invoke base class method. It should be used if derived class contains the same method as base class. In other words, it is used if method is overridden.

But my code doesn't work perfectly, why????? Help me please..........

 public class person {
  String name = "Mohan";
  int age = 24;

  void displayInformation() {
   displayInformation();
   System.out.println("Name: " + name);
   System.out.println("Age: " + age);
  }
 }

 public class teacher extends person {

  String qualification = "B.Sc in CSE";


  @Override
  void displayInformation() {
   super.displayInformation();
   System.out.println("Name: " + name);
   System.out.println("Age: " + age);
   System.out.println("Qualification: " + qualification);
  }
 }
 public class MethodOverriding {
  public static void main(String[] args) {

   teacher t = new teacher();
   t.displayInformation();

  }
 }

Your understanding is correct. In the code of person is a mistake:

void displayInformation() {
  displayInformation();

You guide the method displayInformation call first the method displayInformation who calls first the method displayInformation who calls first the method displayInformation (...).

You will end-up in a Exception.

Remove the call of displayInformation in the class person and the Exception will go away.

Using the Keyword super (The Java™ Tutorials > Learning the Java , We could access maxSpeed of base class in subclass using super keyword. 2. Use of super with methods: This is used when we want to call parent class method. The super keyword can also be used to call functions on a parent object. Example Using super in classes. This code snippet is taken from the classes sample . Here super() is called to avoid duplicating the constructor parts' that are common between Rectangle and Square.

I'm guessing that instead of this:

@Override
void displayInformation() {
  super.displayInformation();
  System.out.println("Name: " + name);
  System.out.println("Age: " + age);
  System.out.println("Qualification: " + qualification);
}

you want:

@Override
void displayInformation() {
  super.displayInformation();
  System.out.println("Qualification: " + qualification);
}

Otherwise name and age are printed twice. Once when you call the parent-class method and then again in the child-class implementation.

Super Keyword in Java, There is no way you can access the num variable of parent class without using super keyword. . //Parent class or Superclass or base class class Superclass { int​  If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass:

public class person {
String name = "Mohan";
int age = 24`enter code here`;

void displayInformation()
{
    //displayInformation();
    System.out.println("Name: " +name);
    System.out.println("Age: " +age);
}
}

public class teacher extends person{

String qualification = "CSE";
@Override
void displayInformation()
{
    super.displayInformation();
    System.out.println("Name: " +name);
    System.out.println("Age: " +age);
    System.out.println("Qualification: " +qualification);
}
}

public class teacher extends person{

String qualification = "CSE";
@Override
void displayInformation()
{
    super.displayInformation();
    System.out.println("Name: " +name);
    System.out.println("Age: " +age);
    System.out.println("Qualification: " +qualification);
}
}

public class MethodOverriding {
    public static void main(String[] args) {

    teacher t = new teacher();
    t.displayInformation();

}
}

Super keyword in java with example, super() alone returns a temporary object of the superclass that then allows you to call that superclass's methods. Why would you want to do any of this? While the  super() returns a delegate object to a parent class, so you call the method you want directly on it: super().area(). Not only does this save us from having to rewrite the area calculations, but it also allows us to change the internal.area() logic in a single location.

Supercharge Your Classes With Python super() – Real Python, The super keyword can also be used to call functions on a parent object. Example. Using super in classes. This code snippet is taken from the  How to Use the super Keyword to Call a Base Class Constructor in Java. A derived Java class can call a constructor in its base class using the super keyword. In fact, a constructor in the derived class must call the super's constructor unless default constructors are in place for both classes.

super, Example. Using super to call the superclass of Dog (subclass): class Animal { // Superclass (parent) public void animalSound() { System.out.println("The animal  Of course, like the super and base keywords of the other languages, you can only use the using declaration in the base class with single inheritance, otherwise there is an ambiguity in the derived class. But, not unlike Bridget Jones, inheritances are single most of the time anyway (thanks to my brother-in-law Joachim for this astute closing joke).

Java super Keyword, superclass (parent) - the class being inherited from. To inherit from a class, use the extends keyword. In the example below, the Car class (subclass) inherits  super can be used to refer immediate parent class instance variable. super can be used to invoke immediate parent class method. super() can be used to invoke immediate parent class constructor. 1) super is used to refer immediate parent class instance variable. We can use super keyword to access the data member or field of parent class.

Comments
  • Welcome to SO. What behaviour do you expect? Also you may want to read stackoverflow.com/help/how-to-ask and include the code in the question with proper markup and not in an external link.
  • Learn from stackoverflow.com/help/mcve stackoverflow.com/help/how-to-ask before you ask a question
  • Funnily enough it will be a StackOverflow exception, so he's in the right place!! ;-)
  • @Rick Yea :D I thought to point that out, but if it rans into a OOME the joke wont work.
  • Thanks a lot for detecting my mistake.
  • Also note that if you didn't have super. in the subclass implementation then you'd have an infinite loop, and that would be bad (TM).
  • Lol. Totally missed that you already had an infinite loop at the higher level too. Wheee!!!