Vui La Chinh
Vui La Chinh

Reputation: 243

How to create an instance that belongs to an interface and implement it

I'm currently learning Java so please bear with my ignorance. Here is my current code

Shape.java

public interface Shape {
    public abstract void draw();
}

Rectangle.java

public abstract class Rectangle implements Shape {

    private final double width, length;

    public Rectangle() {
        this(1,1);
    }
    public Rectangle(double width, double length) {
        this.width = width;
        this.length = length;
    }

    public void draw() {
        System.out.println("A rectangle of sides " +  length + " by " + width + " will be drawn");
    }

}

TestPolymorph.java

public class TestPolymorph implements Shape {

    public static void main(String[] args) {
        Shape[] drawObject = { new Rectangle(40, 60) };
        drawObject[0].draw();
    }

    @Override
    public void draw() {
        // TODO Auto-generated method stub

    }

}

Is there anything wrong with my current code since it's not working. My question is how can I create a drawObject instance that belongs to Shape class and during runtime drawObjectwill be created with two arguments, length and width (giving 40 and 60 for example), draw method of Rectangle will then be invoked.

Upvotes: 1

Views: 142

Answers (5)

KayV
KayV

Reputation: 13835

Make it simple. Rather than complicating the things. Just go in a valid inherited manner. Remove the abstract from Rectangle class and also there is no need for TestPolymorph to implement the Shape interface. Rectangle implements the Shape interface and just Test Polymorphism in TestPolymorph.

Upvotes: 0

Abhishek Dadhich
Abhishek Dadhich

Reputation: 321

  1. Improving Interface

Every method declaration in the body of an interface is implicitly public and abstract.

Ref: http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.4

Upvotes: 0

Priyamal
Priyamal

Reputation: 2969

Shape[] drawObject = { new Rectangle(40, 60) };

above is your code

Shape[] drawObject = { new Rectangle(40, 60) {} };

this is correct code

since you are creating objects of abstract class you have to go with this approach. or you can just remove abstract modifier from Rectangle class

if you are creating a object of abstract class you are forced to implement all its abstract methods , since you dont have any abstract method inside your abstract rectangle class you just have to new Rectangle(40, 60) {} pass empty brackets at the end bt if your class has abstract methods inside the brackets you have to implement the abstract method

Upvotes: 2

Jesse Buss
Jesse Buss

Reputation: 843

Try to remove the abstract modifier from the Rectangle object. You also don't need to implement the Shape interface in TestPolymorph.java

Upvotes: 2

Brett Partridge
Brett Partridge

Reputation: 256

You're close, there really isn't any need to have your TestPolymorph implement Shape. That is your driver, not a model implementing an interface so you could take that off.

Lastly, remove the abstract from the Rectangle class. That is not an abstract class because you actually want an instance of that type.

Upvotes: 5

Related Questions