Abstract classes and Methods in java

Preview:

DESCRIPTION

Index Definition Usefulness Why they cannot be instantiated? How to use abstract classes? Purpose of abstract class What is Concrete class? Examples Characteristics explained

Citation preview

Abstract classes and Methods inJava

A Presentation in Depth

Index

DefinitionUsefulnessWhy they cannot be instantiated?How to use abstract classes?Purpose of abstract classWhat is Concrete class?ExamplesCharacteristics explained

Definition

Abstract classes—for which you never intend tocreate objects.

Useful

They’re used only as superclasses in inheritancehierarchies, we refer to them as abstractsuperclasses.

Why they cannot be instantiated?

1. These classes cannot be used to instantiateobjects, because abstract classes areincomplete.

2. Abstract superclasses are too general tocreate real objects—they specify only what iscommon among subclasses.

So how can we use them? Orinstantiate them?

Subclasses must declare the “missing pieces” tobecome “concrete” classes, from which you caninstantiate objects.

What is the purpose of abstract class?

An abstract class’s purpose is to provide anappropriate superclass from which other classescan inherit and thus share a common design.

What is concrete class?

Classes that can be used to instantiate objectsare called concrete classes. Such classes provideimplementations of every method they declare(some of the implementations can be inherited).

Concrete classes provide the specifics that makeit 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 abstractmethods.

Characteristics summary

1. Abstract class can be empty.

2. Abstract class can be made without abstractmethods.

3. A non-abstract class cannot contain abstractmethod.

Abstract class can be empty

Abstract class can be empty

abstract class Vehicle{}

Abstract class can be empty

abstract class Vehicle{}

Contains nomembers

Abstract class can be empty

abstract class Vehicle{}

Contains nomembersPerfectly validPerfectly valid

Abstract class can be made withoutabstract methods

Abstract class can be made withoutabstract methods

abstract class Vehicle{

void brake(){System.out.println("non abstract method brake");

}}

Abstract class can be made withoutabstract methods

abstract class Vehicle{

void brake(){System.out.println("non abstract method brake");

}}

Non abstractmethod

Non abstractmethod

Abstract class can be made withoutabstract methods

abstract class Vehicle{

void brake(){System.out.println("non abstract method brake");

}}

Perfectlyvalid

Perfectlyvalid

Non abstractmethod

Non abstractmethod

class Vehicle{

abstract void brake();}

class Vehicle{

abstract void brake();}

Invalid/compilationerror

A non-abstract class cannot containabstract method

class Vehicle{

abstract void brake();}

Invalid/compilationerror

A non-abstract class cannot containabstract method

class Vehicle{

abstract void brake();}

Invalid/compilationerror

In other words, ifa class contains

abstract methodthen class mustalso be abstract.

In other words, ifa class contains

abstract methodthen class mustalso be abstract.

A non-abstract class cannot containabstract method

class Vehicle{

abstract void brake();}

Invalid/compilationerror

abstract class Vehicle{

abstract void brake();}

Now valid

In other words, ifa class contains

abstract methodthen class mustalso be abstract.

In other words, ifa class contains

abstract methodthen class mustalso 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

ErrorError

Method can be abstract or final, notboth

Method can be abstract or final, notboth

ErrorError

Real Examples of Abstract Classes inJava API

1. java.awt.Component

Component

Button Checkbox Label TextComponent

Real Examples of Abstract Classes inJava API

2. java.lang.Number

Number

Byte Long Integer Float Double

Real Examples of Abstract Classes inJava API

3. javax.swing.AbstractButton

AbstractButtonAbstractButton

JButtonJButton JToggleButtonJToggleButton JMenuItemJMenuItem

Characteristics summary

Characteristics summary

6. Non-abstract class cannot contain abstract methods eventhere are non abstract methods too.

Characteristics summary

6. Non-abstract class cannot contain abstract methods eventhere are non abstract methods too.

7. An abstract class can contain both abstract and non abstractmethods.

Characteristics summary

6. Non-abstract class cannot contain abstract methods eventhere are non abstract methods too.

7. An abstract class can contain both abstract and non abstractmethods.

8. Abstract class can be inherited like normal class.

Characteristics summary

6. Non-abstract class cannot contain abstract methods eventhere are non abstract methods too.

7. An abstract class can contain both abstract and non abstractmethods.

8. Abstract class can be inherited like normal class.

9. If abstract class contains no abstract methods then subclassof it, can be empty.

Non-abstract class cannot contain abstractmethods even there are non-abstract methods

too

Non-abstract class cannot contain abstractmethods even there are non-abstract methods

too

Non abstractmethod

Non abstractmethod

Non-abstract class cannot contain abstractmethods even there are non-abstract methods

too

AbstractmethodAbstractmethod

Non abstractmethod

Non abstractmethod

Non-abstract class cannot contain abstractmethods even there are non-abstract methods

too

AbstractmethodAbstractmethod

Non abstractmethod

Non abstractmethod

Either make the classabstract or make method

non abstract to correctthis error

Either make the classabstract or make method

non abstract to correctthis error

An abstract class can contain bothabstract and non abstract methods

An abstract class can contain bothabstract and non abstract methods

Nonabstractmethod

Nonabstractmethod

An abstract class can contain bothabstract and non abstract methods

Nonabstractmethod

Nonabstractmethod

AbstractmethodAbstractmethod

Abstract class can be inherited likenormal class

If abstract classis empty thensubclass can

also be empty.

If abstract classis empty thensubclass can

also be empty.

Abstract class can be inherited likenormal class

If abstract classis empty thensubclass can

also be empty.

If abstract classis empty thensubclass can

also be empty.

Abstract class can be inherited likenormal class

No errorIf abstract classis empty thensubclass can

also be empty.

If abstract classis empty thensubclass can

also be empty.

If abstract class contains no abstractmethods then subclass of it, can be empty

If abstract class contains no abstractmethods then subclass of it, can be empty

If abstract class contains no abstractmethods then subclass of it, can be empty

Perfectlyvalid

Perfectlyvalid

Example 5

Example 6

Example 7

Characteristics summary

Characteristics summary10. If abstract class contains one or more abstract methods

then subclass of it, can not be empty.

Characteristics summary10. 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 methodsthen subclass of it, can be empty, only if subclass is alsoabstract.

Characteristics summary10. 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 methodsthen subclass of it, can be empty, only if subclass is alsoabstract.

12. If a abstract class contains abstract methods then subclassmust have to implements(write code) for abstractmethods, if subclass does not want to be abstract.

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

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

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

ErrorError

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

ErrorError

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

There are two ways to correctthis error either implement

abstract methods in subclass ormake subclass abstract.

ErrorError

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

There are two ways to correctthis error either implement

abstract methods in subclass ormake subclass abstract.

ErrorError

Next slideswill show

how toremove this

error

Next slideswill show

how toremove this

error

If abstract class contains one or more abstractmethods then subclass of it, can be empty, only

if subclass is also abstract

If abstract class contains one or more abstractmethods then subclass of it, can be empty, only

if subclass is also abstract

If abstract class contains one or more abstractmethods then subclass of it, can be empty, only

if subclass is also abstract

Perfectlyvalid

Perfectlyvalid

If a abstract class contains abstract methods then subclass musthave to implements(write code) for abstract methods, if subclass

does not want to be abstract

If a abstract class contains abstract methods then subclass musthave to implements(write code) for abstract methods, if subclass

does not want to be abstract

Perfectlyvalid

Perfectlyvalid

Characteristics summary

Characteristics summary13.Abstract class can implement super class

abstract methods.

Characteristics summary13.Abstract class can implement super class

abstract methods.

14.Abstract classes can contain final methods,constructors, static methods.

Characteristics summary13.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 wecan make reference of this class.

If abstract class contains one or more abstractmethods then subclass of it, can be abstract and still

can implements super class methods.

If abstract class contains one or more abstractmethods then subclass of it, can be abstract and still

can implements super class methods.

If abstract class contains one or more abstractmethods then subclass of it, can be abstract and still

can implements super class methods. Abstractsuper class

If abstract class contains one or more abstractmethods then subclass of it, can be abstract and still

can implements super class methods.

Abstractsub class

Abstractsuper class

If abstract class contains one or more abstractmethods then subclass of it, can be abstract and still

can implements super class methods.

In other words,abstract class canimplement super

class abstractmethods

In other words,abstract class canimplement super

class abstractmethods

Abstractsub class

Abstractsuper class

If abstract class contains one or more abstractmethods then subclass of it, can be abstract and still

can implements super class methods.

In other words,abstract class canimplement super

class abstractmethods

In other words,abstract class canimplement super

class abstractmethods

Perfectlyvalid

Perfectlyvalid

Abstractsub class

Abstractsuper class

Abstract classes can containconstructors

Abstract classes can contain staticmethods

Abstract classes can contain staticmethods

Abstract classes can contain staticmethods

Abstract classes can contain staticmethods

Output

Abstract classes can contain finalmethods

Abstract classes can contain finalmethods

Perfectlyvalid

Perfectlyvalid

An abstract class cannot beinstantiated

An abstract class cannot beinstantiated

We can make reference ofabstract class

An abstract class cannot beinstantiated

Error

We can make reference ofabstract class

Example 8

Example 9

Characteristics summary

8. Abstract methods cannot be private. Theycan have public, protected or default accessspecifier.

9. Abstract class can extend non-abstract class.

Abstract methods cannot be private

Abstract methods cannot be private

ErrorError

Abstract methods can use publicaccess specifier

Abstract methods can use protectedaccess specifier

Abstract methods can use defaultaccess specifier

Abstract class can inherit non-abstractclass

Abstract class can inherit non-abstractclass

Perfectlyvalid

Perfectlyvalid

Complete Characteristics summary

1. Abstract class can be empty.(slide 2)2. Abstract class can be made without abstract methods. (slide 3)3. A non-abstract class cannot contain abstract method.(slide 4)4. Non-abstract class cannot contain abstract methods even there

are non abstract methods too.(slide 5)5. An abstract class can contain both abstract and non abstract

methods.(slide 6)6. Abstract class can be inherited like normal class(slide 7)7. If abstract class contains no abstract methods then subclass of it,

can be empty(slide 8)

Complete Characteristics summary8. If abstract class contains one or more abstract methods then subclass of

it, can not be empty(slide 9)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)10. If a abstract class contains abstract methods then subclass must have to

implements(write code) for abstract methods, if subclass does not wantto be abstract(slide 11)

11. Abstract class can implement super class abstract methods. (slide 12)12. Abstract classes can contain final methods, constructors, static

methods.(slide 13,14,15)13. An abstract class cannot be instantiated, but we can make reference of

this class.(slide 16)14. Abstract methods cannot be private. They can have public, protected or

default access specifier.(slide 17,18,19,20)15. Abstract class can extend non-abstract class.(slide 21)

Complete Characteristics summary

17.Class can be final or abstract, not both.(slide22)

18.Method be can be final or abstract notboth.(slide 23)

Recommended