Home > Archive > Java Help > March 2004 > Inheritance question.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Inheritance question.
|
|
| Zalek Bloom 2004-03-27, 12:30 am |
| I have a problem understanding inheritance. Here is the code:
package test4;
public class super_class {
String a = "super_class variable";
void m(){
System.out.println("This is a super_class with variable: " + a);
}
}
package test4;
public class sub_class extends super_class{
String a = "sub_class variable";
void m(){
System.out.println("This is a subclass with variable: " + a);
}
}
package test4;
public class test {
public static void main(String[] args) {
super_class sub = new sub_class();
sub.m();
System.out.println("sub.a: " + sub.a);
}
}
the result is:
This is a subclass with variable: sub_class variable
sub.a: super_class variable
Why the method m() prints the value of the member variable "a" as "sub_class
variable", but sub.a prints it as "super_class variable"?
Thanks,
Zalek
| |
| Johann Weber 2004-03-28, 12:01 am |
| Zalek Bloom wrote:
> I have a problem understanding inheritance. Here is the code:
>
> package test4;
> public class super_class {
> String a = "super_class variable";
> void m(){
> System.out.println("This is a super_class with variable: " + a);
> }
> }
>
> package test4;
> public class sub_class extends super_class{
> String a = "sub_class variable";
> void m(){
> System.out.println("This is a subclass with variable: " + a);
> }
> }
>
> package test4;
> public class test {
> public static void main(String[] args) {
> super_class sub = new sub_class();
> sub.m();
> System.out.println("sub.a: " + sub.a);
> }
> }
>
> the result is:
>
> This is a subclass with variable: sub_class variable
> sub.a: super_class variable
>
>
> Why the method m() prints the value of the member variable "a" as "sub_class
> variable", but sub.a prints it as "super_class variable"?
>
> Thanks,
>
> Zalek
Hi!
The point is, that you cannot override variables but only methods of a
super class.
When accessing the variable a of object sub (which is declared as a
super_class object!) you get the a which is member of super_class. When
calling m(), which is overridden in sub_class, the sub_class::m() method
is invoked - polymorphism as expected.
The sub_class::m() method accesses the a declared in sub_class. Inside
sub_class::m() or any other non-static method of sub_class you can
access super_class::a by using super.a.
Regards, Johann
| |
| VisionSet 2004-03-28, 12:01 am |
|
"Johann Weber" <J.Weber@gmx.net> wrote in message
news:c43gei$v1b$1@online.de...
>
> The point is, that you cannot override variables but only methods of a
> super class.
Yes you can
class Sup {
int i = 5;
}
class Sub {
{
i = 9;
}
}
--
Mike W
| |
| Johann Weber 2004-03-28, 12:01 am |
| VisionSet wrote:
> "Johann Weber" <J.Weber@gmx.net> wrote in message
> news:c43gei$v1b$1@online.de...
>
>
>
>
> Yes you can
>
> class Sup {
> int i = 5;
> }
> class Sub {
> {
> i = 9;
> }
> }
>
> --
> Mike W
>
>
Hi!
I cannot see any overriding in your code. What you will get compiling
this code is an error like "i cannot be resolved" in line "i = 9;".
But lets assume Sub extends Sup:
class Sub extends Sup { { i = 9; } }
This has nothing to do with "overriding", since overriding is a
mechansim which allows you to reimplement an inherited method in a
derived class.
Your code shows an access to i which is defined in Sup (assuming that
Sub extends Sup) and inherited by Sub.
Have a look at http://java.sun.com/docs/books/tutorial/index.html,
inheritance is covered in
http://java.sun.com/docs/books/tuto...aOO/index.html.
Regards, Johann
| |
| VisionSet 2004-03-28, 12:01 am |
|
"Johann Weber" <J.Weber@gmx.net> wrote in message
news:c44fut$drb$1@online.de...
> VisionSet wrote:
>
> Hi!
>
> I cannot see any overriding in your code. What you will get compiling
> this code is an error like "i cannot be resolved" in line "i = 9;".
>
> But lets assume Sub extends Sup:
> class Sub extends Sup { { i = 9; } }
>
> This has nothing to do with "overriding", since overriding is a
> mechansim which allows you to reimplement an inherited method in a
> derived class.
>
> Your code shows an access to i which is defined in Sup (assuming that
> Sub extends Sup) and inherited by Sub.
>
It basically achieves the same and yes I missed the extends out.
The value of i depends on what class the object is typed as at runtime in
much the same way as a method is chosen.
--
Mike W
| |
| Johann Weber 2004-03-28, 12:01 am |
| VisionSet wrote:
> "Johann Weber" <J.Weber@gmx.net> wrote in message
> news:c44fut$drb$1@online.de...
>
>
>
> It basically achieves the same and yes I missed the extends out.
>
> The value of i depends on what class the object is typed as at runtime in
> much the same way as a method is chosen.
>
Hi!
I think the mechansim you are talking about is called "hiding member
variables".
Once again from
http://java.sun.com/docs/books/tuto...O/subclass.html
"
Subclasses don't inherit a superclass's member if the subclass declares
a member with the same name. In the case of member variables, the member
variable in the subclass hides the one in the superclass. In the case of
methods, the method in the subclass overrides the one in the superclass.
"
Regards, Johann
| |
| A.Buschmann 2004-03-28, 12:01 am |
| Hi,
The problem is, that overriding variables does not work in the same way
as overriding methods.
When a method gets overwritten, the compiler generates extra-source, so
that the JVM can find the correct method-implementation.
When a variable gets overwritten, it simply becomes invisible in the
Sub-Class.
This means:
When the JVM knows, that the variable is in the Sub-Class, it will use the
correct variable (Here: "sub_class variable").
When calling a method, the compiler knows implicity, in which Class to
search
for the method-code (see above). So, it finds the code for accessing the
correct
variable (to the compiler the actual name of a variable means nothing). In
other
OO-Languages, this is done by explicitly marking the method as "virtual". In
Java,
every method is virtual, so the JVM is calling the right code without any
work done
by the programmer.
When accessing a variable, the JVM does not have the information to find the
correct implementation of the class, so it looks in the Class, that seems to
be
the right class (defined through the variable-declaration in class test).
The variable "sub" looks as if it was an instance of the class
"super_class", so
the JVM fetches the variable of the super_class.
To get the correct variable, an explicit type-cast is necessay.
This code would give the correct answer:
package test4;
public class test {
public static void main(String[] args) {
super_class sub = new sub_class();
sub.m();
System.out.println("sub.a: " + ((sub_class)sub).a);
} ^^^^^^^^^^
}
This tells the JVM, which class-implementation should be used here, and the
output is:
This is a subclass with variable: sub_class variable
sub.a: sub_class variable
regards
A. Buschmann
| |
| VisionSet 2004-03-28, 12:01 am |
|
--
Mike W
"Johann Weber" <J.Weber@gmx.net> wrote in message
news:c44iah$nt5$1@online.de...
> VisionSet wrote:
>
in[color=darkred]
>
> Hi!
>
> I think the mechansim you are talking about is called "hiding member
> variables".
>
> Once again from
> http://java.sun.com/docs/books/tuto...O/subclass.html
>
> "
> Subclasses don't inherit a superclass's member if the subclass declares
> a member with the same name. In the case of member variables, the member
> variable in the subclass hides the one in the superclass. In the case of
> methods, the method in the subclass overrides the one in the superclass.
> "
Hiding is acheived by the following code in which the variable i is a
completely separate variable
class Sup {
int i = 5;
}
class Sub extends Sup {
int i = 9;
}
But this isn't the same at all as:
class Sup {
int i = 5;
}
class Sub extends Sup {
{i = 9;}
}
In the latter Sub is inheriting i.
Do your own tests and see :)
--
Mike W
| |
| Johann Weber 2004-03-28, 10:36 pm |
| VisionSet wrote:
> The value of i depends on what class the object is typed as at runtime in
> much the same way as a method is chosen.
>
I disagree with this, and I think this is what the original question was
about : the difference between overriding a method an - let's say -
overriding (or hiding) a data member.
In my understanding the JVM choses at runtime which method
implementation is called (SuperClass.method() or SubClass.method())
while access to a data member is determined by the compiler at
compiletime (SuperClass.data or SubClass.data). So, whenever the
compiler finds
SomeClass c;
// ... creating c, possibly of derived class
c.d;
the compiler will check if class SomeClass has an accessible data member
named d. If not, it will not compile, if yes, it always will mean data
member d of class SomeClass, no matter what type c is created of.
Regards, Johann
| |
| VisionSet 2004-03-28, 10:36 pm |
| "Johann Weber" <J.Weber@gmx.net> wrote in message
news:c46u21$bsf$1@online.de...
> VisionSet wrote:
in[color=darkred]
>
> I disagree with this, and I think this is what the original question was
> about : the difference between overriding a method an - let's say -
> overriding (or hiding) a data member.
>
> In my understanding the JVM choses at runtime which method
> implementation is called (SuperClass.method() or SubClass.method())
> while access to a data member is determined by the compiler at
> compiletime (SuperClass.data or SubClass.data). So, whenever the
> compiler finds
>
> SomeClass c;
> // ... creating c, possibly of derived class
> c.d;
>
> the compiler will check if class SomeClass has an accessible data member
> named d. If not, it will not compile, if yes, it always will mean data
> member d of class SomeClass, no matter what type c is created of.
Yes that is right, but there is only ever one chosen method or one selected
value for a variable at a particular time even at runtime. Only one method
will be executed and the variable will only take on one value. So what's
the difference in reallity? With the variable example I gave I suppose int
i is initialised to 5 before being changed to 9 (if the type is Sub),
whereas the correct method is selected instantly. But this is a variable it
doesn't need as refined a treatment as a method.
--
Mike W
|
|
|
|
|