1

I was wondering. When I have two classes while the second class metalRobot extends the first class robot is there a way of calling the method of the first class inside of the second method which does override it?

Example

//super class
class robot{
   private int width;
   private int height;

   //constructor
   robot(int width,int height){
    this.width=height;
    this.width=height;
   }

   //display width and height
   void render()
   {
     System.out.println(width + " " + height);
   }

}


//child class
class metalRobot{
   private int x;
   private int y;

   //constructor
   metalRobot(int x,int y, int height, int width){
    super(width,height);
    this.x=x;
    this.y=y;
   }

   @Override
   void render()
   {
     System.out.println(x + " " + y);
   }

}

so when i call render, it will print out x,y width and height

Harmlezz
  • 7,972
  • 27
  • 35
Yusof Bandar
  • 169
  • 1
  • 4
  • 9
  • 2
    Possible duplicate of [Java Inheritance - calling superclass method](http://stackoverflow.com/questions/6896504/java-inheritance-calling-superclass-method) – Tom Apr 12 '17 at 11:52
  • Your `metalRobot` needs to extend the `robot`-class, else it's not a child-class. – hamena314 Apr 12 '17 at 11:53
  • Class names in Java, by convention, should use UpperCase. – bcsb1001 Apr 12 '17 at 12:12
  • When I was teaching, I told my students I usually got suspicious when I saw an overriding method that didn’t contain a `super` call, as in `super.render();`. – Ole V.V. Apr 12 '17 at 12:16

5 Answers5

3

Call super.render() on the first line of render() in metalRobot.

Additional info: https://docs.oracle.com/javase/tutorial/java/IandI/super.html

Jure Kolenko
  • 799
  • 5
  • 10
2

First of you need to inherit from robot:

class metalRobot extends robot {
}

Then you can access the parent method by the keyword super as:

@Override
void render() {
    super.render();
    System.out.println(width + " " + height);
}
Harmlezz
  • 7,972
  • 27
  • 35
1

Calling super.render() is one solution. Another solution, and one that I personally prefer, is to use the decorator pattern. It requires an additional interface but it gives the user of your class more control over how they compose a composite object.

interface Robot
{
    void render();
}

class DefaultRobot implements Robot
{
    public void render()
    {
        //do some stuff
    }
}

class MetalRobot implements Robot
{
    private final Robot wrapped;

    public MetalRobot(Robot robot)
    {
        this.wrapped = robot;
    }

    public void render()
    {
        wrapped.render();
        // do some extra stuff
    }
}

Example usage may be something like:

Robot robot = new MetalRobot(new DefaultRobot());

Say you were to add a FatRobot and a RedRobot later on you could do:

// metal, fat, red robot
Robot robot = new MetalRobot(new FatRobot(new RedRobot(new DefaultRobot())));

// fat and red robot
Robot robot2 = new FatRobot(new RedRobot(new DefaultRobot()));

// red, metal robot
Robot robot3 = new RedRobot(new MetalRobot(new DefaultRobot()));

Or any other combination you can imagine. Polymorphism does not allow you to do this in an elegant way.

Michael
  • 41,989
  • 11
  • 82
  • 128
0

In order to call the overriden method of your super class you must use the super

keyword follow by a dot then the method name.

e.g. super.render()

Note that the super is only allowed in a non static method of the sub class.

You can also use the super in your constructor when you have a constructor in the

parent class that is not a default constructor you must provide the needed

arguments in your superclass constructor through your subclass constructor

e.g. If your super class has a constructor of MySuperclass(String myString)

and does not contain default constructor,

you must provide values for your superclass constructor to do this you use the super keyword

MySubclass()
{
   super("Needed String");
}

also note that multiple inheritance is not allowed in java so you cannot extends multiple times e.g. 2 times or more

Why?

because the child class doesn't know which super is which and many other reasons that is why James Gosling doesn't allow this

0xDEADBEEF
  • 590
  • 1
  • 5
  • 16
0

use super.render() to access the method of parent class