• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Object instantiation inside a superclass

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13366
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic