Abstract classes and Methods in
Java
A Presentation in Depth
Index
Definition
Usefulness
Why they cannot be instantiated?
How to use abstract classes?
Purpose of abstract class
What is Concrete class?
Examples
Characteristics explained
Definition
Abstract classes—for which you never intend to
create objects.
Useful
They’re used only as superclasses in inheritance
hierarchies, we refer to them as abstract
superclasses.
Why they cannot be instantiated?
1. These classes cannot be used to instantiate
objects, because abstract classes are
incomplete.
2. Abstract superclasses are too general to
create real objects—they specify only what is
common among subclasses.
So how can we use them? Or
instantiate them?
Subclasses must declare the “missing pieces” to
become “concrete” classes, from which you can
instantiate objects.
What is the purpose of abstract class?
An abstract class’s purpose is to provide an
appropriate superclass from which other classes
can inherit and thus share a common design.
What is concrete class?
Classes that can be used to instantiate objects
are called concrete classes. Such classes provide
implementations of every method they declare
(some of the implementations can be inherited).
Concrete classes provide the specifics that make
it reasonable to instantiate objects.
Example 1
Example 2
Characteristics summary
Characteristics summary
1. Abstract class can be empty.
Characteristics summary
1. Abstract class can be empty.
2. Abstract class can be made without abstract
methods.
Characteristics summary
1. Abstract class can be empty.
2. Abstract class can be made without abstract
methods.
3. A non-abstract class cannot contain abstract
method.
Abstract class can be empty
Abstract class can be empty
abstract class Vehicle{}
Abstract class can be empty
abstract class Vehicle{}

Contains no
members
Abstract class can be empty
abstract class Vehicle{}

Perfectly valid

Contains no
members
Abstract class can be made without
abstract methods
Abstract class can be made without
abstract methods
abstract class Vehicle
{
void brake()
{
System.out.println("non abstract method brake");

}
}
Abstract class can be made without
abstract methods
abstract class Vehicle
{
void brake()
{
System.out.println("non abstract method brake");

}
}
Non abstract
method
Abstract class can be made without
abstract methods
abstract class Vehicle
{
void brake()
{
System.out.println("non abstract method brake");

}
}
Non abstract
method

Perfectly
valid
class Vehicle
{
abstract void brake();
}
class Vehicle
{
abstract void brake();
}

Invalid/compilation
error
A non-abstract class cannot contain
abstract method
class Vehicle
{
abstract void brake();
}

Invalid/compilation
error
A non-abstract class cannot contain
abstract method
class Vehicle
{
abstract void brake();
}

Invalid/compilation
error

In other words, if
a class contains
abstract method
then class must
also be abstract.
A non-abstract class cannot contain
abstract method
class Vehicle
{
abstract void brake();
}

Invalid/compilation
error

Now valid

abstract class Vehicle
{
abstract void brake();
}

In other words, if
a class contains
abstract method
then class must
also be abstract.
Example 3
Example 4
Characteristics summary
Characteristics summary
4. Class can be final or abstract, not both.
Characteristics summary
4. Class can be final or abstract, not both.
5. Method be can be final or abstract not both.
Class can be abstract or final, not both
Class can be abstract or final, not both

Error
Method can be abstract or final, not
both
Method can be abstract or final, not
both

Error
Real Examples of Abstract Classes in
Java API
1. java.awt.Component
Component

Button

Checkbox

Label

TextComponent
Real Examples of Abstract Classes in
Java API
2. java.lang.Number

Number

Byte

Long

Integer

Float

Double
Real Examples of Abstract Classes in
Java API
3. javax.swing.AbstractButton
AbstractButton

JButton

JToggleButton

JMenuItem
Characteristics summary
Characteristics summary
6. Non-abstract class cannot contain abstract methods even
there are non abstract methods too.
Characteristics summary
6. Non-abstract class cannot contain abstract methods even
there are non abstract methods too.
7. An abstract class can contain both abstract and non abstract
methods.
Characteristics summary
6. Non-abstract class cannot contain abstract methods even
there are non abstract methods too.
7. An abstract class can contain both abstract and non abstract
methods.
8. Abstract class can be inherited like normal class.
Characteristics summary
6. Non-abstract class cannot contain abstract methods even
there are non abstract methods too.
7. An abstract class can contain both abstract and non abstract
methods.
8. Abstract class can be inherited like normal class.
9. If abstract class contains no abstract methods then subclass
of it, can be empty.
Non-abstract class cannot contain abstract
methods even there are non-abstract methods
too
Non-abstract class cannot contain abstract
methods even there are non-abstract methods
too

Non abstract
method
Non-abstract class cannot contain abstract
methods even there are non-abstract methods
too
Abstract
method

Non abstract
method
Non-abstract class cannot contain abstract
methods even there are non-abstract methods
too
Abstract
method

Non abstract
method

Either make the class
abstract or make method
non abstract to correct
this error
An abstract class can contain both
abstract and non abstract methods
An abstract class can contain both
abstract and non abstract methods

Non
abstract
method
An abstract class can contain both
abstract and non abstract methods
Abstract
method

Non
abstract
method
Abstract class can be inherited like
normal class

If abstract class
is empty then
subclass can
also be empty.
Abstract class can be inherited like
normal class

If abstract class
is empty then
subclass can
also be empty.
Abstract class can be inherited like
normal class

No error

If abstract class
is empty then
subclass can
also be empty.
If abstract class contains no abstract
methods then subclass of it, can be empty
If abstract class contains no abstract
methods then subclass of it, can be empty
If abstract class contains no abstract
methods then subclass of it, can be empty

Perfectly
valid
Example 5
Example 6
Example 7
Characteristics summary
Characteristics summary
10. If abstract class contains one or more abstract methods
then subclass of it, can not be empty.
Characteristics summary
10. If abstract class contains one or more abstract methods
then subclass of it, can not be empty.
11. If abstract class contains one or more abstract methods
then subclass of it, can be empty, only if subclass is also
abstract.
Characteristics summary
10. If abstract class contains one or more abstract methods
then subclass of it, can not be empty.
11. If abstract class contains one or more abstract methods
then subclass of it, can be empty, only if subclass is also
abstract.
12. If a abstract class contains abstract methods then subclass
must have to implements(write code) for abstract
methods, if subclass does not want to be abstract.
If abstract class contains one or more abstract
methods then subclass of it, can not be empty
If abstract class contains one or more abstract
methods then subclass of it, can not be empty
If abstract class contains one or more abstract
methods then subclass of it, can not be empty

Error
If abstract class contains one or more abstract
methods then subclass of it, can not be empty

Error
If abstract class contains one or more abstract
methods then subclass of it, can not be empty

Error

There are two ways to correct
this error either implement
abstract methods in subclass or
make subclass abstract.
If abstract class contains one or more abstract
methods then subclass of it, can not be empty

Error

There are two ways to correct
this error either implement
abstract methods in subclass or
make subclass abstract.

Next slides
will show
how to
remove this
error
If abstract class contains one or more abstract
methods then subclass of it, can be empty, only
if subclass is also abstract
If abstract class contains one or more abstract
methods then subclass of it, can be empty, only
if subclass is also abstract
If abstract class contains one or more abstract
methods then subclass of it, can be empty, only
if subclass is also abstract

Perfectly
valid
If a abstract class contains abstract methods then subclass must
have to implements(write code) for abstract methods, if subclass
does not want to be abstract
If a abstract class contains abstract methods then subclass must
have to implements(write code) for abstract methods, if subclass
does not want to be abstract

Perfectly
valid
Characteristics summary
Characteristics summary
13.Abstract class can implement super class
abstract methods.
Characteristics summary
13.Abstract class can implement super class
abstract methods.
14.Abstract classes can contain final methods,
constructors, static methods.
Characteristics summary
13.Abstract class can implement super class
abstract methods.
14.Abstract classes can contain final methods,
constructors, static methods.
15.An abstract class cannot be instantiated, but we
can make reference of this class.
If abstract class contains one or more abstract
methods then subclass of it, can be abstract and still
can implements super class methods.
If abstract class contains one or more abstract
methods then subclass of it, can be abstract and still
can implements super class methods.
If abstract class contains one or more abstract
methods then subclass of it, can be abstract and still
can implements super class methods. Abstract

super class
If abstract class contains one or more abstract
methods then subclass of it, can be abstract and still
can implements super class methods. Abstract

super class

Abstract
sub class
If abstract class contains one or more abstract
methods then subclass of it, can be abstract and still
can implements super class methods. Abstract

super class

Abstract
sub class

In other words,
abstract class can
implement super
class abstract
methods
If abstract class contains one or more abstract
methods then subclass of it, can be abstract and still
can implements super class methods. Abstract

super class

Abstract
sub class

Perfectly
valid

In other words,
abstract class can
implement super
class abstract
methods
Abstract classes can contain
constructors
Abstract classes can contain static
methods
Abstract classes can contain static
methods
Abstract classes can contain static
methods
Abstract classes can contain static
methods

Output
Abstract classes can contain final
methods
Abstract classes can contain final
methods

Perfectly
valid
An abstract class cannot be
instantiated
An abstract class cannot be
instantiated
We can make reference of
abstract class
An abstract class cannot be
instantiated
We can make reference of
abstract class

Error
Example 8
Example 9
Characteristics summary
8. Abstract methods cannot be private. They
can have public, protected or default access
specifier.
9. Abstract class can extend non-abstract class.
Abstract methods cannot be private
Abstract methods cannot be private

Error
Abstract methods can use public
access specifier
Abstract methods can use protected
access specifier
Abstract methods can use default
access specifier
Abstract class can inherit non-abstract
class
Abstract class can inherit non-abstract
class

Perfectly
valid
Complete Characteristics summary
1.
2.
3.
4.
5.
6.
7.

Abstract class can be empty.(slide 2)
Abstract class can be made without abstract methods. (slide 3)
A non-abstract class cannot contain abstract method.(slide 4)
Non-abstract class cannot contain abstract methods even there
are non abstract methods too.(slide 5)
An abstract class can contain both abstract and non abstract
methods.(slide 6)
Abstract class can be inherited like normal class(slide 7)
If abstract class contains no abstract methods then subclass of it,
can be empty(slide 8)
Complete Characteristics summary
8.
9.
10.
11.
12.
13.
14.
15.

If abstract class contains one or more abstract methods then subclass of
it, can not be empty(slide 9)
If abstract class contains one or more abstract methods then subclass of
it, can be empty, only if subclass is also abstract(slide 10)
If a abstract class contains abstract methods then subclass must have to
implements(write code) for abstract methods, if subclass does not want
to be abstract(slide 11)
Abstract class can implement super class abstract methods. (slide 12)
Abstract classes can contain final methods, constructors, static
methods.(slide 13,14,15)
An abstract class cannot be instantiated, but we can make reference of
this class.(slide 16)
Abstract methods cannot be private. They can have public, protected or
default access specifier.(slide 17,18,19,20)
Abstract class can extend non-abstract class.(slide 21)
Complete Characteristics summary
17.Class can be final or abstract, not both.(slide
22)
18.Method be can be final or abstract not
both.(slide 23)

Abstract classes and Methods in java

  • 1.
    Abstract classes andMethods in Java A Presentation in Depth
  • 2.
    Index Definition Usefulness Why they cannotbe instantiated? How to use abstract classes? Purpose of abstract class What is Concrete class? Examples Characteristics explained
  • 3.
    Definition Abstract classes—for whichyou never intend to create objects.
  • 4.
    Useful They’re used onlyas superclasses in inheritance hierarchies, we refer to them as abstract superclasses.
  • 5.
    Why they cannotbe instantiated? 1. These classes cannot be used to instantiate objects, because abstract classes are incomplete. 2. Abstract superclasses are too general to create real objects—they specify only what is common among subclasses.
  • 6.
    So how canwe use them? Or instantiate them? Subclasses must declare the “missing pieces” to become “concrete” classes, from which you can instantiate objects.
  • 7.
    What is thepurpose of abstract class? An abstract class’s purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design.
  • 8.
    What is concreteclass? Classes that can be used to instantiate objects are called concrete classes. Such classes provide implementations of every method they declare (some of the implementations can be inherited). Concrete classes provide the specifics that make it reasonable to instantiate objects.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
    Characteristics summary 1. Abstractclass can be empty. 2. Abstract class can be made without abstract methods.
  • 14.
    Characteristics summary 1. Abstractclass can be empty. 2. Abstract class can be made without abstract methods. 3. A non-abstract class cannot contain abstract method.
  • 16.
  • 17.
    Abstract class canbe empty abstract class Vehicle{}
  • 18.
    Abstract class canbe empty abstract class Vehicle{} Contains no members
  • 19.
    Abstract class canbe empty abstract class Vehicle{} Perfectly valid Contains no members
  • 21.
    Abstract class canbe made without abstract methods
  • 22.
    Abstract class canbe made without abstract methods abstract class Vehicle { void brake() { System.out.println("non abstract method brake"); } }
  • 23.
    Abstract class canbe made without abstract methods abstract class Vehicle { void brake() { System.out.println("non abstract method brake"); } } Non abstract method
  • 24.
    Abstract class canbe made without abstract methods abstract class Vehicle { void brake() { System.out.println("non abstract method brake"); } } Non abstract method Perfectly valid
  • 26.
  • 27.
    class Vehicle { abstract voidbrake(); } Invalid/compilation error
  • 28.
    A non-abstract classcannot contain abstract method class Vehicle { abstract void brake(); } Invalid/compilation error
  • 29.
    A non-abstract classcannot contain abstract method class Vehicle { abstract void brake(); } Invalid/compilation error In other words, if a class contains abstract method then class must also be abstract.
  • 30.
    A non-abstract classcannot contain abstract method class Vehicle { abstract void brake(); } Invalid/compilation error Now valid abstract class Vehicle { abstract void brake(); } In other words, if a class contains abstract method then class must also be abstract.
  • 31.
  • 32.
  • 33.
  • 34.
    Characteristics summary 4. Classcan be final or abstract, not both.
  • 35.
    Characteristics summary 4. Classcan be final or abstract, not both. 5. Method be can be final or abstract not both.
  • 36.
    Class can beabstract or final, not both
  • 37.
    Class can beabstract or final, not both Error
  • 38.
    Method can beabstract or final, not both
  • 39.
    Method can beabstract or final, not both Error
  • 40.
    Real Examples ofAbstract Classes in Java API 1. java.awt.Component Component Button Checkbox Label TextComponent
  • 41.
    Real Examples ofAbstract Classes in Java API 2. java.lang.Number Number Byte Long Integer Float Double
  • 42.
    Real Examples ofAbstract Classes in Java API 3. javax.swing.AbstractButton AbstractButton JButton JToggleButton JMenuItem
  • 43.
  • 44.
    Characteristics summary 6. Non-abstractclass cannot contain abstract methods even there are non abstract methods too.
  • 45.
    Characteristics summary 6. Non-abstractclass cannot contain abstract methods even there are non abstract methods too. 7. An abstract class can contain both abstract and non abstract methods.
  • 46.
    Characteristics summary 6. Non-abstractclass cannot contain abstract methods even there are non abstract methods too. 7. An abstract class can contain both abstract and non abstract methods. 8. Abstract class can be inherited like normal class.
  • 47.
    Characteristics summary 6. Non-abstractclass cannot contain abstract methods even there are non abstract methods too. 7. An abstract class can contain both abstract and non abstract methods. 8. Abstract class can be inherited like normal class. 9. If abstract class contains no abstract methods then subclass of it, can be empty.
  • 48.
    Non-abstract class cannotcontain abstract methods even there are non-abstract methods too
  • 49.
    Non-abstract class cannotcontain abstract methods even there are non-abstract methods too Non abstract method
  • 50.
    Non-abstract class cannotcontain abstract methods even there are non-abstract methods too Abstract method Non abstract method
  • 51.
    Non-abstract class cannotcontain abstract methods even there are non-abstract methods too Abstract method Non abstract method Either make the class abstract or make method non abstract to correct this error
  • 54.
    An abstract classcan contain both abstract and non abstract methods
  • 55.
    An abstract classcan contain both abstract and non abstract methods Non abstract method
  • 56.
    An abstract classcan contain both abstract and non abstract methods Abstract method Non abstract method
  • 57.
    Abstract class canbe inherited like normal class If abstract class is empty then subclass can also be empty.
  • 58.
    Abstract class canbe inherited like normal class If abstract class is empty then subclass can also be empty.
  • 59.
    Abstract class canbe inherited like normal class No error If abstract class is empty then subclass can also be empty.
  • 60.
    If abstract classcontains no abstract methods then subclass of it, can be empty
  • 61.
    If abstract classcontains no abstract methods then subclass of it, can be empty
  • 62.
    If abstract classcontains no abstract methods then subclass of it, can be empty Perfectly valid
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
    Characteristics summary 10. Ifabstract class contains one or more abstract methods then subclass of it, can not be empty.
  • 68.
    Characteristics summary 10. Ifabstract class contains one or more abstract methods then subclass of it, can not be empty. 11. If abstract class contains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract.
  • 69.
    Characteristics summary 10. Ifabstract class contains one or more abstract methods then subclass of it, can not be empty. 11. If abstract class contains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract. 12. If a abstract class contains abstract methods then subclass must have to implements(write code) for abstract methods, if subclass does not want to be abstract.
  • 70.
    If abstract classcontains one or more abstract methods then subclass of it, can not be empty
  • 71.
    If abstract classcontains one or more abstract methods then subclass of it, can not be empty
  • 72.
    If abstract classcontains one or more abstract methods then subclass of it, can not be empty Error
  • 73.
    If abstract classcontains one or more abstract methods then subclass of it, can not be empty Error
  • 74.
    If abstract classcontains one or more abstract methods then subclass of it, can not be empty Error There are two ways to correct this error either implement abstract methods in subclass or make subclass abstract.
  • 75.
    If abstract classcontains one or more abstract methods then subclass of it, can not be empty Error There are two ways to correct this error either implement abstract methods in subclass or make subclass abstract. Next slides will show how to remove this error
  • 76.
    If abstract classcontains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract
  • 77.
    If abstract classcontains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract
  • 78.
    If abstract classcontains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract Perfectly valid
  • 79.
    If a abstractclass contains abstract methods then subclass must have to implements(write code) for abstract methods, if subclass does not want to be abstract
  • 80.
    If a abstractclass contains abstract methods then subclass must have to implements(write code) for abstract methods, if subclass does not want to be abstract Perfectly valid
  • 81.
  • 82.
    Characteristics summary 13.Abstract classcan implement super class abstract methods.
  • 83.
    Characteristics summary 13.Abstract classcan implement super class abstract methods. 14.Abstract classes can contain final methods, constructors, static methods.
  • 84.
    Characteristics summary 13.Abstract classcan implement super class abstract methods. 14.Abstract classes can contain final methods, constructors, static methods. 15.An abstract class cannot be instantiated, but we can make reference of this class.
  • 85.
    If abstract classcontains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods.
  • 86.
    If abstract classcontains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods.
  • 87.
    If abstract classcontains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods. Abstract super class
  • 88.
    If abstract classcontains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods. Abstract super class Abstract sub class
  • 89.
    If abstract classcontains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods. Abstract super class Abstract sub class In other words, abstract class can implement super class abstract methods
  • 90.
    If abstract classcontains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods. Abstract super class Abstract sub class Perfectly valid In other words, abstract class can implement super class abstract methods
  • 91.
    Abstract classes cancontain constructors
  • 92.
    Abstract classes cancontain static methods
  • 93.
    Abstract classes cancontain static methods
  • 94.
    Abstract classes cancontain static methods
  • 95.
    Abstract classes cancontain static methods Output
  • 96.
    Abstract classes cancontain final methods
  • 97.
    Abstract classes cancontain final methods Perfectly valid
  • 98.
    An abstract classcannot be instantiated
  • 99.
    An abstract classcannot be instantiated We can make reference of abstract class
  • 100.
    An abstract classcannot be instantiated We can make reference of abstract class Error
  • 101.
  • 102.
  • 103.
    Characteristics summary 8. Abstractmethods cannot be private. They can have public, protected or default access specifier. 9. Abstract class can extend non-abstract class.
  • 104.
  • 105.
    Abstract methods cannotbe private Error
  • 106.
    Abstract methods canuse public access specifier
  • 107.
    Abstract methods canuse protected access specifier
  • 108.
    Abstract methods canuse default access specifier
  • 109.
    Abstract class caninherit non-abstract class
  • 110.
    Abstract class caninherit non-abstract class Perfectly valid
  • 111.
    Complete Characteristics summary 1. 2. 3. 4. 5. 6. 7. Abstractclass can be empty.(slide 2) Abstract class can be made without abstract methods. (slide 3) A non-abstract class cannot contain abstract method.(slide 4) Non-abstract class cannot contain abstract methods even there are non abstract methods too.(slide 5) An abstract class can contain both abstract and non abstract methods.(slide 6) Abstract class can be inherited like normal class(slide 7) If abstract class contains no abstract methods then subclass of it, can be empty(slide 8)
  • 112.
    Complete Characteristics summary 8. 9. 10. 11. 12. 13. 14. 15. Ifabstract class contains one or more abstract methods then subclass of it, can not be empty(slide 9) If abstract class contains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract(slide 10) If a abstract class contains abstract methods then subclass must have to implements(write code) for abstract methods, if subclass does not want to be abstract(slide 11) Abstract class can implement super class abstract methods. (slide 12) Abstract classes can contain final methods, constructors, static methods.(slide 13,14,15) An abstract class cannot be instantiated, but we can make reference of this class.(slide 16) Abstract methods cannot be private. They can have public, protected or default access specifier.(slide 17,18,19,20) Abstract class can extend non-abstract class.(slide 21)
  • 113.
    Complete Characteristics summary 17.Classcan be final or abstract, not both.(slide 22) 18.Method be can be final or abstract not both.(slide 23)