File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in overriding method Subclass referance

 
Deepika Namasani
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nischal Tanna
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 951
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Timmy Marks
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the compiler will complain about calling a static method with a reference variable.
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B1 has only the methods declared in Base, so yoy cannot use B1 to invoke new methods declared in Sub.
 
reubin haz
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic