DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    May 2006

    Class Inheritance

    I have a super class: SuperClass (for simplicity).

    I also have 2 classes that sublcass this SuperClass: SubclassA and SubclassB.

    Assuming I have the following method:
        public SuperClass getObj(String name, Date create){
               SuperClass obj;
               if(name == "blah"){
                    return obj = new SubclassA();
               //if create date after some other date
               if(create.After(new Date(1232341324) ){
                    return obj = new SubclassB();      
               return obj = new SuperClass();
    Based on this, will all the objects ONLY be SuperClass objects and only available to use SuperClass methods, etc?
    Or, if the first or second if statement is executed, the returned object from this method will have access to the methods defined in the subclass?


  2. #2
    Join Date
    Dec 2004
    San Bernardino County, California
    You're attempting to traverse in the wrong direction on a class hierarchy. Derived (sub) classes are instances of the base (parent or super) class, but the parent cannot be an object of the derived class.

    You can address an object of the derived class as a parent object, but not vice-versa.

    So, in answer to your questions, the SuperClass objects will only be able to use SuperClass methods ... a SuperClass object will only look in the method table for the SuperClass and does not have access to the method table (virtual table/Class Instance Record) for a SubClass: therefore, it has no access to the fields and methods of the SubClass.

    You are attempting to create a system which is addressed by the "Strategy" Design Pattern ... research this to modify your design.
    Last edited by nspils; 01-07-2007 at 03:19 PM.

Similar Threads

  1. Replies: 27
    Last Post: 02-11-2016, 05:29 AM
  2. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  3. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  4. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  5. Replies: 5
    Last Post: 10-17-2002, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.