• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

subclass member method called in superclass constructor

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test extends A{

public static void main(String [] args){
Test t = null;
try{
t = new Test();
}catch(Exception e){
System.out.println("Object t = "+t);
}
}

void test(){
System.out.println("test Test");
}
}
class A{
A(){
test();
if(true){
throw new NullPointerException("null");
}
}
void test(){
System.out.println("test A");
}
}

Output:
test Test
Object t = null

In the above program, constructor of super class A is calling a method test() and throwing NullPointerException hence construction of object of subclass Test is not possible. But then how come overridden member method test() of subclass Test is called before its object is created successfully?
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But then how come overridden member method test() of subclass Test is called before its object is created successfully?



That is how it is specified. It works even during construction -- and you have to be careful because of this.

Henry
 
prakash chandra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its clear that the the run time object's method will be called. But in the code above the super class object creation and subclass object creation is not yet complete. So during run time how come the subclass method is called ( the object itself is not yet created) .

If we assume that ok.. the method will be called.... then how come the test() method of superclass A is not called... (the subclass method test() is called ) . Because from superclass constructor , we are calling the method test() .
 
Henry Wong
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prakash chandra wrote:If we assume that ok.. the method will be called.... then how come the test() method of superclass A is not called... (the subclass method test() is called ) . Because from superclass constructor , we are calling the method test() .



For the exact same reason in every other case, the overriden method is always called. Why do you expect the mechanism to behave differently during construction? Doesn't it confuses things if the behaviors change?

Henry
 
prakash chandra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.. got it...

the only thing i missed was that the overridden concept stands true during object construction also. Hence the overriden method will be called even if its object creation is still in process. thanks Henry....
 
reply
    Bookmark Topic Watch Topic
  • New Topic