2

I have 3 classes where one is the super class and other two are sub classes. They have print() method common in them. While calling print() methods from main() I have upcasted the objects to super class then is it not that it must invoke method of super class rather than subclasses. I am confused here a little bit. How is it possible? Is there any reason for such kind of behaviour in java.

Here is my code

class Tree {
public void print() {
    System.out.println("I am Tree");
}
public static void main(String []args) {
    Tree t[] = new Tree[3];
    t[0] = new Tree();
    t[1] = new BanyanTree();
    t[2] = new PeepalTree();
    for(int i = 0; i < 3; i++) {
        ((Tree)t[i]).print();
    }
  }
}


public class BanyanTree extends Tree{
public void print() {
    System.out.println("I am BanyanTree");
  }
}


public class PeepalTree extends Tree{
public void print() {
    System.out.println("I am PeepalTree");
  }
}

The output of the program is

I am Tree
I am BanyanTree
I am PeepalTree

According to me the output should be

I am Tree
I am Tree
I am Tree

Am I missing something here???

3
  • Your code is great example of polymorphism, which will execute the subclass method even on an object typed as the superclass. Commented Aug 14, 2019 at 17:19
  • Just because you call it something else doesn't actually change the underlying object in memory. This is how it should be, otherwise you'd break a fairly large sect of programming paradigms (LSP in particular). Upcasting is actually a bit of a nonsense phrase that I'm guilty of having used in the past myself Commented Aug 14, 2019 at 17:22
  • The cast (Tree) is not even needed here, since t is an array of Tree. Therefore t[i] is already a Tree, so the cast is a request to treat the Tree as a Tree. Commented Aug 15, 2019 at 3:05

3 Answers 3

1

This is called polymorphism. The method takes the same type (none), same return type (void), and same name (print). So, when you call the print method, it calls the most specialized (the method that is lowest in the inheritance tree). There is something called what they think the object is and what it really is. They think it is a Tree, but is actually a Banyan Tree. If you up cast it, it doesn't do anything because the array of Trees make it so that they think they are Trees, and if you up cast it to a tree they still think it is a tree. Technically, you're up casting it to the exact same thing. Sorry for not including this in my answer. When you make a new Tree, the inheritance structure looks like this.

Object (Cosmic Super-class)

 |

\ /

Tree

The most specialized method is tree. However, when you create another type of tree, let's say BanyanTree, the structure looks like this.

Object

 |

\ /

Tree

 |

\ /

BanyanTree

The most specialized print method comes from BanyanTree, so it executes that. Sorry if you didn't understand, it's a pretty complicated topic.

Sign up to request clarification or add additional context in comments.

4 Comments

But I have upcasted it to super class doesn't it have any meaning while resolving function call??
There is something called what they think the object is and what it really is. For example, Tree a = new BanyanTree(); They think it is a Tree, but is actually a Banyan Tree. If you up cast it, it doesn't do anything because the array of Trees make it so that they think they are Trees, and if you up cast it to a tree they still think it is a tree. Technically, you're up casting it to the exact same thing. Sorry for not including this in my answer.
@VaibhavMore if a subclass overrides a parent's method, it's impossible to execute the parent's version of the method from outside of the subclass without using reflection. The cast doesn't matter.
A point of polymorphism is that code that possesses a reference to a mere Tree can (in this example) cause the Tree to print out what sort of Tree it is, without actually having to know the different sort of Trees.
0

If DerivedClass extends BaseClass and has a method with same name and same signature of BaseClass then the overriding will take place at run-time polymorphism. Also there is no need in upcast as you already initialized your array as BaseClass[ ]

Comments

0

base class work as blueprint for derived class, since derived class inherit from base class and adds its own behaviors , the following link maybe help for this topic.

https://youtu.be/CZ3VMokXqQI

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.