Abstract Class in Java





  • A class that is marked with abstract keyword, is known as abstract class in java.

  • Abstract can have abstract and non-abstract methods (method with body).

  • A method that doesn't have its definition is called abstract method, and to declare a method as abstract use keyword abstract.

    Syntax :

    abstract return-type method-name(arg-list);
  • If Any class contains direct or indirect ( through parent ), one or more abstract method, that class must be declared as abstract.

  • An abstract class cannot have objects because it is not complete, but it can have references.

  • Static methods and constructors cannot be declared as abstract.

    abstract static void show();prowessapps.in
  • A concrete method can be overridden to become abstract.

  • It is illegal to declare a class and method both final and abstract.

    abstract final void show();prowessapps.in
    abstract final class Test {}
 //abstract class
abstract class Shape
{
 //abstract method
 abstract double area();

//concrete method
 void displayDetail()
 {
  System.out.println("Details of Shape");
 }
}
class Square extends Shape
{
 double side;
//Overriding the abstract method.
 double area() 
 { 
  return side*side;
 }
}

USE : A super class that only defines a generalized form that will be shared by all its sub classes, leaving the implementation details to its subclasses is said to an abstract class.

Example :

Example to understand the concept :
abstract class Animal
{
 abstract void talk();
}
class Dog extends Animal
{
  void talk()
   {
    System.out.println("bark");
   }
}
class Cat extends Animal
{
  void talk()
   {
    System.out.println("mew");
   }
}
class MainClass
{
  public static void main(String[] args)
  {
//ERROR, can not create object
   Animal ob = new Animal();

//can have references
    Animal ob1,ob2;

//Dynamic Polymorphism
    ob1 = new Dog();
    ob2 = new Cat();
    ob1.talk();
    ob2.talk();
  }
}           
OUTPUT : if error remove
bark
mew


 




Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved