The Artima Developer Community
Sponsored Link

Java Answers Forum
Inheritance question

3 replies on 1 page. Most recent reply: May 26, 2005 7:16 AM by barron

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
rassuls

Posts: 10
Nickname: rassuls
Registered: Mar, 2003

Inheritance question Posted: May 18, 2005 8:59 AM
Reply to this message Reply
Advertisement
Hi,
The following two java programs Test1.java and Test2.java use 'this' keyword. In Test1.java I use 'this' to call method() and in Test2.java I use 'this' to call the class varible 'x'. The answers are at the end of each program. I expect to get the result for Test2.java as:
2
2
as the same order as in Test1. What is going on? Thanks in advance for your help. Russell
class A
{
    public A() { this.method(); }
    void method() { System.out.println("method in A"); }
}
/**********************************************************/
class B extends A
{
    public B() { this.method(); }
    void method() { System.out.println("method in B"); }
}
/**********************************************************/
public class Test1
{
    public static void main(String[] args)
    {
        new B();
    }
}
/*
method in B
method in B
*/

class A
{
    int x = 1;
    public A() { System.out.println(this.x); }
}
/************************************************/
class B extends A
{
    int x = 2;
    public B() { System.out.println(this.x); }
}
/************************************************/
public class Test2
{
    public static void main(String[] args)
    {
        new B();
    }
}
/*
1
2
*/


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Inheritance question Posted: May 18, 2005 3:47 PM
Reply to this message Reply
1. When you redefine an instance variable (defining x in class B in your case) then it hides the definition of inherited variable with same name.

2. Same thing happends when you redefine a static method in descendant class.

The reference to hidden variable or method is resolved at compile time and not at run time. So, this.x in class B refers to hidden variable x = 2 in class B. However, inside class A, variable x refers to x in class A.

In first case, where you are calling method() from constructor, it is a case of overriding. When you redefine an instance method then it is a case of overriding and not hiding. However, redefining an instance variable is a case of instance hiding and not overriding.

In case of overriding, the reference to method() call is resolved at run time. Since you are saying new B(), method() in class B is called in both cases. It is so, because "this" in class A and class B construtor is referring to an instance of class B.

Change the method() definition and make it static in both class A and class B. This time you will get the result same as in case of variable x being hidden. This time, method() will be hidden and its reference will be resolved at compile time.

barron

Posts: 13
Nickname: toniblair
Registered: May, 2005

Re: Inheritance question Posted: May 26, 2005 6:41 AM
Reply to this message Reply
why is the super class constructor being called? he is overriding the default constructor and not calling super()??

barron

Posts: 13
Nickname: toniblair
Registered: May, 2005

Re: Inheritance question Posted: May 26, 2005 7:16 AM
Reply to this message Reply
nevermind hehe...
i don't know what i was thinking...

Flat View: This topic has 3 replies on 1 page
Topic: Moving a JinternalFrame out of a JFrame Previous Topic   Next Topic Topic: error while compiling Servlet class as:package  javax.servlet does not exis

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use