|
-
Oct 15th, 2001, 04:53 AM
#1
Thread Starter
Randalf the Red
[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.
-
Oct 15th, 2001, 11:23 AM
#2
Dazed Member
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();
-
Oct 15th, 2001, 09:51 PM
#3
Thread Starter
Randalf the Red
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.
.
-
Oct 15th, 2001, 11:55 PM
#4
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Oct 16th, 2001, 01:27 AM
#5
Thread Starter
Randalf the Red
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.
.
-
Oct 16th, 2001, 12:20 PM
#6
Dazed Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|