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();
 }
}
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
muhammad
  • 11
  • 1
  • 3
  • 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 '16 at 10:02
  • @JonSkeet, said it all. Perhaps you should read some of the [Java documentation related](https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html) and [this](http://stackoverflow.com/questions/24626/abstraction-vs-information-hiding-vs-encapsulation) post. – António Ribeiro Feb 27 '16 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 '16 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 '16 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 '16 at 10:48

1 Answers1

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();

Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39