• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Abstract Keyword

January 17, 2014 //  by Krishna Srinivasan//  Leave a Comment

Abstract keyword is used with class and method declaration. Abstract defines incomplete data structures where the final or complete implementation will be done by its child class. In Java, Abstract used for defining the common behaviour for a class and let the child class to override the common behaviour. Some of the characteristics of the abstract keyword are :

Characteristics of Abstract

  • Abstract class may or may not have abstract methods
  • Abstract classes can not be instantiated
  • If a class declares abstract method, the class must be declared as abstract class
  • If a class extedns abstract class and it is not implementing the inherited abstract methods, then child class also has to be defined as abstract
  • An abstract method is a method that is declared without an implementation
  • An abstract class may have static fields and static methods. You can use these static members with a class reference (for example, bstractClass.staticMethod()) as you would with any other class.

Abstract Keyword Example

Employee.java

package javabeat.net.core;

public abstract class Employee {
	public void getEmpId(){
		System.out.println("EMPLOYEE ID");
	}
	public abstract void getRole();
}

Manager.java

package javabeat.net.core;

public class Manager extends Employee {
	@Override
	public void getRole() {
		System.out.println("Manager Role");
	}
}

Clerk.java

package javabeat.net.core;

public class Clerk extends Employee{
	@Override
	public void getRole() {
		System.out.println("Clerk Role");
	}
}

AbstractDemo.java

package javabeat.net.core;

public class AbstractDemo {
	public static void main(String args[]){
		Employee manager = new Manager();
		Employee clerk = new Manager();
		manager.getEmpId();
		manager.getRole();
		clerk.getEmpId();
		clerk.getRole();
	}
}

Output

EMPLOYEE ID
Manager Role
EMPLOYEE ID
Manager Role

If you look at the above example code, You will understand the use of abstract keyword in Java. If you have any questions, please write it in the comments section.

Category: JavaTag: Java Basics

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « JSP Actions
Next Post: Final Keyword »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

EJB 3.0 Timer Services

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact