Can Java Call Parent Overridden Method in Other Objects But Not Subtype

Can java call parent overridden method in other objects but not subtype?

You can't call the super method in other objects - that would violate encapsulation. The whole point is that the object controls what its overridden methods do. For instance, you might override a collection's add method to throw an exception in certain circumstances, so it could ensure only "valid" items got added to the collection. That would be pointless if callers could just bypass it with a cast!

The only reason an object gets to call super.foo() for itself is to enable one call to be implemented by using the parent implementation. It's up to the code in the class to make sure it only ever does that sensibly. Again, to take the add-in-a-collection example, if the collection overrides add it would have to have some way of adding the validated item to the collection, which it would do with super.add().

Note that for the same reason of encapuslation, you can only call your parent implementation, not the grandparent implementation - so super.foo() is valid, but super.super.foo() isn't.

how to call Parent overridden method through object

The instance you created is an A instance. Wherever A has an implementation for a method like display(), that one (A.display() ) will be called and get control. Always.

A.display() can decide that it'll use the parent's implementation by calling super.display() somewhere in its body. You can't do it from outside, only the A.display() method itself can do so.

When you design a class, you decide how its instances will react to method calls. If you override a method, you have a reason to do so: with a child instance, the parent implementation won't do what the method name demands. So, were it possible to call the parent method from outside, things would generally go wrong.

why overridden method calling from Subclass if i have done up-casting?

When using inheritance, the compile-time type of the reference to an object on which you call a method is only used to see (at compile time) if the method may be invoked.

But at the invocation time, it does not matter what that compile-time type is. What really matters in this case is the runtime type of the object. It is Test, so the method is searched on Test first.

For methodOne() it is a bit different: it is not overriden by Test, so the version from its superclass (Example) is invoked.

this refers to what when a non-overridden method is invoked on a subclass object?

You declare the variable as type Employee

this.className

refers to the className in that class.

this.getClass().getSimpleName()

this.getClass() returns the class Employee, since that's how you declared the variable, which is why the getSimpleName() returns "Employee"

Is there a way in Java to call different child method depending on which subclass is the object?

Yes, it is entirely possible. Here's how that method would look like:

public <T extends Superclass> void foo(T subclassObject) {
...
}

Or:

public void foo(Superclass obj) {
...
}

Note that in the above method, you can pass subclasses' objects as well (they are covariant data types).

Non overridden method doesn't show subclass field

why print() called on subclass doesn't show string - "inner" ?

Because member variables cannot be overridden. The member variable st in class Child does not override the member variable st in class Parent. The two member variables are two separate variables, which happen to have the same name.

The methods in class Parent see the member variable st that is defined in class Parent, and not the one in class Child, even if the object is really an instance of class Child.

Only methods can be overridden.

More information in Oracle's Java Tutorials: Hiding Fields (thanks @JonK).



Related Topics



Leave a reply



Submit