Abstract Class, Interface: Csi 211: Object Oriented Programming
Abstract Class, Interface: Csi 211: Object Oriented Programming
INTERFACE
CSI 211: OBJECT ORIENTED PROGRAMMING
Tanjina Helaly
Meaning of Abstract
• Adjective
• existing in thought or as an idea but not having a physical or
concrete existence.
• Verb:
• consider something theoretically or separately from
(something else).
Abstraction
• Abstraction is a process of hiding the implementation
details from the user, only the functionality will be
provided to the user.
• In other words, the user will have the information on
what the object does instead of how it does it.
• In Java, abstraction is achieved using
• Abstract classes and
• interfaces.
Abstraction
In OOP ( Object Oriented Programming ) ,
Abstraction facilitates the easy conceptualization of
real world objects into the software program.
Humans manage complexity through abstraction.
Think about a car.
Do you think of a car as a set of tens of thousands of individual
parts?
No, we think of it as a well-defined object with its own unique
behavior.
This abstraction allows people to use a car without
being overwhelmed by the complexity of the parts
that form the car.
Abstract Class
• Abstract classes
• Are superclasses (called abstract superclasses) that
• Cannot be instantiated
• Incomplete
• subclasses fill in "missing pieces“
• Contains zero or more abstract method.
Note:
• Abstract class has all the features and use of
normal/concrete class. Only differences are
• Can not be instantiated
• Must create a Child class
• Can contain abstract method
• If there are abstract methods, child class must override those methods.
Abstract Class– example (no abstract
method)
abstract class Animal{
// instance variables
String name, color;
double weight;
// Constructors
Animal(){ }
// Concrete methods
public void eat(){
System.out.println(name + " eats.");
}
}
Abstract Class– example(with abstract
method)
abstract class Animal{
// instance variables
String name, color;
double weight;
// Constructors
Animal(){ }
// Concrete methods
public void eat(){
System.out.println(name + " eats.");
}
// abstract methods
public abstract void makeSound();
}
Extending an abstract class
• A class normally extends an abstract class.
• When a class extends an abstract class, the class must
override all the abstract methods declared in the
interface.
• If a class does not override all the behaviors/methods of
the abstract parent class, the class must declare itself as
abstract.
Extending an abstract class - example
class Bird extends Animal{
public Bird() {
name = "Bird";
}
@Override
public void makeSound() {
System.out.println("Chirp");
}
}
Output:
Bird eats.
Tiger eats.
Chirp
Roar
Abstract Class – another example
Abstract Class
Concrete Classes
15
Abstraction vs. encapsulation
• Abstraction represent taking out the behavior from
how exactly its implemented,
• one example of abstraction in Java is interface
• Encapsulation means hiding details of
implementation from outside world so that when
things change no body gets affected.
• One example of Encapsulation in Java is private methods;
clients don't care about it, You can change, amend or even
remove that method if that method is not encapsulated and
it were public all your clients would have been affected.
Abstraction vs. encapsulation
• Encapsulation(hiding complexity) implements of
abstraction(show what is only necessary)
• Abstraction is the thought process or model
• Encapsulation is the implementation
Interface
Interface
• Using the keyword interface, you can fully abstract a
class’ interface from its implementation.
• That is, using interface, you can specify what a class
must do, but not how it does it.
• Once it is defined, any number of classes can
implement an interface.
• Also, one class can implement any number of
interfaces.
Interface
• Interfaces are syntactically similar to classes, but they
can only
• Have fields that are final and static. (even if they are not explicitly
declared as such.)
• Can contains only public abstract methods. (even though the
interface might not say so)
• Interfaces have the same access levels as classes, public
and package.
• An interface, like a class, defines a type.
• Fields, variables, and parameters can be declared to be of a type
defined by an interface.
Interface – What can’t do
• Interfaces can not have
• instance variables.
• All fields in an interface are final and static even if they are not explicitly
declared as such.
• Constructor
• Normal/concrete method
• Like abstract class, one can not make an object from an
interface.
Interface – how to declare
public interface NameOfInterface {
// Any number of final, static fields
// Any number of abstract method declarations
}
Note:
Example
interface Flyable{
public static final String media = "Sky";
void fly();
boolean needFuel();
}
Interface - how to declare
•Compiler do the following conversion
interface Flyable{
String media = "Sky";
void fly();
boolean needFuel();
}
Compiler
interface Flyable{
public static final String media =
"Sky";
@Override
public boolean needFuel() {
return false;
}
}
Implementing interface - example
class Airplane implements Flyable{
@Override
public void fly() {
System.out.println("Plane can fly in the " + Flyable.media);
}
@Override
public boolean needFuel() {
return true;
} Output:
} Plane can fly in the Sky
Bird can fly in the Sky
public class TestInterface {
public static void main(String[] args) {
Bird b = new Bird();
Airplane a = new Airplane ();
a.fly();
b.fly();
}
}
Implementing interface – some rules
• A class can implement more than one interface at a time.
• Each interface name is separated by comma after the implements
keyword.
• The class must override all methods of all interfaces.
• A class can both extends a class and implements many
interfaces.
• An interface can extend other interfaces (allow multiple
extends), in a similar way as a class can extend another
class.
• If a parent class implements an interface, its child
classes automatically implement the interface.
Implementing multiple interfaces -
example
interface Flyable{
public static final String media = "Sky";
interface Floatable{
public abstract void canFloat();
}
interface Floatable{
public abstract void canFloat();
}
class Animal{
String name, color; “extends” should appear
before “implements”
}
A (Concrete Class)
B C
When to use - Abstract Class vs. Interface
• If a method in B is so different from the same method in
C, there is no shared implementation possible in A.
• We can make the method and A an abstract classes. The methods
in A then indicate which methods must be implemented in B and C.
A can act as type, which can hold objects of type B or C.
A (Abstract Class)
B C
When to use - Abstract Class vs. Interface
• But if all the methods of B must be implemented
differently than the same method in C. And there is no
common attributes between B and C, make A an
interface.
A (interface)
B C
Reference
• Java: Complete Reference: Chapter 7,8, 9
• Java: How to Program: Chapter 10
• Online Reference:
• https://www.tutorialspoint.com/java/java_interfaces.htm
• http://www.javatpoint.com/interface-in-java
• https://www.youtube.com/watch?v=1Q4I63-hKcY
• https://www.youtube.com/watch?v=yyU3bXyc_oU