z1lent
z1lent

Reputation: 147

Java: subclass calling parent class methods

I have a parent class Shape and a subclass Rectangle, I have a method in parent class Shape called Output. How can I call the parent class method Output in the children class?

The Parent Class

public class Shape {
  public int edge;
  public Shape(int Gedge) {
    edge=Gedge;
  }
  public void Output() {
    System.out.println("This shape has "+edge+" eges");
  }
}

The Subclass:

public class Rectangle extends Shape {
  public int edge, length, width;
  public Rectangle(int Gedge, int Glength, int Gwidth) {
    super (Gedge);
    edge=Gedge;
    length=Glength;
    width=Gwidth;
  }
  public void Output1() {
    //I want to call the parent class Method "Output" here.
    System.out.println("The Area is "+length*width);
}
  public static void main (String [] args) {
    Rectangle A1=new Rectangle(4,3,5);
      A1.Output1();
}
}

If I run this code now, the output is The Area is 15, but I want to call the Output method in Shape, so ideally it prints

This shape has 4 edges

The area is 15

Help is Appreciated. Thanks

Upvotes: 0

Views: 2647

Answers (3)

NicholasFolk
NicholasFolk

Reputation: 1137

As others have already answered the question (both call super.Output() or just call Output() are correct), I will attempt to give a little more clarity as to why both are correct (and it's mostly due to your naming conventions).

When you have a parent-child relationship between classes, like so:

class A {
    public void doStuff(){
        System.out.println("Doing A Stuff!");
    }
}
class B extends A {
    public void doStuff(){
        System.out.println("Doing B Stuff!");
    }
}

What you are doing is overriding the doStuff method. The behaviour will be as follows:

A a = new B(); // A is the parent type of a, B is the actual type.
a.doStuff(); // prints "Doing B stuff!"

When you override in Java, the method has to have the exact same name, parameters list (in the same order), and return type. So in your example Output1 is NOT an override of Output. This is why you actually can get away with calling Output() anywhere in your subclass, without the need for the 'super' keyword. If you were to refactor your code so that both methods had the same signature, then yes, the only way to call the behaviour of the parent class is to call super.Output() in the overriding method. In fact, if you then wrote your subclass B in the following way, the call to doStuff would be a recursive call and the method would recurse until you hit a stack overflow (hence, you need to use super.doStuff()):

class B extends A {
    public void doStuff(){

        doStuff(); // BAD CODE!! Use super.doStuff() here instead!

        System.out.println("Doing B Stuff!");
    }
}

Another way you could ensure you call the superclass's method is to instantiate the object with an actual type of the supertype, but then you lose all of the functionality of the subclass:

A a = new A(); //both parent type and actual type of a is A
a.doStuff(); // prints "Doing A stuff!" (can't print "Doing B Stuff!")

Also, as commented, you should check out this question which will point you in the direction of official Java style guidelines and conventions. Some quick things to note about your code is the lack of lower camel case for local variables (A1 should be a1) and methods (Output() should be output())

Upvotes: 0

MaxDevelop
MaxDevelop

Reputation: 637

I think in your example specifically shape must be interface(with method area() and (Rect,Square..) implementing it. Back to your question, because that is in your parent class Output is public you can do from child class this : super.Output();

Upvotes: 0

Eran
Eran

Reputation: 394156

Just call the method :

public void Output1() 
{
    Output();
    System.out.println("The Area is "+length*width);
}

There's no need for the super keyword, since you are not calling a method of the base class that is overridden by your Output1 method. You are calling a different method.

Upvotes: 3

Related Questions