-3

Here is the code:

class base
{
    public void abc()
    {
        System.out.print("what's up dude");
    }
}

class derived
{
    public void abc()
    {
        System.out.print("wad up ");
    }
}

public class superreftosub
{
    public static void main(String args[])
    {
        base b=new base();
        derived d=new derived();
        base b=d;
        b.abc();
    }
}
Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
Davinci
  • 13
  • 4
  • You don't assign objects to objects. – Sotirios Delimanolis Jun 23 '14 at 18:58
  • 3
    `derived` doesn't currently extend `base`. You have two `b` variables declared in `main`. – rgettman Jun 23 '14 at 18:58
  • 4
    You don't derive. Where is the extends? – deets Jun 23 '14 at 18:58
  • 1
    Assigning one object to another just assigns the object reference (a pointer more or less). It does NOT copy member variables etc. You need to read about cloning. From the first paragraph of the wiki for Java clone(): In Java, objects are manipulated through reference variables, and there is no operator for copying an object—the assignment operator duplicates the reference, not the object. The clone() method provides this missing functionality. – clever_bassi Jun 23 '14 at 19:01

3 Answers3

1

You must use a superclass-subclass relation:

so:

class derived extends base {

    @Override
    public void abc() {
        System.out.print("wad up ");
    }
}

In some weakly-typed programming paradigms (like for instance duck-typing), this is indeed not necessary. Java is however strongly typed (well java is not fully strongly typed, but that's another discussion) and only allows assigning extended types and you can only give a new definition to a method using the @Override annotation.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Technically you don't *have* to annotate your overriding method, but it's a good idea to always do it. – azurefrog Jun 23 '14 at 19:05
  • The `@Override` keyword makes sure that the compiler checks that there is a method in the superclass with that signature. It is not necessary but will decrease the number of mistakes. I agree with your comment, but one should try to give advice on how to produce good code. – Willem Van Onsem Jun 23 '14 at 19:07
0

you should use extends keyword to inherit a class,

Like

class derived extends base {
    //...
}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
KNOWME
  • 61
  • 7