0

I am a newbie in Java, and would like to understand more about inheritance. Suppose

class Vehicle{

public void move(){

System.out.println(“Vehicles can move”);

}

}

class MotorBike extends Vehicle{

   public void move(){

    System.out.println(“MotorBike can move and accelerate tool”);

    }

    }

class Test{

public static void main(String[] args){

Vehicle vh=new MotorBike();

vh.move();    

vh=new Vehicle();

vh.move();   

}

}

When we do vh.move() in the 1st time it prints MotorBike can move and accelerate tool. Second time it prints Vehicles can move. It can be called method overriding. Because we have same method name in two class.

But, if two classes have different method, then which method should be called? I want to say like that,

class Vehicle{

public void move(){

System.out.println(“Vehicles can move”);

}

}

class MotorBike extends Vehicle{

   public void part(){

    System.out.println(“MotorBike can move and accelerate tool”);

    }

    }

class Test{

public static void main(String[] args){

vehicle a = new vehicle();
Vehicle vh=new MotorBike();


}

}

In the first case vehicle a = new vehicle();it invoke move() and

What will be the second case? If I do `Vehicle vh=new MotorBike(); Which method should be called? move() or part()?

2
  • vehicle a = new vehicle(); Vehicle vh=new MotorBike(); you are not calling any methods here? Commented Jul 31, 2015 at 20:09
  • The method that gets invoke depends on actual type of the object, not type of the variable you assign it to. Vehicle vh = new MotorBike(); makes vh a MotorBike object, so its override will be invoked. In addition, having a reference to the MotorBike object that is of type Vehicle will limit you to ONLY the methods and fields declared in the Vehicle class, but if overrides exist in MotorBike, their implementation will be used. Check out docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html Commented Jul 31, 2015 at 20:23

2 Answers 2

1

In the second case, even though you have defined an extra method part(), you can only call the move() method (which is inherited from Vehicle) because you have told the compiler that vh is a Vehicle (it doesn't know that it's a Motorbike)

Vehicle vh = new MotorBike(); // from now on, vh is a Vehicle - we don't know it's a MotorBike
vh.move(); // this is fine
vh.part(); // this will not compile

If you want to call the part() method then you have to define the variable as a MotorBike:

MotorBike vh = new MotorBike(); // now we know it's a MotorBike
vh.part(); // this is ok now
Sign up to request clarification or add additional context in comments.

Comments

0

it can be called method overridding.becoz we have same method name in two class.

Well, yes ... but what your code illustrates is that the same method invoked on two objects referenced by the same variable can exhibit different behavior (that is determined by their underlying type). Yes, this requires overloading, but the principle that has been demonstrated is polymorphism.

what will be the 2nd case?if i do Vehicle vh=new MotorBike(); which method should be called? move() or part()??

In polymorphism, the most specific method in an object hierarchy gets invoked when that method is invoked on an object reference. Consider this example:

public class Parent {

    public void emote() {
        System.out.println("I'm the parent")'
    }

    public void parentMethod() { ... }
}

public class Child extends Parent {

    @Override
    public void emote() {
        System.out.println("I'm the child");
    }

    public void childMethod() { ... }
}

Now consider the following test code:

public void test() {

    Parent p1 = new Parent();
    p1.emote();    // method invocation prints "I'm the parent"

    Parent p2 = new Child();
    p2.emote();    // prints "I'm the child"
}

Note that the p2 variable type does not determine the method that gets invoked when the emote() method is called. This is important to understant because a reference variable can point to an object of its own type ... or to any other object that is a subclass of the variable's type.

The method that is invoked is the most specific method that applies to the actual object that is referenced by the variable. In this case, the emote() method on the Child object itself is invoked.

Also, this is valid:

p2.parentMethod();

Even though p2 references a Child object, the Child class inherits the parentMethod() method from it and so the invocation works. These fail, though:

p1.childMethod();
p2.childMethod();

The Parent class does not know anything about the methods that any of its subclasses may define. It may not be intuitive why the second invocation should fail. Although p2 references a Child object, the Parent class does not have a childMethod() declaration. To make this work, we would need to cast p2 to the Child type:

((Child) p2).childMethod();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.