aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding" Watch "overriding" New topic
Author

overriding

ganga prabhu
Greenhorn

Joined: Sep 29, 2005
Posts: 10
Someone plz clear this doubt



When the overriding method is commented ...how it is printing super as output

my friend told that whenever a class is inherited and method in it is not overridden same code is inlined in derived class..

on the basis of it when variable is looked it should print sub only but how super is displaying

Anybody explain little bit clearly
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
It's true to say that methods are inherit in derived classes, not that "same code is inlined in dervived classes".

Here the method getDesc() is called from Super, and variable d in getDesc() refer to variable d in Super.

Note that variables are not overriden, but only shadowed. (It can be a good practice to not use same variable names in sub-classes as in super classes).

Change the name of variable d in Sub, maybe it can help to understand.


Regards,<br />Seb<br /> <br />SCJP 1.4
Ishan Bhatt
Greenhorn

Joined: Oct 09, 2005
Posts: 14
If you comment the overridden method it means that you are not overridding.
So, obviously it will call the method in the super class. And for the super class "d" means "super".
Hence the o/p.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312

Super s = new Sub();System.out.println(s.getDesc());


s is have refernce to Super that d as same as s.d ( reference to Super Class )


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
ganga prabhu
Greenhorn

Joined: Sep 29, 2005
Posts: 10


hi somkiat

According to ur words

call to method1 should refer super class method1 but it is refering sub class method1(still we have base class references for that object)

why this happening
Kris Krason
Greenhorn

Joined: Aug 05, 2005
Posts: 25
Your code will not compile.

You can't call static method from non-static one.
If you had a call to method1() from main(), then it should print the superclass string.


Kris Krason<br />SCJP1.4 (96%)<br />SCJP1.5 (91%)<br /> <br />See my <a href="http://krisreviews.com/2006/12/code-complete-second-edition/" target="_blank" rel="nofollow">Code Complete review</a><br /> <br /><a href="http://www.coderookie.com/2006/tutorial/the-pseudocode-programming-process/" target="_blank" rel="nofollow">Pseudocode Programming Process</a>
ganga prabhu
Greenhorn

Joined: Sep 29, 2005
Posts: 10

You can't call static method from non-static one.


We can call static method from non-static method
but we cannot call non-static method from static method(without having object of the class)
Kris Krason
Greenhorn

Joined: Aug 05, 2005
Posts: 25
Oops, you are right.
It wouldn't make sense the other way
ganga prabhu
Greenhorn

Joined: Sep 29, 2005
Posts: 10
ok anybody clear my 2nd post doubt in this topic
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,
overriding is resolved at Runtime.
Since your Runtime object is over1(subclass), and also the getD method is overriden in over1 class, so it calls over1's (subclass) getD method. Further the getD method calls method1 which is shadowed in over1(subclass) so method1 of over1 is called which prints: "Sub Class"

Hope I am not wrong


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding
 
Similar Threads
decorator pattern from HFDP
Problem with instance initializer
Overriding of final variables Vs final method