[RESOLVED] inheritance - a beginner's question
Ello,
If class "widget", extends "something", is it possible to create an instance of something then use it it as a widget?
Here's the code I'm trying to get to work:
VB Code:
class universe
{
public static void main(String[] args)
{
something whoKnows;
whoKnows = new widget();
System.out.println(whoKnows.getWeight());
}
}
class something
{
public int weight=2;
}
class widget extends something
{
public int getWeight()
{
return weight;
}
}
Output:
Quote:
C:\JAVA\INHERI~1>javac universe.java
universe.java:8: cannot find symbol
symbol : method getWeight()
location: class something
System.out.println((widget)whoKnows.getWeight());
^
1 error
Thanks!
Re: inheritance - a beginner's question
Quote:
Originally Posted by Evil_Cowgod
If class "widget", extends "something", is it possible to create an instance of something then use it it as a widget?
No, that would be backward inheritance :)
In an inheritance chain, if extending a class is going downwards, classes are "upwards" compatable, but not downwards, if you see what I mean. So if widget extends something you can use a widget instance as a something instance but not the other way round.
If you override a something method in widget then you may alter the behaviour, but you can't remove a method from a class.
Re: inheritance - a beginner's question