Lousi Matthews
Lousi Matthews

Reputation: 27

Java inheritance concept

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()?

Upvotes: 0

Views: 1005

Answers (2)

scottb
scottb

Reputation: 10084

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();

Upvotes: 0

user1886323
user1886323

Reputation: 1199

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

Upvotes: 1

Related Questions