Chap2.2 Abstractclassandinterface
Chap2.2 Abstractclassandinterface
2.2
Abstract Class
& Interface
Chapter Abstract Class & Interface
2:
Objectives
At the end of this lesson, you will be able to
understand and create:
Abstract class
Interface
Abstraction
What is abstraction?
2. Interface
Abstract Class & Interface
Abstract Class
A class which is declared as abstract is
known as an abstract class
It needs to be extended and its method must
be implemented
It cannot be instantiated
Abstract class can have abstract and non-
abstract methods
It can have constructors and static methods
It can have final methods which will force the
subclass not to change the body of the method
Abstract Class & Interface
Abstract class
abstract Keyword
abstract is the keyword used to generalized a
class or method.
Syntax
Access_modifier abstract class ClassName {
// variables or fields
// methods
// abstract method
Access_modifier abstract return_type methodName(parameter list);
// abstract method has no method body
}
Abstract Class & Interface
Abstract class
Shape
- color : String
+getArea() : double
Rectangle Triangle
- length : double - base : double
- Width : double - height : double
Interface
An interface in java is a blueprint of a class.
Interface can have static constants and
abstract methods
There can be only abstract methods in the
Java interface, not method body.
It is used to achieve abstraction and multiple
inheritance in Java
Interface cannot be instantiated
A class that implements an interface must
implement all the methods declared in the
interface
Abstract Class & Interface
Interface
interface Keyword
interface is the keyword used to provide total
abstraction.
Syntax
Access_modifier interface interfaceName {
// declare constant fields
// declare methods header
// method in interface has no method body
}
Abstract Class & Interface
Difference between Class and Interface
Class Interface
Interface
Multiple inheritance in Java by interface
Abstract Class & Interface
Interface
<<interface>> <<interface>>
Behavior Animal
+ family : String
+ eat() : void + name() : void
+ move() : void + leg() : void
+ sound() : void + body() : void
Cat Fish
}
Abstract Class & Interface
Difference between Abstract Class and
Interface
Abstraction Interface
An abstract class can have both The interface can have only abstract
abstract and non-abstract methods. methods.