Vaibhav Soni
Vaibhav Soni

Reputation: 131

Java Overriding issue

I have class Shape that is inherited by class Rectangle as below:

//Class Shape
public class Shape {
    public double area(){
        return 0.0;
    }
}

//Class  Rectangle 
public class Rectangle extends Shape{

    private int width;
    private int height;
    public Rectangle(int width,int height){
        this.width=width;
        this.height=height;
    }

    public void helloRectange(){
        System.out.println("Hello Rectangle");
    }


    public double area(){
        return this.height*this.width;
    }

}

//And Test Class


public class TestShape {

    public static void main(String []arge){

        Shape shape2=new Rectangle(10,20);

        shape2.helloRectange();


        System.out.println("Rectange is"+shape2.area());
    }

}

I am unable to call shape2.helloRectange(); method using shape2 object? Can somebody explain in detail.

The error is:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method helloRectange() is undefined for the type Shape at com.test.overriding.concept.TestShape.main(TestShape.java:9)

Upvotes: 2

Views: 106

Answers (5)

Lucas
Lucas

Reputation: 3281

You cannot do that because you used polymorphic assignment and static type of your objects is Shape. Even though the dynamic type is Rectangle and the object has the method on itself you cannot call it without a cast:

((Rectangle)shape2).helloRectangle();

That happens because the compiler doesn't know the dynamic type of the object and hence is not aware that the method exists there.

Read more about static/dynamic polymorphism:

http://www.oracle.com/technetwork/java/neutral-137988.html

http://beginnersbook.com/2013/04/runtime-compile-time-polymorphism/

Upvotes: 0

Powerlord
Powerlord

Reputation: 88786

For proper Polymorphism, you should declare Shape abstract and add a hello method to it.

Then, in Rectangle, create a hello method with the same arguments.

It'd look something like this:

//Class Shape
public abstract class Shape {
    public abstract void hello();
    public double area(){
        return 0.0;
    }
}

//Class  Rectangle 
public class Rectangle extends Shape{

    private int width;
    private int height;
    public Rectangle(int width,int height){
        this.width=width;
        this.height=height;
    }

    @Override
    public void hello(){
        System.out.println("Hello Rectangle");
    }


    public double area(){
        return this.height*this.width;
    }

}

Then this would work

//And Test Class


public class TestShape {

    public static void main(String []arge){

        Shape shape2=new Rectangle(10,20);

        shape2.hello();


        System.out.println("Rectange is"+shape2.area());
    }

}

Do note that all methods in Java are virtual. This isn't true of all languages and in things like C# you might have to specify the virtual keyword (abstract may automatically imply virtual, though)

Upvotes: 2

sol4me
sol4me

Reputation: 15698

Because Shape class doesn't have the method helloRectange , change:

Shape shape2=new Rectangle(10,20);

to:

Rectangle shape2=new Rectangle(10,20);

or cast the object like:

((Rectangle)shape2).helloRectange();

Upvotes: 4

Suresh Atta
Suresh Atta

Reputation: 121998

    Shape shape2=new Rectangle(10,20);

That line alone means that execute the implementations of Rectangle class of Shape class methods. Since Shape doesn't have a method helloRectangle, you cannot access it.

Upvotes: 0

Lawrence Aiello
Lawrence Aiello

Reputation: 4638

Because it is a Shape object, and shape does not have the helloRectange function. Try making it a Rectangle:

Rectangle shape2 = new Rectangle(10,20);
shape2.helloRectange();

Upvotes: 1

Related Questions