JavaBeat

  • Home
  • 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)
  • Privacy
  • Contact Us

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

[code lang=”java”]
package javabeat.net.core;

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

Manager.java

[code lang=”java”]
package javabeat.net.core;

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

Clerk.java

[code lang=”java”]
package javabeat.net.core;

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

AbstractDemo.java

[code lang=”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();
}
}
[/code]

Output

[code]
EMPLOYEE ID
Manager Role
EMPLOYEE ID
Manager Role
[/code]

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.

Filed Under: Java Tagged With: 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.

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.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved