简体   繁体   中英

Sub using super's instance method on its own field members

Suppose i have the classes SUPER and SUB where SUB extends SUPER . Both are concrete classes.

There is an instance method, say m1() of SUPER . SUPER does some basic calculations using the field members in that method m1() . SUB wants to use those SUPER is doing on its own field members-- and add some more on top of it.

When i'm overriding m1() in SUB , I first can invoke super.m1() . However, this invocation runs super.m1() on the field members of the object of SUPER -- not SUB . What i need is, super.m1() running on SUB 's members so that i don't have to repeat that code in the method i'm overriding. Then add more-- those further calculations that i want to do on SUB 's members in the m1() method of SUB .

So - in the code below, i'm looking to get the effect of calculation in "line-K" on the member x of some so that the printed result will show 661 instead of 67:

class AAA  {    
    int x=55;
    void m1 () { 
        x*=10; // line-K
//        System.out.println("in m1() of SUPER "+x); 
    } 
}

class NewClass extends AAA  {
    int x = 66; 
    void m1 () {
        super.m1(); 
        x++; 
        System.out.println("in m1() of SUB itself..............."+x); 
    } 

    public static void main(String[] args) throws CloneNotSupportedException {
        NewClass c = new NewClass();        
        c.m1();
    }
} 

Can this be done by overriding m1() in some way without repeating that code in super.m1() ?

How?

TIA.

//=========================================

EDIT:

I can pass x as a parameter to the method and get it returned-- by making m1() take a param & return a value, thus get done what I need. But that's not what i'm wondering about.

The concept of declaring a member in subclass with the same name as a member in the superclass is called hiding .

If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class.

(There is hiding for methods and for fields.)

Fields are not polymorphic. In this case, the super class does not know about the member declared in the sub class and it definitely should not. That would break encapsulation.

Can this be done by overriding m1() in some way without repeating that code in super.m1() ?

No. The solution you've proposed with an extra parameter seems appropriate.

Here's the standard way to get what you want:

static class AAA  {    
    int x=55;
    void m1 () { 
        setX(getX()*10); // line-K
    } 
    int getX() { return this.x; }
    void setX(int x) { this.x = x; }
}

static class NewClass extends AAA  {
    int x = 66; 
    void m1 () {
        super.m1(); 
        setX(getX()+1); 
        System.out.println("in m1() of SUB itself..............."+getX()); 
    } 
    @Override
    int getX() { return this.x; }
    @Override
    void setX(int x) { this.x = x; }

}     
public static void main(String[] args)
{
    NewClass c = new NewClass();        
    c.m1();
}

Instance variables are not virtual but methods are.

HOWEVER, hiding instance variables in a subclass is almost never a good idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM