Object Oriented Programming
Chapter 1: Abstract Classes and
Interface
Arsi University
Department MIS
Prepared Debritu A.
Course Content
2
Introduction
Abstract Classes in Java
Java Interfaces
Declaring Interfaces
Implementing Interface
Extending Interface
OOP –MIS-AU
Introduction to Abstraction
3
Essential element of object-oriented programming.
Manages complexity.
Managed through the use of hierarchical classifications.
Allows you to layer the semantics of complex systems, breaking
them into more manageable pieces.
Process of hiding the implementation details of a class, and
showing only functionality to the user.
Abstraction lets you focus on what the object does instead of how it
does it.
It can be achieved using Abstract classes and Interfaces.
OOP –MIS-AU
Abstract Classes in Java
4
Class that is declared with abstract keyword.
It can have abstract and non-abstract methods.
Abstract method- declared as abstract but not defined (has no
implementation).
Non-abstract method- declared and defined (has implementation).
Good for defining a general category containing specific, “concrete”
classes.
Concrete classes are those that are specific enough to be instantiated.
OOP –MIS-AU
Java Interfaces
5
Way
to describe what classes should do, without
specifying how they should do it.
A collection of abstract methods.
Two different concepts:
Class =attributes and behaviors
An interface contains behaviors that a class implements.
OOP –MIS-AU
Java Interfaces …
6
Similarities:
An
interface can contain any number of methods.
An
interface is written in a file with a .java extension, with the name of
the interface matching the name of the file.
The
bytecode of an interface appears in a .class file.
Interfaces
appear in packages, and their corresponding bytecode file
must be in a directory structure that matches the package name.
OOP –MIS-AU
Java Interfaces …
7
Difference
You
cannot instantiate an interface.
An
interface does not contain any constructors.
All
of the methods in an interface are abstract.
An
interface cannot contain instance fields.
An
interface is not extended by a class; it is implemented by a
class.
An
interface can extend multiple interfaces
OOP –MIS-AU
Java Interfaces …
8
Declaring Interfaces
Interface
keyword is used to declare an interface.
access interface Nameofinterface{
return-type method-name(parameter-list);
}
Methods
that are declared have no bodies
End with a semicolon after the parameter list.
OOP –MIS-AU
Java Interfaces …
9
Properties of Interfaces:
An
interface is implicitly abstract. no need of using abstract
keyword
Each
method in an interface is also implicitly abstract.
Methods
in an interface are implicitly public.
Public interface Animal{
Public void eat();
Public void travel();
}
OOP –MIS-AU
Java Interfaces …
10
Implementing Interfaces:
When
a class implements an interface, class signing a contract,
agreeing to perform the specific behaviors of the interface.
If a class does not perform all the behaviors of the interface,
the class must declared as abstract.
Class
OOP –MIS-AU
uses the implements keyword to implement an interface.
Java Interfaces …
11
Implementing Interfaces: …
Public class mammal implemets Animal{
Public void eat(){
System.out.println(“mammal eat”);
}
Public void travel(){
System.out.println(“mammal travel”);
}
Public static void main(String args[]){
Mammal m =new mammal();
m.eat();
m.travel();
}
}
OOP –MIS-AU
Extending Interfaces
12
Interface can extend another interface, similarly to the way
that a class can extend another class.
Extends keyword is used to extend an interface, and the child
interface inherits the methods of the parent interface.
Interface can extend more than one parent interface.
Extends keyword is used once, and the parent interfaces are declared in a
comma-separated list.
Example: if the Hockey interface extends both Sports and
Event, it would be declared as:
Public interface Hockey extends Sports, Event
OOP–MIS-AU
Extending Interfaces…
13
Differences between Abstract classes and Interfaces:
OOP–MIS-AU
Thank you!!!
14
End of chapter One!
OOP-MIS-AU