create abstract class java code example

Example 1: abstract class in java

Sometimes we may come across a situation where we cannot provide 
implementation to all the methods in a class. We want to leave the 
implementation to a class that extends it. In such case we declare a class
as abstract.To make a class abstract we use key word abstract. 
Any class that contains one or more abstract methods is declared as abstract. 
If we don’t declare class as abstract which contains abstract methods we get 
compile time error.
  
  1)Abstract classes cannot be instantiated
  2)An abstarct classes contains abstract method, concrete methods or both.
  3)Any class which extends abstarct class must override all methods of abstract
    class
  4)An abstarct class can contain either 0 or more abstract method.

Example 2: What are abstract methods in java

An abstract method is the method which does’nt have any body. 
Abstract method is declared with
keyword abstract and semicolon in place of method body.

  public abstract void <method name>();
Ex : public abstract void getDetails();
It is the responsibility of subclass to provide implementation to 
abstract method defined in abstract class

Example 3: abstraction in java

Abstraction is defined as hiding internal implementation and showing only 
necessary information.
// abstract class
abstract class Addition
{
   // abstract methods
   public abstract int addTwoNumbers(int number1, int number2);
   public abstract int addFourNumbers(int number1, int number2, int number3, int number4);
   // non-abstract method
   public void printValues()
   {
      System.out.println("abstract class printValues() method");
   }
}
class AbstractMethodExample extends Addition
{
   public int addTwoNumbers(int number1, int number2)
   {
      return number1 + number2;
   }
   public int addFourNumbers(int number1, int number2, int number3, int number4)
   {
      return number1 + number2 + number3 + number4;
   }
   public static void main(String[] args)
   {
      Addition add = new AbstractMethodExample();
      System.out.println(add.addTwoNumbers(6, 6));
      System.out.println(add.addFourNumbers(8, 8, 3, 2));
      add.printValues();
   }
}

Example 4: java abstract class

// abstract class
abstract class Shape
{
   // abstract method
   abstract void sides();
}
class Triangle extends Shape
{
   void sides()
   {
      System.out.println("Triangle shape has three sides.");
   }
}
class Pentagon extends Shape
{
   void sides()
   {
      System.out.println("Pentagon shape has five sides.");
   }
   public static void main(String[] args)
   {
      Triangle obj1 = new Triangle();
      obj1.sides();
      Pentagon obj2 = new Pentagon();
      obj2.sides();
   }
}

Example 5: abstract class in java

public abstract class GraphicObject {

   abstract void draw();
}

Example 6: abstract class example in java

//abstract parent class
abstract class Animal{
   //abstract method
   public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal{

   public void sound(){
	System.out.println("Woof");
   }
   public static void main(String args[]){
	Animal obj = new Dog();
	obj.sound();
   }
}

Tags:

Misc Example