2

I have two classes:

abstract class A 
{
    private int a;
    protected int get(){return a;}
}

class B extends A
{
    private int a = 2;
}

What i want is to inherit get() method which would give me the result 2 instead of 0. I know why this code is not working but have no idea is it even possible to make this kind of trick in java.

4
  • 1
    Curious, why would you want to do this? Is it just a learning exercise? IRL, name "collisions" like this are often something to consider avoiding, but only because anyone reading the code later might get confused. Commented Mar 11, 2015 at 13:06
  • 1
    i Have abstract class Effect which is the base of DmgEffect, StunEffect etc. with several variables like duration, value etc. I thought I could write so much less code if it would be possible. Commented Mar 11, 2015 at 13:50
  • 1
    I suspect that you're overlooking something if you believe that to be the case. Member variables are never polymorphic in nature. If you attempt to exploit the fact that a subclass can actually hide the access to an otherwise accessible field, then you'll eventually end up in trouble....the exhausted engineer at 3am hopped up on caffeine might well not catch what you're doing. In your example, you have two private variables, so they're externally hidden already, but you still run the risk of confusion. It's just not something that seasoned OO engineers are accustomed to dealing with. Commented Mar 11, 2015 at 16:54
  • See @magnamag's answer for a far more acceptable solution, AND one that engineers would expect to encounter. Commented Mar 11, 2015 at 16:59

3 Answers 3

3

If you don't want to override your get() method, you could pass the value to your a attribute in the constructor:

abstract class A {
    private int a;

    protected A(int a) {
        this.a = a;
    }

    protected int get() {
        return this.a;
    }
}

class B
    extends A {

    B() {
        super(2);
    }

    public static void main(String[] args) {
        B b = new B();
        System.out.println(b.get()); // 2
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Just for my knowing - Since we did not instantiated any object of A can we set a value using constructor to A? Here you assign 2 using super(2) to a.
@Razib A is abstract, so you cannot instantiate any instance of A, but only of its non-abstract subclasses. Regarding your question: yes, you can pass any arguments in the constructor and assign them to the attributes, as long as there exists a constructor that actually accepts those arguments (that's why I created a protected constructor in A that accepts an int).
@hejcz I'm glad my answer was of help to you.
3

You can override get() in class B :

class B extends A
{
    private int a = 2;

    @Override
    protected int get() {
        return a;
    }
}

This way, calling get() on an instance of class B would return the a member of B.

4 Comments

Well, I know. I was wondering is there a way to just produce less code as I have a variable in both classes. ( so I'm looking for the way of resolving it without actually making get() method in class B - I just want to inherit fully functionally get() from A class).
you can use super keyword
@hejcz A's get() would always return A's a member, since you can't override a member. If you want to return B's a member, you must override get().
I'm sorry, but I have to reiterate that this falls in the category of "crummy technique". See @Magnamag's answer.
0

If you an accessor such as getA() you can use get() with all of it's functionality as long as any code inside the definition of get() uses getA() to get the value of a instead of just using a.

Small example:

abstract class A 
{
    private int a;

    protected int get(){
        // do anything with getA(), here just return the value as in your question.
        return getA();
    }

    public int getA(){ return a; }
}

class B extends A
{
    private int a = 2;

    @Override
    public int getA(){ return a; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.