• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#Overriding
Please help me with the output???//explain


class A{
A(){
display();
}
public void display(){
System.out.println("This is A");
}}
class B extends A{
B(){
display();
}
public void display(){
System.out.println("This is B");
}}
class C extends B{
C(){
display();
}
public void display(){
System.out.println("This is C");
}}
class Override{
public static void main(String... a1){
C c=new C();
}
}

//Output:--
This is C
This is C
This is C


PLease help!!
explain how the output came
how every display() call gets overridden
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kaiz jessani wrote:
how every display() call gets overridden


The overridden version of the method always get called -- even if the object has not completed construction.

Henry
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but how does this happen??
display() version of class C isnt present in class A or B!!
so how does it override??
please explain properly!
 
Bartender
Posts: 1840
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does it invoke C's display method from A and B? That's Polymorphism and late binding for you.
It decides at runtime based on the actual class of the object you have which version of the method it will call.
It will ALWAYS call the method appropriate to its actual class, no matter where in the hierarchy you currently are.
If this wasn't the case then abstract methods wouldn't be able to work at all.

You construct an Object of type C.
The process it will go through:

Start Constructor C
Start Constructor B
Start Constructor A
Constructor A - call display()
End Constructor A
Constructor B - call display()
End Constructor B
Constructor C - call display()
End Constructor C

In each case, the display method gets mapped to C's version (because we have an object of type C, even if we haven't finished constructing it yet.
This is why you should take care in calling an overridable function from a constructor.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and welcome to the Ranch
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup I got it..
Thank you so much for your help !
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Why does it invoke C's display method from A and B? That's Polymorphism and late binding for you.
It decides at runtime based on the actual class of the object you have which version of the method it will call.
It will ALWAYS call the method appropriate to its actual class, no matter where in the hierarchy you currently are.
If this wasn't the case then abstract methods wouldn't be able to work at all.

CAN YOU ELABORATE WITH EXAMPLE ABOUT ABSTRACT CLASSES THAT YOU WERE SPEAKING
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kaiz jessani wrote:
CAN YOU ELABORATE WITH EXAMPLE ABOUT ABSTRACT CLASSES THAT YOU WERE SPEAKING


1. Change your class A to be abstract, with an abstract display() method. And of course, remove the implementation body of the abstract method.

2. In your main method, change ...to ...
3. Add a call to the display method -- using the "a" reference (which is a reference of a type that is abstract).

Henry
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANK YOU
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kaiz jessani wrote:CAN YOU ELABORATE WITH EXAMPLE ABOUT ABSTRACT CLASSES THAT YOU WERE SPEAKING

Kaiz, can you KeepItDown (←click) a bit please?

I'm sure you don't mean to, but putting lots of CAPITALS and BOLDING and '!!'s in your posts is like shouting!! at people; and we're all volunteers here.

The fact is that there's a very good chapter about polymorphism in the tutorials.

And if you want an example of an abstract class, here's one (from your example above):and then all you'd need to do is change A to:
  public abstract class A extends AbstractA { ...

Try it out.

Winston
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid UPPER‑CASE writing.

For good design: any method called from the constructor should be marked final or private; I don't think you need both modifiers. That will prevent the sort of confusion you are getting here.
 
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does "Dynamic method dispatch" come into play here?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:Does "Dynamic method dispatch" come into play here?

What, when you override? I think so. I seem to remember an old adage in "C++-speak" that says:
  "All non-final instance methods in Java are virtual".
although I suspect they weren't including private methods in that list.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!