0

I'm new to Java and OOP, I have one super-class and one sub-class like above. When I try to inherit the methods of super-class inside the sub-class and compile the code, it gives me a @782a9122 result. Where am I wrong??

thanks for your helps.

class A {
    // to inherit from normal class => use `extends` keyword 
    // to inherit from interface class => use `implements` keyword 
    private int _x, _y;
    
    public A(int x, int y) {
        this._x = x;
        this._y = y;
    }
    public int getX() {return _x;}
    public int getY() {return _y;}
    public void move(int x, int y) {
        this._x = x;
        this._y = y;
    }
}   


class B extends A {
    private String _color;
    public B(int x, int y, String color) {
        super(x, y);
        this._color = color;
    }
    // i try to invoke the method of super-class inside the sub-class
    public void setXY(int x, int y) {
        super.move(x, y);
    }
    public void setColor(String color) {
        this._color = color;
    }

    public static void main(String[] args) {
        B b = new B(5, 5, "Yellow");
        
        // when I compile this code, I got the@4759e894 as the result.
        b.setXY(10, 20);
        b.setColor("Red");
        String str = b.toString();
        System.out.println(str);
        
    }
}


frankiie
  • 456
  • 1
  • 6
  • 19
  • You're going to need to provide the actual stack trace and error message from the compilation. You also haven't provided a return type for your `ColorPoint()` initializer or the `A()` initializer, and you're missing some imports of basic Java language features. – James McPherson Oct 18 '20 at 08:14
  • I've edited the code, I've just modified the natural class name. – frankiie Oct 18 '20 at 08:16
  • 1
    Do you mean when you run the code? It gives something like that if you call the default `Object.toString()` in your code. In that case you need to override `toString()`. – pirho Oct 18 '20 at 08:19
  • yeah, when I run the code, this gives me the result like @7852a92 – frankiie Oct 18 '20 at 08:27
  • 1
    Neither A nor B override toString(). So yes, you get that output. – Polygnome Oct 18 '20 at 08:27
  • @pirho, thank you, I'll try this – frankiie Oct 18 '20 at 08:28

0 Answers0