• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Abstract class and methods

October 9, 2011 By j2eereference 1 Comment

What is an Abstract class?

An abstract class’s main purpose is to be subclassed, you cannot instantiate an abstract class, and if you try to instantiate you will get compiler error. Advantage of using an abstract class is polymorphism and it gives greatest degree of flexibility and extensibility. Abstract class is used when you want to implement most generic methods in superclass (abstract class) and more specialized behavior in subclass. For ex: there is a class Animal has a generic methods common to all animals. You cannot create object of animal class because behavior of all animals is different, each animal has its own specific behavior.  Let see an example,

1
2
3
4
5
6
7
abstract class Animal
{
    private int age;
    public void getAge();
    public abstract void eat();
    public abstract void makeNoise();
}

If you try to create an object of Animal, you will get compiler error. Abstract class or method can be defined using abstract keyword. An abstract methods ends with semicolon, rather than curly braces.

 

Abstract class cannot be declared as final or private as abstract class’s main purpose is inheritance and if any class is final or private we cannot create subclass from it.

Abstract Methods

An abstract method declared using abstract keyword, ends with semicolon and it cannot be implemented.

public abstract void eat();

If class has even a single abstract method, then class must be declared as abstract class. Concrete class cannot contain abstract method; if it has it should be declared as abstract class. Abstract class can contain abstract methods and concrete methods.

The first non-abstract subclass of an abstract class must implement all abstract methods of the superclass; otherwise you will get compiler error. If your abstract class is extended by another abstract class, then abstract class need not required to implement methods of superclass. Later when concrete class is subclassed from abstract class; it must provide implementation for all the methods from up the inheritance tree.

Abstract methods cannot be declared as final, private or static. We need to override the methods of superclass and has to provide implementation for all methods in subclass. If any of these modifiers is used in combination with abstract we cannot override methods in subclass.

Abstract class can have constructor and can be accessed or called when subclass is instantiated.

The following example gives an inheritance tree with abstract class and concrete class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<strong>
public</strong> <strong>abstract</strong> <strong>class</strong> Animal
 {
<strong> public</strong> Animal ()
    {
    System.<em>out</em>.println("In Animal constructor");
    }
 
<strong> private</strong> <strong>int</strong> age;
<strong> public</strong> <strong>abstract</strong> <strong>void</strong> eat();
<strong> public</strong> <strong>abstract</strong> <strong>void</strong> makeNoise();
<strong> public</strong> <strong>int</strong> getAge()
   {
<strong> return</strong> age;
   }
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<strong>
public</strong> <strong>class</strong> Cat <strong>extends</strong> Animal
{
 
<strong> public</strong> Cat()
   {
   System.<em>out</em>.println("In cats Constructor");
   }
 
<strong> public</strong> <strong>void</strong> eat()
  {
    System.<em>out</em>.println("Cat is eating fish");
  }
 
<strong> public</strong> <strong>void</strong> makeNoise()
  {
   System.<em>out</em>.println("Meow meow");
  }
 
<strong> public</strong> <strong>static</strong> <strong>void</strong> main(String [] args)
  {
   Animal cat = <strong>new</strong> Cat();
   cat.eat();
   cat.makeNoise();
   }
 
}

Output:

In Animal constructor

In cats Constructor

Cat is eating fish

Meow meow

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Comments

  1. pulipati udaykiran says

    November 8, 2011 at 11:03 am

    good tutorial about abstract class and methods. I clarified a doubt on abstract class and methods by reading this article.

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.