Overloading and Polymorphism - java

I wish someone would explain me how this decision was taken. I understand that, the overloaded version is chosen based on the declared type, but why, on the second call, the decision was taken based the runtime type?
public class Test {
public static void main(String[] args) {
Parent polymorphicInstance = new Child();
TestPolymorphicCall tp = new TestPolymorphicCall();
tp.doSomething(polymorphicInstance);
// outputs: Parent: doing something...
call(polymorphicInstance);
// outputs: Child: I'm doing something too
}
public static void call(Parent parent){
parent.doSomething();
}
public static void call(Child child){
child.doSomething();
}
}
public class Parent {
public void doSomething() {
System.out.println("Parent: doing something...");
}
}
public class Child extends Parent{
#Override
public void doSomething() {
System.out.println("Child: I'm doing something too");
}
}
public class TestPolymorphicCall {
public void doSomething(Parent p) {
System.out.println("Parent: doing something...");
}
public void doSomething(Child c) {
System.out.println("Child: doing something...");
}
}
Thanks in advance!

Your Parent class reference is referring to Child class object:
Parent polymorphicInstance = new Child();
So, when you pass the reference in call method, the actual method invoked is the one with Parent parameter type only. But when you call the method doSomething(), on parent reference:
public static void call(Parent parent){
parent.doSomething();
}
It will call doSomething() method, that you have overridden in Child class.
This is the classic case of polymorphism. Suppose you have a class Shape and a subclass Circle, which overrides the method calculateArea() defined in Shape class.
Shape circle = new Circle();
// This will invoke the method in SubClass.
System.out.println(circle.calculateArea());
When you override a super class method in subclass, then the actual method invoked is decided at runtime, based on what actual object your super class reference points to. This is called Dynamic Dispatch of method call.

Related

Call method with implicit downcast

I have a List defined as
List<ParentClass> parentClassList;
Parent class is abstract, so when I add elements to the List I do something like
parentClassList.add(new ChildClassOne(...));
parentClassList.add(new ChildClassTwo(...));
and so on... I actually have 5 child classes right now.
What I'd like to do is to call a method in another Class, overwriting its arguments, so:
public void doSomething(ChildClassOne arg) {...}
public void doSomething(ChildClassTwo arg) {...}
But if the type of the List is the parent Class I can't do
doSomething(parentClassList.get(0));
Basically I need to perform different actions based on the child's type and I need access to all the methods inside a specific child. Different childs have different methods, they have only one methods in common.
Define an abstract emthod doSomthing() in ParentClass, and in each sub class you implement it like this:
class ChildClassOne {
void doSomething() {
instanceOfSomeOtherClass.doSomething(this); // will call the correct method
}
}
and SomeOtherClass will have methods for each sub class:
class SomeOtherClass {
void doSomething(ChildClassOne o) {};
void doSomething(ChildClassTwo o) {};
void doSomething(ChildClassThree o) {};
...
}
You can read about this approach in regards to the Visitor Pattern
It might be even simpler if you moved the code from SomeOtherClass into the ChildClasses, though.
In this situation a double dispatch technique called Visitor Pattern becomes very handy:
interface Visitor {
void visitChildOne(ChildOne child);
void visitChildTwo(ChildTwo child);
void visitChildThree(ChildThree child);
}
abstract class ParentClass {
public abstract void accept(Visitor v);
...
}
class ChildClassOne extends ParentClass {
#Override
public void accept(Visitor v) { v.visitChildOne(this); }
}
class ChildClassTwo extends ParentClass {
#Override
public void accept(Visitor v) { v.visitChildTwo(this); }
}
class ChildClassThree extends ParentClass {
#Override
public void accept(Visitor v) { v.visitChildThree(this); }
}
When you need to perform some task that does different things depending on the type of a child, provide an implementation of the Visitor interface, and pass it to accept of each of the children that you wish to process:
Visitor v = new Visitor() {
#Override
public void visitChildOne(ChildOne child) {
System.out.println("Visiting child type 1");
String someProperty = child.getPropertySpecificToChildOne();
}
#Override
public void visitChildTwo(ChildTwo child) {
System.out.println("Visiting child type 2");
int someProperty = child.getPropertySpecificToChildTwo();
}
#Override
public void visitChildThree(ChildThree child) {
System.out.println("Visiting child type 3");
}
};
for (Parent p: parentClassList) {
p.accept(v);
}
Basically I need to perform different actions based on the child's type and I need access to all the methods inside a specific child.
In this case the behavior you want to implement belongs into this specialized classes.
You might add another (abstract) method signature to the base class which you implement in the child classes providing the type depended behavior.

is there any way to call parent class method from child class object in java without modifying methods

I have parent class and a child class, both of having a method m1 with same signature (Override), can I call parent class method in following scenario. I dont want to change child class method.
// Super class
public class Parent
{
public void m1()
{
System.out.println("Parent method");
}
}
// Sub class
public class Child extends Parent {
#Override
public void m1() {
System.out.println("Child method");
}
}
// User class
public class Kavi {
public static void main(String[] args) {
Parent p = new Child();
p.m1();
}
}
I want to call parent class m1 method. I know that I can use super in child class method to call its parent method. but I have no right to change the source code of child class. and I have to call it from child class object. please anybody help !!! is it possible in java ??
While creating the Object you are using reference of Super class but your object is of child class, so while calling m1() method the overrided method will be invoked. If you want the method of the super class to be invoked then object should be of Super class. As :
Parent parent=new Parent();
parent.m1();
OR
you can invoke the super class m1() method from the child class.
#Override
public void m1() {
super.m1();
System.out.println("Child method");
}
OR ELSE
import java.lang.reflect.*;
class A {
public void method() {
System.out.println("In a");
}
}
class B extends A {
#Override
public void method() {
System.out.println("In b");
}
}
class M {
public static void main( String ... args ) throws Exception {
A b = new B();
b.method();
b.getClass()
.getSuperclass()
.getMethod("method", new Class[]{} )
.invoke( b.getClass().getSuperclass().newInstance() ,new Object[]{} ) ;
}
}
Without changing the code, you can't do this. You're essentially talking about p.super.m1() which isn't legal in Java. If you want your parent to act like a parent, don't make it a child.
If both parent and child are stateless, you could create a facade over them and explicitly manage the state; this would work, but I wouldn't recommend it.
public class Facade extends Parent {
public enum State {PARENT, CHILD};
private final Child delegate;
private State state = State.CHILD;
public Facade(Child delegate) {
this.delegate = delegate;
}
#Override
public void m1() {
if (State.CHILD == state) {
delegate.m1();
} else {
super.m1();
}
}
public void setState(State state) {
this.state = state;
}
}
This is a purely academic exercise - I can't think of a single good reason to do this in the real world. If you're using an OO language, don't fight the OO paradigm!
I think it not possible. There are two ways to call a parent class method
1.) crate object of parent class as
Parent p = new Parent();
2.) Use super in child class method as
#Override
public void m1() {
super.m1();
System.out.println("Child method");
}
Apart from the already mentioned way, you can declare both the methods as static.
so when you do this
Parent p = new Child();
p.m1();
the static method of parent class would be called and the output will be "Parent method"
Note : The static keyword in Java means that the variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves.
So if you have a variable:
private static int i = 0; and you increment it ( i++ ) in one instance, the change will be reflected in all instances.
If you can not use super then instead of creating the child class object you can directly use
Parent p = new Parent();
p.m1();
if you can't even modify the code inside main method then I think it's not possible .

Overriding vs Hiding Java - Confused

I'm confused on how overriding differs from hiding in Java. Can anyone provide more details on how these differ? I read the Java Tutorial but the sample code still left me confused.
To be more clear, I understand overriding well. My issue is that I don't see how hiding is any different, except for the fact that one is at the instance level while the other is at the class level.
Looking at the Java tutorial code:
public class Animal {
public static void testClassMethod() {
System.out.println("Class" + " method in Animal.");
}
public void testInstanceMethod() {
System.out.println("Instance " + " method in Animal.");
}
}
Then we have a subclass Cat:
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The class method" + " in Cat.");
}
public void testInstanceMethod() {
System.out.println("The instance method" + " in Cat.");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
Then they say:
The output from this program is as follows:
Class method in Animal.
The instance method in Cat.
To me, the fact that calling a class method testClassMethod() directly from the Animal class executes the method in Animal class is pretty obvious, nothing special there. Then they call the testInstanceMethod() from a reference to myCat, so again pretty obvious that the method executed then is the one in the instance of Cat.
From what I see, the call hiding behaves just like overriding, so why make that distinction? If I run this code using the classes above:
Cat.testClassMethod();
I'll get:
The class method in Cat.
But if I remove the testClassMethod() from Cat, then I'll get:
The class method in Animal.
Which shows me that writing a static method, with the same signature as in the parent, in a subclass pretty much does an override.
Hopefully I'm making clear my where I'm confused and someone can shed some light. Thanks very much in advance!
Overriding basically supports late binding. Therefore, it's decided at run time which method will be called. It is for non-static methods.
Hiding is for all other members (static methods, instance members, static members). It is based on the early binding. More clearly, the method or member to be called or used is decided during compile time.
In your example, the first call, Animal.testClassMethod() is a call to a static method, hence it is pretty sure which method is going to be called.
In the second call, myAnimal.testInstanceMethod(), you call a non-static method. This is what you call run-time polymorphism. It is not decided until run time which method is to be called.
For further clarification, read Overriding Vs Hiding.
Static methods are hidden, non-static methods are overriden.
The difference is notable when calls are not qualified "something()" vs "this.something()".
I can't really seem to put it down on words, so here goes an example:
public class Animal {
public static void something() {
System.out.println("animal.something");
}
public void eat() {
System.out.println("animal.eat");
}
public Animal() {
// This will always call Animal.something(), since it can't be overriden, because it is static.
something();
// This will call the eat() defined in overriding classes.
eat();
}
}
public class Dog extends Animal {
public static void something() {
// This method merely hides Animal.something(), making it uncallable, but does not override it, or alter calls to it in any way.
System.out.println("dog.something");
}
public void eat() {
// This method overrides eat(), and will affect calls to eat()
System.out.println("dog.eat");
}
public Dog() {
super();
}
public static void main(String[] args) {
new Dog();
}
}
OUTPUT:
animal.something
dog.eat
This is the difference between overrides and hiding,
If both method in parent class and child class are an instance method, it called overrides.
If both method in parent class and child class are static method, it called hiding.
One method cant be static in parent and as an instance in the child. and visa versa.
If I understand your question properly then the answer is "you already are overriding".
"Which shows me that writing a static method, with the same name as in the parent, in a subclass pretty much does an override."
If you write a method in a subclass with exactly the same name as a method in a superclass it will override the superclass's method. The #Override annotation is not required to override a method. It does however make your code more readable and forces the compiler to check that you are actually overriding a method (and didn't misspell the subclass method for example).
Overriding happens only with instance methods.
When the type of the reference variable is Animal and the object is Cat then the instance method is called from Cat (this is overriding). For the same acat object the class method of Animal is used.
public static void main(String[] args) {
Animal acat = new Cat();
acat.testInstanceMethod();
acat.testClassMethod();
}
Output is:
The instance method in Cat.
Class method in Animal.
public class First {
public void Overriding(int i) { /* will be overridden in class Second */ }
public static void Hiding(int i) { /* will be hidden in class Second
because it's static */ }
}
public class Second extends First {
public void Overriding(int i) { /* overridden here */ }
public static void Hiding(int i) { /* hides method in class First
because it's static */ }
}
The rule for memorizing is simple: a method in an extending class
can't change static to void and
can't change void to static.
It will cause of compile-error.
But if void Name is changed to void Name it's Overriding.
And if static Name is changed to static Name it's Hiding. (Both the static method of the subclass as well as the one of the superclass can be called, depending on the type of the reference used to call the method.)
In this code snippet I use 'private' access modifier instead of 'static' to show you difference between hiding methods and overriding methods.
class Animal {
// Use 'static' or 'private' access modifiers to see how method hiding work.
private void testInstancePrivateMethod(String source) {
System.out.println("\tAnimal: instance Private method calling from "+source);
}
public void testInstanceMethodUsingPrivateMethodInside() {
System.out.println("\tAnimal: instance Public method with using of Private method.");
testInstancePrivateMethod( Animal.class.getSimpleName() );
}
// Use default, 'protected' or 'public' access modifiers to see how method overriding work.
protected void testInstanceProtectedMethod(String source) {
System.out.println("\tAnimal: instance Protected method calling from "+source);
}
public void testInstanceMethodUsingProtectedMethodInside() {
System.out.println("\tAnimal: instance Public method with using of Protected method.");
testInstanceProtectedMethod( Animal.class.getSimpleName() );
}
}
public class Cat extends Animal {
private void testInstancePrivateMethod(String source) {
System.out.println("Cat: instance Private method calling from " + source );
}
public void testInstanceMethodUsingPrivateMethodInside() {
System.out.println("Cat: instance Public method with using of Private method.");
testInstancePrivateMethod( Cat.class.getSimpleName());
System.out.println("Cat: and calling parent after:");
super.testInstanceMethodUsingPrivateMethodInside();
}
protected void testInstanceProtectedMethod(String source) {
System.out.println("Cat: instance Protected method calling from "+ source );
}
public void testInstanceMethodUsingProtectedMethodInside() {
System.out.println("Cat: instance Public method with using of Protected method.");
testInstanceProtectedMethod(Cat.class.getSimpleName());
System.out.println("Cat: and calling parent after:");
super.testInstanceMethodUsingProtectedMethodInside();
}
public static void main(String[] args) {
Cat myCat = new Cat();
System.out.println("----- Method hiding -------");
myCat.testInstanceMethodUsingPrivateMethodInside();
System.out.println("\n----- Method overriding -------");
myCat.testInstanceMethodUsingProtectedMethodInside();
}
}
Output:
----- Method hiding -------
Cat: instance Public method with using of Private method.
Cat: instance Private method calling from Cat
Cat: and calling parent after:
Animal: instance Public method with using of Private method.
Animal: instance Private method calling from Animal
----- Method overriding -------
Cat: instance Public method with using of Protected method.
Cat: instance Protected method calling from Cat
Cat: and calling parent after:
Animal: instance Public method with using of Protected method.
Cat: instance Protected method calling from Animal
I think this is not yet fully explained.
Please see the following example.
class Animal {
public static void testClassMethod() {
System.out.println("The static method in Animal");
}
public void testInstanceMethod() {
System.out.println("The instance method in Animal");
}
}
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The static method in Cat");
}
public void testInstanceMethod() {
System.out.println("The instance method in Cat");
}
public static void main(String[] args) {
Animal myCat = new Cat();
Cat myCat2 = new Cat();
myCat.testClassMethod();
myCat2.testClassMethod();
myCat.testInstanceMethod();
myCat2.testInstanceMethod();
}
}
The output will be as follows.
The static method in Animal
The static method in Cat
The instance method in Cat
The instance method in Cat
Based on my recent Java studies
method overriding, when the subclass have the same method with the same signature in the subclass.
Method hiding, when the subclass have the same method name, but different parameter. In this case, you're not overriding the parent method, but hiding it.
Example from OCP Java 7 book, page 70-71:
public class Point {
private int xPos, yPos;
public Point(int x, int y) {
xPos = x;
yPos = y;
}
public boolean equals(Point other){
.... sexy code here ......
}
public static void main(String []args) {
Point p1 = new Point(10, 20);
Point p2 = new Point(50, 100);
Point p3 = new Point(10, 20);
System.out.println("p1 equals p2 is " + p1.equals(p2));
System.out.println("p1 equals p3 is " + p1.equals(p3));
//point's class equals method get invoked
}
}
but if we write the following main:
public static void main(String []args) {
Object p1 = new Point(10, 20);
Object p2 = new Point(50, 100);
Object p3 = new Point(10, 20);
System.out.println("p1 equals p2 is " + p1.equals(p2));
System.out.println("p1 equals p3 is " + p1.equals(p3));
//Object's class equals method get invoked
}
In the second main, we using the Object class as static type, so when we calling the equal method in Point object, it's waiting a Point class to arrive as a parameter,but Object coming. So the Object class equals method getting run, because we have an equals(Object o) there. In this case, the Point's class equals dosen't overrides, but hides the Object class equals method.
public class Parent {
public static void show(){
System.out.println("Parent");
}
}
public class Child extends Parent{
public static void show(){
System.out.println("Child");
}
}
public class Main {
public static void main(String[] args) {
Parent parent=new Child();
parent.show(); // it will call parent show method
}
}
// We can call static method by reference ( as shown above) or by using class name (Parent.show())
The linked java tutorial page explains the concept of overriding and hiding
An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.
If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.
The distinction between hiding a static method and overriding an instance method has important implications:
The version of the overridden instance method that gets invoked is the one in the subclass.
The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.
Coming back to your example:
Animal myAnimal = myCat;
/* invokes static method on Animal, expected. */
Animal.testClassMethod();
/* invokes child class instance method (non-static - it's overriding) */
myAnimal.testInstanceMethod();
Above statement does not show hiding yet.
Now change the code as below to get different output:
Animal myAnimal = myCat;
/* Even though myAnimal is Cat, Animal class method is invoked instead of Cat method*/
myAnimal.testClassMethod();
/* invokes child class instance method (non-static - it's overriding) */
myAnimal.testInstanceMethod();
In addition to the examples listed above, here is a small sample code to clarify the distinction between hiding and overriding:
public class Parent {
// to be hidden (static)
public static String toBeHidden() {
return "Parent";
}
// to be overridden (non-static)
public String toBeOverridden() {
return "Parent";
}
public void printParent() {
System.out.println("to be hidden: " + toBeHidden());
System.out.println("to be overridden: " + toBeOverridden());
}
}
public class Child extends Parent {
public static String toBeHidden() {
return "Child";
}
public String toBeOverridden() {
return "Child";
}
public void printChild() {
System.out.println("to be hidden: " + toBeHidden());
System.out.println("to be overridden: " + toBeOverridden());
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
child.printParent();
child.printChild();
}
}
The call of child.printParent() outputs:
to be hidden: Parent
to be overridden: Child
The call of child.printChild() outputs:
to be hidden: Child
to be overridden: Child
As wee can see from the outputs above (especially the bold marked outputs), method hiding behaves differently from overriding.
Java allows both hiding and overriding only for methods. The same rule does not apply to variables. Overriding variables is not permitted, so variables can only be hidden (no difference between static or non-static variable). The example below shows how the method getName() is overriden and the variable name is hidden:
public class Main {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.name); // prints Parent (since hiding)
System.out.println(p.getName()); // prints Child (since overriding)
}
}
class Parent {
String name = "Parent";
String getName() {
return name;
}
}
class Child extends Parent {
String name = "Child";
String getName() {
return name;
}
}
At runtime the child version of an overridden method is always executed for an instance
regardless of whether the method call is defi ned in a parent or child class method. In this
manner, the parent method is never used unless an explicit call to the parent method is
referenced, using the syntax
ParentClassName.method().
Alternatively, at runtime the parent
version of a hidden method is always executed if the call to the method is defined in the
parent class.
In method overriding, method resolution is done by the JVM on the basis of runtime object. Whereas in method hiding, method resolution is done by the compiler on the basis of reference.
Thus,
If the code would have been written as,
public static void main(String[] args) {
Animal myCat = new Cat();
myCat.testClassMethod();
}
The Output would be as below:
Class method in Animal.
It is called hiding because the compiler hides the super class method implementation, when subclass has the same static method.
Compiler has no restricted visibility for overridden methods and it’s only during runtime that it’s decided which one is used.
This is the difference between overriding and hiding:
Animal a = new Cat();
a.testClassMethod() will call the method in parent class since it is an example of method hiding. The method to be called is determined by the type of the reference variable and decided at compile time.
a.testInstanceMethod() will call the method in child class since it is an example of method overriding. The method to be called is determined by the object which is used to call the method at runtime.
How is static method hiding happening in java?
Cat class is extending Animal class. So in Cat class will have both static methods (i mean Child class's static method and Parent class's static method)
But how JVM hiding Parent static method? How it's dealing in Heap and Stack?

How to call overriden function from child in java?

I want to make an interface and from user point of view i want it to look clean and they can write their code with this syntax.
public class child extends parent {
#Override
public void run() {
}
}
Main is in the parent but how can one call a overriden function in parent.
Also i don't want the name "child" to be mandatory so i can't call it directly.
PS:this is the run function i want to override.
public class parent{
public static void main(String[] args) {
run();
}
}
Make Parent and run abstract. Abstract for a class means that this class cannot be directly instantiated, but can be instantiated if there is a subclass. Abstract for a method means that the method is defined in the abstract class, but is not implemented in the abstract class. Instead subclasses must provide an implementation of the abstract method or declare themselves to be abstract.
public abstract class Super {
public static void main(String[] args) {
Super s = new Sub();
s.main();
}
public abstract void run();
public void main() {
System.out.println("Calling sub class's implementation of run");
// The super class does not know the implementation of run
// but it does know that there must be an implementation to use.
run();
System.out.println("Done!");
}
}
class Sub extends Super {
#Override
public void run() {
System.out.println("sub class implementation of run");
}
}
to call overridden function of perent class use..
super.run();
EX:
public class child extends parent {
#Override
public void run() {
super.run();
}
}
what i get from you is ..u want to call child function from parent class..
For that is seams like a normal class function coz there is only relationship b/w parent-to-child no relation in child-to-parent
So u just make object of child and then call function of child class...
So your main of parent class may be look like this..
public static void main(String[] args) {
new child().run();
}
To call an overwritten function in the parent user something like one of these:
super.run();
((parent) this).run();
Do you mean you want to call the run() function defined in the parent?
public class child extends parent
{
#Override
public void run()
{
super.run(); // call parent's run() function
this.doStuff(); // call child's additional functionality
}
}
You can call super(Parent) class methods using 'super' keyword. Like this.
public class child extends parent {
#Override
public void run() {
super.run();
}
}
I suppose you are confusing between main() function and some random Main class that you are referring to. In general when it comes to inheritance you cannot call a derived class's overridden function from a base class. In that case what you do is create a base class pointer and make it point to derived class object. In that case when you use the base class pointer to invoke the function the overriden derived classs' function gets called.
I am not sure if I answered you entirely..
This might help you.. -> http://www.oodesign.com/dependency-inversion-principle.html
Not sure if i understand correctly. But parent can't know if, where and how it's protected/public methods will be overriden so it cant call the overridden implementations.
If you have to do it, you probably got your class hierarchy design wrong.
You probably need something like this.
public Parent
{
public final void run()
{
this.runChild
}
protected abstract void runChild();
}
public class Child extends Parent
{
public static void main(String[] args)
{
run();
}
protected void runChild()
{
....
}
}
I am not sure whether I am answering your question right. From what I understood is you are trying to do some thing like calling child's run from Parent. If that is the case below is the code snippet to do it.
public abstract class Parent{
public abstract void run();
public void mainMethod(){
//This automatically calls run from child
run();
}
}
And your child implementation is like as shown below.
public class child extends parent {
#Override
public void run() {
//Do the stuff you want to
}
}
public class MainClass{
public static void main(String args[]){
Parent obj = new Child();
// This inturn takes care of the rest.
obj.mainMethod();
//Some other child
obj = new Child2();
obj.mainMethod();
}
}
Hope this helps you.

Method calls another method that gets overridden, which is called in the subclass?

If I have 2 classes, one being the parent with the following:
public class Parent {
...
public void method1() {
method2();
}
public void method2() {
}
}
And then in the subclass
public class Child extends Parent {
...
public void method2() {
...
}
}
If I run the following code:
Child c = new Child();
c.method1();
Which version of method2 gets called?
All methods are virtual in Java, which means that it is the Child.method2 that will be called (even if the call is done from the context of Parent).
If the correctness of Parent.method1 relies on the implementation of method2, you should design it differently:
public class Parent {
...
public void method1() {
method2impl();
}
public void method2() {
method2impl();
}
// make it private or final.
public final method2impl() {
...
}
}
Child#method2 will be called, as it overrides that of the parent.
Once you've created an object of type Child, that's its runtime type. This won't change, regardless of casts or whatever you do to it. When you call a method, the implementation of that runtime type is going to be executed. If that type doesn't have an implementation of its own, it'll delegate to the parent class.
Even though you call method1 which was defined in Parent, once that method calls method2 it'll resolve to the implementation of the runtime type of the object. If that's Child, then that's the class' method which will be called.
Mind that this dynamic behaviour is different than selecting a method based on parameter types, which is done statically. Take the following, with your class definitions...
public void methodTest(Parent p) {} //Let's call this "first method"
public void methodTest(Child c) {} //Let's call this "second method"
Parent p = new Parent();
Child c = new Child();
//Assume a is a variable of some type that implements the above methods
a.methodTest(p); //Will call first method
a.methodTest(c); //Will call second method
a.methodTest((Parent)c); //WILL CALL FIRST METHOD!
So selecting a method based on parameter types is done statically. It won't select a different method based on runtime type.
But selecting a method based on what object it's being called on depends on that object's runtime type. That's what allows us to override method behaviour in subclasses.
public class Parent {
public void method1() {
method2();
}
public void method2() {
System.out.println("parent m 2");
}
}
public class Child extends Parent {
public void method2(){
System.out.println("child m 2");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.method1();
System.out.println("________________");
c.method2();
}
}
And the output will be:
child m 2
________________
child m 2
I have some more questions for you:
public interface CanDoMethod1 {
public void method1();
}
public class Parent implements CanDoMethod1 {
public void method1() {
System.err.println("Parent doing method1");
}
}
public class Child extends Parent {
public void method1() {
System.err.println("Child doing method1");
}
}
Now you run the following code:
CanDoMethod1 instance = new Parent();
instance.method1();
What is the output?
And when you run:
CanDoMethod1 instance = new Child();
instance.method1();
Then what is the output?
And when you run:
Parent instance = new Child();
instance.method1();
Then what is the output?
Why is no cast needed here?
And when you run:
Child instance = (Child) new Parent();
instance.method1();
Does this compile?
If so, then what is the output?
In summary, notice that the method called is always the method of the implementation class you created, no matter what you cast or assign it to.

Categories

Resources