简体   繁体   中英

How to access the attributes of an object instantiated via a constructor in an abstract class, from an overridden method in a concrete child class?

I'm new to OOP and am working my way through an exercise in which I'm provided an abstract class and asked to implement a concrete subclass, while also modifying the abstract class by adding hidden state.

public abstract class someAbstractClass{

    /* Constructor */
    public New(String a, String b){
        this.a = a;
        this.b = b;
    }

    /* Abstract Getter 1 */
    public abstract String geta();

    /* Abstract Getter 2 */
    public abstract String getb();

}

public class concreteClass extends someAbstractClass{

    /* Constructor */
    public New(String a, String b){
        super(a,b);
    }

    /* Implementation of Getter 1 */
    @Override
    public String geta(){
        // ???
    }

    /* Implementation of Getter 2 */
    @Override
    public String getb(){
        // ???
    }
}

The above is a simplification of the two classes. What I am unsure of is how can I define the two getter methods in my concrete child class, if the methods are abstract in the superclass? More specifically, how can I return the attributes of the object, if I've used the superclass constructor to instantiate the object.

I cannot simply say something like return super.a or return this.a - when I try to do so, the compiler complains that it cannot find a . If I were to have a non-abstract method in the abstract class, I could reference that method in my concrete (child) class, and access the attributes I'm trying to access. Surely I'm overlooking something very banal. Thank you all in advance for any shared wisdom.

I have made some corrections to answer your question.

  • Names of classes start with capital letters (its a convention)

  • If the abstract class references a and b with this , it also has to declare those variables as members of the class.

  • The constructor is defined with the same name of the class.

When you extend a class, the extended class inherits the member variables of the super class as its own. So to reference a and b from the inherited class you can just write a, b, or this.a, this.b ( this referencess the instance of the inherited class from withing itself).

If the inherited class will be able to just read the value or change it will depend if it was declared as public, private, or (the default) protected in the super class (the abstract class in this case)

public abstract class SomeAbstractClass{
    protected String a;
    protected String b;

    /* Constructor */
    public SomeAbstractClass(String a, String b){
        this.a = a;
        this.b = b;
    }

    /* Abstract Getter 1 */
    public abstract String geta();

    /* Abstract Getter 2 */
    public abstract String getb();

}

public class concreteClass extends someAbstractClass{

    /* Constructor */
    public New(String a, String b){
        super(a,b);
    }

    /* Implementation of Getter 1 */
    @Override
    public String getA(){
        return this.a;
    }

    /* Implementation of Getter 2 */
    @Override
    public String getB(){
        return this.b;
    }
}

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