Results 1 to 6 of 6

Thread: [Resolved] Inheritance question ...

  1. #1

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    [Resolved] Inheritance question ...

    If I decide to subclass the Frame class, and then write code like this:

    Code:
    //All the necessary import statements here
    public class frm extends Frame implements WindowListener
    {   frm()
         {  super("My Frame");
         }
         public void windowClosed(blah blah)
         {}
         //further blah blah to override the WindowListener methods
    }
    And in my applet I write this:
    Code:
    //All necessary import statements
    public class MyApplet extends Applet
    {   Frame f;
         public void init()
         {   f = new frm();
         }
         //some more blah blah
    }
    In the applet code, I am declaring a reference to the Frame class, which is parent to the frm class. But when I instantiate it, I am creating a new object of the frm class and not the Frame class. Is this allowed?

    We were taught this yesterday, the teacher says it works OK, and I have yet to test it out. The teacher said it works because the child class frm does not have its own public methods, so its public interface is not different from the parent Frame class.

    Is the above code right? Can you use a parent class pointer to point to an object of the child class?

    Also a little off the topic, does the Frame class implement WindowListener? I don't think so. If it does not, the overridden methods from the WindowListener interface in the frm class do constitute the public interface of the frm class, right? And that means the public interface of the frm class is different from that of the Frame class, because there are public void windowClosed and other methods in frm which are absent in Frame.

    Can someone help me understand this?

    .
    Last edited by honeybee; Oct 16th, 2001 at 07:36 AM.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Sorry i didnt read all of your post. Im bad like that.
    But to try and answer some of your questions, yes you can
    either use the WindowListener interface or subclass WindowAdapter. For low level events subclassing an Adapter class is the way to go becuase then all you have to do is provide implementation for the desired method. You other question "Can you use a parent class pointer to point to an object of the child class?" This is one of the questions that used to stump me becausing of it being at the hart of Object Oriented Programming.
    As far as i understand it you can use a Super or "parent" class in place of a subclass or "child" class. An implicit widening conversion is performed. Or what is commonly called upcasting up the in inheritance hierarchy. I think the same holds true for a
    class that implements an interface. The interface can be used in place of the class. For instance List l = new LinkedList();

  3. #3

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    I am sorry, but you really seem to have missed the point!

    The point is I am declaring an object reference of the parent class Frame, and when I instantiate it, I make it to point to a child class object frm(). Is this valid?

    The stuff on WindowListener was there just to say that the frm class does have public methods which are absent in Frame class, so the public interface of the parent and child are not the same.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    The answer is yes. I'm too tired right now to give a whole big explanation of why , but, I can tell you that its all about the "is a" relationship.

    If class Frm (must be capitalized ) extends class Frame, then a Frm object is a Frame object. Just like every object is an Object object.

    You can do the same thing with Collections:
    Code:
    Collection c = new List();
    that is allowed because a List is a Collection. This technique would mostly be used for polymorphism.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Thanks for the info. Actually as per the inheritance rules/conventions, I think a pointer to the child class can point to a parent class object, but not vice versa, and that's what confused me. I shall try it out sometime today or morrow and see how it goes.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Exactally. That what i was getting at in the second part of my explanation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width