- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Abstraction
In Java, Abstraction is the ability to make a class abstract in Object Oriented Programming (OOP).
Java abstract Class
An abstract class is the one that cannot be instantiated. All the other functionality of the class still exists. Its fields, methods, and constructors are all accessed in the same manner. You cannot create an instance of the abstract class.
If a class is abstract and can't be instantiated, the class does not have much use, it is subclass. This is typically how the abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but parent class itself is too abstract to be used on its own.
There are some situations in which you will want to define a superclass that declares the structure of a given abstraction without providing the full implementation of every method i.e., sometimes you will want to create a superclass that only defines a generalized form which will be shared by all of its subclasses, leaving it to each subclass to fill in the details. Such class determines the nature of the methods that the subclasses must implement.
One way this situation can take place is when a superclass is unable to create a meaningful implementation for a method. When you create your own class libraries, it is common for a method to have no meaningful definition in the context of its superclass.
You can handle this situation in two ways. One way, is to simply have it report a warning message. While this approach can be useful in certain (definite but not specified) situations, such as debugging, it is not usually appropriate. You may have methods that must be overridden by the subclass in order for subclass to have any meaning. Consider the class Triangle. It has no meaning if the area() is not defined. In this case, you want some way to ensure that a subclass does, indeed, override all the necessary methods. Java's solution to this problem is the abstract method.
You can require that certain methods to be overridden by subclasses by specifying the abstract type modifier. These methods are sometimes referred to as a subclasser responsibility because they have no implementation specified in the superclass. Thus, a subclass must override them, it can't simply use the version defined in the superclass.
Declare an abstract Method
To declare an abstract method, use the following general form :
abstract type name(parameter-list);
As you can see, no method body is present.
A class, contains one or more abstract methods must also be declared as abstract.
Declare an abstract Class
To declare a class abstract, you simply use the abstract keyword in from of class keyword at the beginning of class declaration as shown here.
abstract class class_name
An abstract class can't be directly instantiated with operator new. Such objects will be useless, because an abstract class is not entirely defined. Also, you can't declare abstract constructors, or abstract static methods. Any subclass of an abstract class must implement all of the abstract static methods.
Java abstract Class and abstract Method Example
Following is a simple example program of a class with an abstract method, followed by a class which implements that method :
/* Java Program Example - Java Abstraction Example * This program demonstrates abstract class */ abstract class A { abstract void callme(); /* the concrete methods are still allowed in the * abstract classes */ void callmealso() { System.out.println("This is a concrete method"); } } class B extends A { void callme() { System.out.println("B's implementation of callme"); } } class JavaProgram { public static void main(String args[]) { B obj = new B(); obj.callme(); obj.callmealso(); } }
When the above Java program is compile and executed, it will produce the following output:
Notice that no objects of the class A are declared in the above program. As mentioned that it is not possible to instantiate an abstract class. Also class A implements a concrete method called callmealso(). This is perfectly acceptable. Abstract classes can include as much implementation as then see fit.
Even though, abstract classes can't be used to instantiate objects, they can be used to create object references, because approach of Java to run-time polymorphism is implemented through the use of the superclass references. Thus, it must be possible to create a reference to the abstract class so that it can be used to point to a subclass object. You will see this feature put to use in the upcoming example program.
The following version of the program declares area() is abstract inside the Figure. This means that all the classes derived from the Figure must override the area().
/* Java Program Example - Java Abstraction Example * This program uses abstract methods and * classes */ abstract class Figure { double d1; double d2; Figure(double x, double y) { d1 = x; d2 = y; } /* area is now an abstract method */ abstract double area(); } class Rectangle extends Figure { Rectangle(double x, double y) { super(x, y); } /* override area for rectangle */ double area() { System.out.println("Inside Area for Rectangle : "); return d1 * d2; } } class Triangle extends Figure { Triangle(double x, double y) { super(x, y); } /* override area for right triangle */ double area() { System.out.println("Inside Area for Triangle : "); return d1 * d2 / 2; } } class JavaProgram { public static void main(String args[]) { /* Figure fig = new Figure(100, 100); // illegal now!! */ Rectangle rec = new Rectangle(90, 50); Triangle tri = new Triangle(100, 80); Figure figref; // this is OK, no object is created figref = rec; System.out.println("Area is " + figref.area()); figref = tri; System.out.println("Area is " + figref.area()); } }
When the above Java program is compile and executed, it will produce the following output:
As the comment inside the main() indicates, it is no longer possible to declare objects of type Figure, since it is now abstract. And, all the subclasses of the Figure must override the area(). To provide this to yourself, try creating a subclass that does not override the method area(). You will receive a compile-time error.
Even though it is not possible to create an object of type Figure, you can create a reference variable of type Figure. The variable figref is declared as a address to the Figure, which means that it can be used to refer to an object of any class derived from the Figure. As explained, it is through superclass reference variables that overridden methods are resolved at run time.
« Previous Tutorial Next Tutorial »