• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object instantiation inside a superclass  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a pretty basic question. Check out this code:



Does the instantiated object exist within SubPane? Or Should I be instantiating it every time within SubClass? Thanks in advance for any advice on this.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Acording to Oracles' Java Docs it is clear that.."A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass."

Similarly goes for the objects. A subclass does not directly inherits the instances of its superclass.

Also according to Java Docs: "Every class we make is a subclass of the Class Object. Still we have to define any methods that we are going to use in that particular class"

I hope its helps....
Regards
 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So here's what I don't understand. The subclass will recognize the instance 'frame', but all of the methods won't take. For example, the subclass will be undecorated (as called in the superclass), but the SetLocationRelativeTo() won't apply to the subclass. Why some and not others? And why does it recognize 'frame' as a valid object? Thanks again.
 
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The subclass does not recognize frame. It's neither a member of SubPane nor a local variable of SubPane's constructor. Line 27 should not compile because frame is undeclared in SubPane.

A question the need to create a frame inside a panel's constructor. Why does the panel need to know about the frame? Why not add the panel to the frame in another class that acts as a controller?

Whatever you do, NEVER call pack() or setVisible() in a constructor or a method called from a constructor. These methods are able to start new threads, and you should not start threads inside a constructor call.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!