Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javatutorial Abstract Class in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Abstract Class in Java

Advertisement
Abstract class in Java is a class that is declared using abstract keyword. It cannot be instantiated but can be extended into subclass. Abstract class cannot be instantiated means that new instances of an abstract class cannot be created. When an abstract class is extended into subclass, the abstract method of abstract class must be defined in the subclass.

Abstract class in Java is a class that is declared using abstract keyword. It cannot be instantiated but can be extended into subclass.

Abstract class cannot be instantiated means that new instances of an abstract class cannot be created.

It is not necessary that an abstract class have abstract methods. An abstract method is declared without body but is followed by a semicolon.

If we have to define an abstract method under class then we have to make the class abstract as well.

All abstract methods must be public.

When an abstract class is extended into subclass, the subclass provides an implementation of the abstract methods in its parent class.

When an abstract class is extended into subclass, the abstract method of abstract class must be defined in the subclass.

Points of Abstract class :

  • Abstract class may or may not contain abstract methods.
  • An abstract class cannot be instantiated.
  • Abstract classes can be extended into sub classes.

Abstract class can be declared as:

abstract class AbstractClass {
}

How to use of Abstract Method:

public class SecondClass extends AbstractClass
 {
     void display()
      {
          System.out.println("display() method implemented in SecondClass");
      }
 }

Abstract class Example:

AbstractClass.java

abstract class AbstractClass {
 
  String name;  
  public void setName(String name)
   {
      this.name = name;
   }
   public String getName()
   {
      return this.name;
   }
   abstract void display(); 
}

FirstClass.java

public class FirstClass extends AbstractClass
 {
     void display()
      {
          System.out.println("display() method implemented in FirstClass");
      }
 }

SecondClass.java

public class SecondClass extends AbstractClass
{
void display()
{
System.out.println("display() method implemented in SecondClass");
}
}

TestAbstractClass.java

 class TestAbstractClass
 {
     public static void main(String args[])
       {
         
          FirstClass first = new FirstClass();
          SecondClass second = new SecondClass();
          System.out.println("\n*************Output*************");
          System.out.println("Abstract Class Example");
          first.setName("First Name : Rose");
          second.setName("Second Name : India");
          
          first.display();
          System.out.println(first.getName());
          second.display();
          System.out.println(second.getName());  
          System.out.println("\n*************END****************");
       }
 }
 
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 22, 2013

Related Tutorials

Discuss: Abstract Class in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com