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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in overriding  method Subclass referance " Watch "Doubt in overriding  method Subclass referance " New topic
Author

Doubt in overriding method Subclass referance

Deepika Namasani
Greenhorn

Joined: May 19, 2005
Posts: 25
I feel that output for this code should be

InQuestion30Super class version
InQuestion30

but the output i get when i execute this program is

InQuestion30Super class version
InQuestion30Super class version

In overriding subclass verision should work .

but how is this different with this program
suppose eat() is overriden in both Animal and Horse(where Horse extends Animal)
Animal a = new Animal();
a.eat()//should run Animal version of eat

(where Horse extends Animal)
Animal h = new Horse();
h.eat()//should run Horse version of eat
---------------------------------------------------------
applying the same rule i feel the output


q31.printSomething();
// should print super class version ie "InQuestion30Super class version"
q30.printSomething();//
//should print super class version ie "InQuestion30"





pls kindly explain

thanks

deepika


Deepika N
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Overridding don't apply to static methods. Remove the static keyword from the print methods, and it should work as expected.

BTW, I am surprised that the compiler didn't complain that you are using an instance to call a static method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
I have a confusion about these static methods.

1) Can we override a static method?
2) If we can, does the static method in the super class run when the subclass is loaded?
3) Even if a static method in the super class is not overridden in a subclass, when the subclass is loaded, does it invoke the static methods in the super class?
4) When an instance of a sublass is made, does it implicitly make an instance of the super class too?

I would be really grateful if someone can explain me the theoretical background of these. If I can get the fundamental understanding of what happens when a subclass is loaded/instantiated to the super class of it, I can solve a lot of problems myself.

Thank you very much.. You guys are great.. :-)
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
I have a confusion about these static methods.

1) Can we override a static method?
2) If we can, does the static method in the super class run when the subclass is loaded?
3) Even if a static method in the super class is not overridden in a subclass, when the subclass is loaded, does it invoke the static methods in the super class?
4) When an instance of a sublass is made, does it implicitly make an instance of the super class too?

I would be really grateful if someone can explain me the theoretical background of these. If I can get the fundamental understanding of what happens when a subclass is loaded/instantiated to the super class of it, I can solve a lot of problems myself.

Thank you very much.. You guys are great.. :-)
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
1) Can we override a static method?
static members are not inherited and hence no overriding only hiding.

4) When an instance of a sublass is made, does it implicitly make an instance of the super class too?
No only the class is loaded for the first time and constructor is called everytime the child class is instantiated


Thnx
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
Originally posted by Henry Wong:
BTW, I am surprised that the compiler didn't complain that you are using an instance to call a static method.

Henry


Henry, y will the compiler complain !.. coz when u declare something as static, it statest that only Class reference is needed to invoke the static thing. But if u provide the instance , the compiler shud be happy enough coz u have not just given the reference u have given the whole instance to it

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks Nischal for the reply.

I have a question.

If a subclass has a constructor, when that subclass is instatiated with, does the compiler call the constructor of the superclass too?

thanks..
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580

If a subclass has a constructor, when that subclass is instatiated with, does the compiler call the constructor of the superclass too?


yes


If a subclass has a constructor

classes always has constructor(s).

[ May 26, 2005: Message edited by: vidya sagar ]
[ May 26, 2005: Message edited by: vidya sagar ]
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

When the subclass is instantiated, it's superclass constructor is always called. Even the constructor of super class is executed before the codes in the subclass constructor. It the constructor of the subclass is not defined, the compiler adds its default constructor which simply call the constructor of superclass.

Question30Super q31 = new Question30Super();
Question30Super q30 = new Question30();


In your example, the Objects are created for different classes. But the references are of the type Superclass. As the methods call to static methods it will always called for the superclass.

If you change the second line as
Question30 q30 = new Question30(), then it will call the static method of subclass. or you can try ((Question30)q30).yourmethod(); It should claa the proper method.

Thanx


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Henry,

Are you sure the compiler would complain about calling a static method with an instance? I know some IDEs notice and point it out as it is bad style, but I thought the compiler would let it go.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
I don't think the compiler will complain about calling a static method with a reference variable.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
I have a confusion about what Narendra has said. Please explain me the difference:
If class "Sub" is extended from class "Base":
If i declare;

Base B1 = new Sub();
Sub B2 = new Sub();

What is the difference when I call methods from B1 and B2? Does it only affect when calling static methods?

If I override a method in Sub, can I call that method from B1?

If I have a method in Sub, which is not defined in Base, can I call that method from B1?

If I override a static method in Sub, can I call that static method from B1? or is it only from B2 that I can call it?

Please answer. I am really confused?
Thanks a lot guys..
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
B1 has only the methods declared in Base, so yoy cannot use B1 to invoke new methods declared in Sub.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287

Base B1 = new Sub();
Sub B2 = new Sub();

What is the difference when I call methods from B1 and B2? Does it only affect when calling static methods?

If I override a method in Sub, can I call that method from B1?

yes


If I have a method in Sub, which is not defined in Base, can I call that method from B1?

no, you are gonna have compiler error


If I override a static method in Sub, can I call that static method from B1? or is it only from B2 that I can call it?

static method cannot be overriden. If you call that static method from B1, you will get result from method in B1; if you call the same (overriden) static method from B2, you will get result from method in B2. Overriding rule won't apply here.


SCJA, SCJP5.0, SCBCD, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in overriding method Subclass referance