0
    abstract class A
{
 abstract void callme(); 
}
class B extends A
{
 void callme()
 {
  System.out.println("this is callme."); 
 }
 public static void main(String[] args)
 {
  B b=new B();
  b.callme();
 }
}

// if this can be done through overriding why use abstraction

 class Animal
{
 Animal myType()
 {
  return new Animal();
 }
}

class Dog extends Animal
{
 Dog myType()     //Legal override after Java5 onward
 {
  return new Dog();
 }
}
5
  • 2
    Your two examples seem to be unrelated. Abstract classes are usually used when the abstract class provides code that isn't specific to the subclass implementations, but includes abstract methods that are then implemented within the subclasses.
    – Jon Skeet
    Feb 27, 2016 at 10:02
  • @JonSkeet, said it all. Perhaps you should read some of the Java documentation related and this post. Feb 27, 2016 at 10:07
  • notice that you cannot create an object using abstract class. new A() is not valid but new Animal() is valid
    – Kachna
    Feb 27, 2016 at 10:21
  • @JonSkeet class A{ public void callme(){ ......}} class B extends A { public void callme(){......} } this is possible possible then why make class A abstract
    – muhammad
    Feb 27, 2016 at 10:47
  • @muhammad: Because class A may not be able to implement callme itself, if it's meant to be a general abstraction.
    – Jon Skeet
    Feb 27, 2016 at 10:48

1 Answer 1

0

abstract class mainly used to bound the subclass to give the body of its abstract method. where as in non abstract class you are not bounding the subclass to give the body of its methods.

in class B its mandatory to give the body of callme();

where as

in class Dog its not mandatory to give the body of myType();

Not the answer you're looking for? Browse other questions tagged or ask your own question.