2

I have these two classes:

Class A 
{
  public int max = 100;

  public int getMax()
  {
     return max;
  }
}



Class B extends A
{
  public int max = 200;
}

I write the following code:

A clsA = new A();
B clsB = new B();


valA = clsA.getMax();
valB = clsB.getMax();

valA is 100 valB is 100 again

How can I write code that clsB.getMax(); returns 200?

M A
  • 71,713
  • 13
  • 134
  • 174
Bob
  • 22,810
  • 38
  • 143
  • 225
  • you didn't even override the method getMax() in classB, why you expect to get 200? it will execute the original method as there is no other one declared to override it in the classB. – Muhammed Refaat Jan 13 '15 at 08:51
  • 1
    look here http://www.tutorialspoint.com/java/java_overriding.htm – Muhammed Refaat Jan 13 '15 at 08:53
  • [This answer](http://stackoverflow.com/questions/12086298/why-instance-variable-of-super-class-is-not-overridden-in-sub-class-method) should also help – sanbhat Jan 13 '15 at 09:13

7 Answers7

6

You can override getMax in class B:

  @Override
  public int getMax()
  {
     return max;
  }

Though, in general, it's a bad idea to have variables with the same name in the super class and the sub-class.

Note that after the change, the following will also return 200 :

A clsB = new B();
valB = clsB.getMax();
Eran
  • 387,369
  • 54
  • 702
  • 768
2

Override the getMax method in B:

class B extends A
{
    public int max = 200;

    public int getMax()
    {
       return max;
    }
}

In the getter method, return max; is equivalent to return this.max where this references the current instance of B. On the other hand, calling return super.max returns the max field value referenced in the parent subclass.

M A
  • 71,713
  • 13
  • 134
  • 174
2

the short answer is that you can't override members in subclasses like you are trying. What you instead should do is make B change the value of max directly. Something like this:

class B extends A
{
    public B() 
    {
        max = 200;
    }
}
Joddie
  • 21
  • 1
0

You have to add method getMax() to class B. The implementation might be the same as in Class A

@Overwrite
public int getMax()
{
   return max;
}

When you DO NOT overwrite the method one from B calls parent's method getMax(). And parent metod getMax() returns 150

Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
0

Try Method Overriding

In Java, the "closest method" will be called. Because clsB extends A, but doesn't have it's own definition of getMax(), A's version of getMax() will be called. However, because clsB also shares the variables of A, getMax (called from class A) will return class A's number.

The solution? add

public int getMax()
{
     return max;
}

to class B.

Aify
  • 3,543
  • 3
  • 24
  • 43
0

Implement Method override concept

 @Override
  public int getMax()
  {
     return max;
  }

Overriding happens at run time

A clsB = new B();
valB = clsB.getMax();

Hope this should work.

Vignesh Shiv
  • 1,129
  • 7
  • 22
0

Java does not support override any variables in a class.

You can have two ways to do it.

  1. Pass the variable through a constructor. I suggest doing this way because, if the method do the same action. By overriding it and doing it again is a bad practice.

    Class A { public int max = 100; public A(int maxVal) { this.max=maxVal; } public int getMax(){ return max; } } B clsB = new B(); A clsA = new A(clsB.max);

  2. Override the method

    @override public int getMax(){ max=200; //do the job. }

Dileep
  • 5,362
  • 3
  • 22
  • 38