0% found this document useful (0 votes)
16 views8 pages

Abstract Classes

An abstract class is a class that cannot be instantiated and may contain abstract methods, which are declared without implementation. Subclasses of an abstract class must implement its abstract methods unless they are also declared abstract. Concrete classes, in contrast, contain all methods with implementations and can be instantiated.

Uploaded by

Aaditarya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Abstract Classes

An abstract class is a class that cannot be instantiated and may contain abstract methods, which are declared without implementation. Subclasses of an abstract class must implement its abstract methods unless they are also declared abstract. Concrete classes, in contrast, contain all methods with implementations and can be instantiated.

Uploaded by

Aaditarya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Abstract Methods and Abstract

Classes
■ An Abstract class is a class that is declared
abstract—it may or may not include abstract
methods. But its objects cannot be created.
■ An Abstract method is a method that is declared
without an implementation i.e. no body(without
braces, and followed by a semicolon), like this:
abstract void add(int X, int Y);
■ If a class includes abstract methods, the class
itself must be declared abstract, as in:
public abstract class Shape
{ // declare properties
// declare non-abstract methods
abstract void area();
}
■ When an abstract class is sub classed, the
subclass usually provides implementations for all
of the abstract methods in its parent class.
However, if it does not, the subclass must also be
declared abstract.
Example
abstract class Shape
{
int x, y;
void accept(int X, int Y) //concrete method
{ ... }
abstract void area(); //abstract method
abstract void perimeter(); //abstract method
}
Each non-abstract subclass of Shape, such as Circle
and Rectangle, must provide implementations for
the area( ) and perimeter( ) methods.
Sub class Circle

class Circle extends Shape


{
void area()
{ ... }
void perimeter()
{ ... }
}
Sub class Rectangle

class Rectangle extends Shape


{
void area()
{ ... }
void perimeter()
{ ... }
}
Concrete class and Abstract
Class
Concrete Class Abstract Class
■Contains all methods ■May contain abstract
with method body methods along with
concrete methods.
■Object can be created ■Object cannot be
of the class. created of abstract
class.
Concrete method and
Abstract method
Concrete method Abstract method
■It has method body ■Does not have method
body.
■May or may not be ■Needs to be
overridden. overridden.
■E.g ■E.g
void show() abstract void show();
{
……
}

You might also like