-7

Abstract method's property is that it doesn't have body, and so when concrete class extends abstract class, it must implement that abstract method.

So my question is how can an abstract method can be overridden when it doesn't have a body? Isn't it implemented, not overridden?

Kush
  • 75
  • 1
  • 8
user2151689
  • 9
  • 1
  • 2
  • 5
    You should have gone through the tutorial before asking such basic stuff http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html – AurA Jul 03 '13 at 07:43
  • http://stackoverflow.com/questions/1005898/java-should-i-add-an-override-annotation-when-implementing-abstract-methods – sanbhat Jul 03 '13 at 07:43

8 Answers8

12

Abstract class with an abstract method.

The method is also marked with abstract and has no body.

public abstract class A {
    public abstract void method();
}

Concretization in the class B that extends abstract class A. Also, the method loses the abstract keyword and gets the body.

public class B extends A {
    @Override
    public void method() {
        System.out.println("Hello");
    }
}
darijan
  • 9,725
  • 25
  • 38
  • Here non abstract method overriding a abstract method so i am little bit confuse about it. – user2151689 Jul 03 '13 at 08:54
  • Yes, `abstract` is a kind of a "sign" that this method **must** be overridden in some of the future methods. A method that overrides must have tame same method signature, but lose the `abstract` keyword. – darijan Jul 03 '13 at 08:59
3
public abstract class BaseClass {
    public abstract void doSomething();
}

public class ConcreteClass extends BaseClass {
    @Override
    public void doSomething() {
        System.out.println("Hi!");
    }
}

public class AnotherConcreteClass extends BaseClass {
    @Override
    public void doSomething() {
        System.out.println("Hello!");
    }
}

The implementation of the doSomething method that gets executed will depend on the runtime type of the object you are calling doSomething upon.

Raibaz
  • 9,280
  • 10
  • 44
  • 65
2

Just by providing an implementation for the method , provide it with a body.

public abstract class Base {
   public abstract void someMethod(); // no implementation here
}

public class Sub extends Base {
   @Override
   public void someMethod() {
    //It is Overridden
   }
}
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
2
public abstract class A {  
    public abstract String getValue();
}

public class B extends A {

    @Override
    public String getValue(){
        return "I'm Overriden";
    }
}

It's the same like in any other class.

ssindelar
  • 2,833
  • 1
  • 17
  • 36
1

When you are implementing a superclass method in subclass, it will override. So Even if the superclass method is abstract, if you implement it in subclass, then it will override it.

So,

public class SuperClass{
   public abstract void someMethod();
}

 public class SubClass{
   public void someMethod(){

           System.out.println("subclass method");

   }
 }
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
1

Foo.java

public abstract class Foo {
  public abstract void doStuff();
}

Bar.java

public class Bar extends Foo {
    @Override
    public void doStuff() {
        // doStuff
    }
}
tom
  • 18,953
  • 4
  • 35
  • 35
1

Abstract method can be overridden just like overriding other normal methods, by providing an implementation.

aksh1t
  • 5,410
  • 1
  • 37
  • 55
1

This is the same as any other method override:

public abstract class AbstractClass {
    public abstract void abstractMethod();
}

...

public class ConcreteClass {

    @Override
    public void abstractMethod() {
        ....
    }
}
robjohncox
  • 3,639
  • 3
  • 25
  • 51