Suppose I have the following classes

  Class Parent{
    public void test(){
        System.out.println("Parent");
    }
  }

  Class Child extends Parent{
    public void test(){
        System.out.println("Child");
    }
  }

Parent parentE = new Child();
parentE.test();
((Parent)parentE).test();

Output is:

Child
Child

But how can I run parent method in this case using parentE reference?

有帮助吗?

解决方案 2

This is a key feature of polymorphism.

The object you instantiated is an instance of Child. Calling that object's test() is always going to result in the Child's (overridden) test() being called, even if you've assigned that instance to a variable of Parent type.

In short, this is how you can achieve more specific behavior while only having to reference the object using a variable declared a superclass type. Often this is used in conjunction with the base class only providing an abstract method for the subclasses to override.

A trivial example in beginner's books usually involves animals or cars:

public abstract class Animal
{
    private final String name;

    public Animal(String name)
    {
        this.name = name;
    }

    public void printName()
    {
        System.out.println(name);
    }

    // more animal general stuff followed by:

    public abstract void makeSound();

}

public class Dog extends Animal
{
    public Dog()
    {
        super("Dog");
    }

    @Override
    public void makeSound()
    {
        System.out.println("Woof!");
    }
}

List<Animal> aList = new ArrayList<Animal>();
aList.add(new Dog());
aList.add(new Cat());
// etc
for (Animal a : aList) 
{
    a.printName();
    a.makeSound(); // calls each subclasses' "makeSound()"
}

You can't call the Parent's test() unless you instantiate a Parent or call super.test() from within a method in Child.

(I was actually thinking I could find a good duplicate for this that provided a decent answer, but I couldn't)

其他提示

When you override the parent method, the child object will always call the overriding method (child method) due to Polymorphism.

So in your case either you call the parent method explicitly, inside the child object:

Class Child extends Parent{
    public void test(){
        super.test();
    }
  }

Or make a Parent object and call it's method.

Parent parentE = new Parent();
parentE.test();

Simple

Parent parentE = new Parent();
 parentE.test();

In case of method overriding that is resolved at run-time based on actual data type of object not on the basis of referee data type.

You can't call call super class's overridden method directly.

Basically, no, you can't do this, and it would be a bad idea. If Child decides that it needs to override one of its inherited methods, then the outside program shouldn't try to subvert that. It's as though the author of the outside module peeked into the implementation of the methods in Parent and Child and decided "I want the Parent version, not the Child version". But that isn't what OOP is all about. The writer of the outside program should not be concerned with how the method is implemented. See Jon Skeet's answer here, which I think explains this as well as anything I've found.

If you find that you really need to do this, then you have a design issue. The solution is going to depend on your particular project (your original code isn't a real-world example so there's no way to answer it for that). But in some cases, the solution might be that the classes need to provide two different methods that have similar but slightly different purposes. And it may be that in some subclasses of your class, the two methods would have the exact same implementation, but in other subclasses they would be different.

I think you mixed two concepts over here. casting and polymorphism. Read about these concept then things will be more clear for you.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top