Java Abstract class
In this chapter you will learn:
- What is abstract class
- How to create abstract class
- Example - abstract class
- A demo for using abstract methods and classes
Description
Abstract class is for abstract idea or concept. For example, int data type is a concrete data type and double is another concrete data type. They are both numbers. Here number is an abstract concept. Shape is another example. We can have spare, rectangle or triangle or circle. They are all concrete while shape is an abstract class.
In Java we use abstract class to define the abstract concept. Abstract concept must have some abstract aspects. For example, the abstract concept is the Shape while the abstract aspect is how to calculate area. The abstract concept becomes abstract class in Java and the abstract aspect becomes the abstract method.
Syntax
You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. To declare an abstract method, use this general form:
abstract type name(parameter-list);
No method body is present for abstract method. Any class that contains one or more abstract methods must also be declared abstract.
abstract class MyAbstractClass{
abstract type name(parameter-list);
}
Example
Here is an abstract class, followed by a class which implements its abstract method.
abstract class MyAbstractClass {
abstract void callme();
/* w w w . jav a 2s .com*/
void callmetoo() {
System.out.println("This is a concrete method.");
}
}
class B extends MyAbstractClass {
void callme() {
System.out.println("B's implementation of callme.");
}
}
public class Main {
public static void main(String args[]) {
B b = new B();
b.callme();
b.callmetoo();
}
}
The output:
Example 2
The following code defines Shape
class as abstract. Shape
class
has abstract method called area(). Rectangle
class extends abstract class
Shape
and implements the area() method for itself.
abstract class Shape {
double height;// w w w . ja va2 s . c o m
double width;
Shape(double a, double b) {
height = a;
width = b;
}
abstract double area();
}
class Rectangle extends Shape{
Rectangle(double a, double b) {
super(a, b);
}
double area() {
System.out.println("Inside Area for Rectangle.");
return height * width;
}
}
class Triangle extends Shape{
Triangle(double a, double b) {
super(a, b);
}
double area() {
System.out.println("Inside Area for Triangle.");
return height * width / 2;
}
}
public class Main {
public static void main(String args[]) {
Rectangle r = new Rectangle(10, 5);
Triangle t = new Triangle(10, 8);
Shape figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- What is Java access control level
- Java Class Access Control Level
- Example - effects of public and private access
- How does the access control act on the inheritance
- What is the access matrix for Java
- What are the targets for different access modifiers
Java Object
Java Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java this Keyword
Java static keyword
Java Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Local Classes
Java Member Classes
Java Static Member Classes
Java Class Variables
Java main() Method
Java Class Inheritance
Java super keyword
Java Method Overriding
Java Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access ControlJava Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files