• Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying an override in inheritance.  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm learning about inheritance and part of my problem is to create an Order with methods, then an UpdateOrder where the total price is
changed by adding four dollars to it, and then a main method displaying a few orders. I've copied all three below in order. My question is
when I run the program it will display the totalprice() first for the second order followed by name, number, etc. Why is that? Is
what you override always displayed first regardless of the order you put them in? (The issue is at line 31 on the third code.)






 
Swaraj Pal
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ryan,
In your code I suppose there is nothing wrong with the inheritance , you are doing everything perfect, so is the JVM,
Here is the problem :-

1. You think that line 31 code runs before line 27.

2. But if you check line 25 it will show you that it runs the overridden method of ShippedOrder Class and on checking the code of ShippedOrder's totalPrice() you have a message display(showMessageDialog) there. check here



In above method it displays the total price before everyone else.

As it is not displayTotalPrice(), you should define this method as:-


Hope it helps, reply if your problem is solved.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes perfect sense. Wish you were my teacher. I'm at work but will dabble when I get home. Thanks!
 
Swaraj Pal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help. :-)
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works perfectly and I appreciate your help. I'm curious, is it possible to override a method and then have a different print out? Meaning, can I change it
so I can use both classes and have one print out "total order is: " and another print out "total order with shipping is: " using the same totalPrice()?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall have to guess what you mean about the total price but let's see if I can't write something similar:-
 
Swaraj Pal
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ryan,
Yes you can do it for sure, infact that is what overriding means, for sake of polymorphism(many forms, that is what you saying).

If I understand your question correct , you are asking for this,
Overriding is used when you have to use same method name for different purpose in different classes(in inheritence ofcourse).

okay so you can do follwing



and its subclass as




So you can execute following instances



So i guess it is helpful to you.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swaraj Pal wrote: . . .
Overriding is used when you have to use same method name for different purpose in different classes(in inheritence ofcourse).
. . .
I think it means you are using the same method name for the same purpose in different classes. The difference is not simply because of the different methods, but because of the different classes.The overridden methods are not different because they are used for different purposes; they are used for the same purpose. They are different because dogs cats and lions make different noises.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you. You guys really explain things in a clear way where I see it immediately. I really appreciate it.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Swaraj Pal wrote: . . .
Overriding is used when you have to use same method name for different purpose in different classes(in inheritence ofcourse).
. . .
I think it means you are using the same method name for the same purpose in different classes. The difference is not simply because of the different methods, but because of the different classes.The overridden methods are not different because they are used for different purposes; they are used for the same purpose. They are different because dogs cats and lions make different noises.

Can we override the main() method??
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!