0% found this document useful (0 votes)
19 views26 pages

06 - Abstract Class and Interface-MJS

The document discusses abstract classes and interfaces in object-oriented programming. It provides examples of when to use abstract classes and interfaces, such as when the implementation of some methods varies between child classes, or to define common behaviors without inheritance. It also compares abstract classes and interfaces, such as that interfaces can only contain public abstract methods while abstract classes can contain other types of methods.

Uploaded by

Rifqi Fauzani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views26 pages

06 - Abstract Class and Interface-MJS

The document discusses abstract classes and interfaces in object-oriented programming. It provides examples of when to use abstract classes and interfaces, such as when the implementation of some methods varies between child classes, or to define common behaviors without inheritance. It also compares abstract classes and interfaces, such as that interfaces can only contain public abstract methods while abstract classes can contain other types of methods.

Uploaded by

Rifqi Fauzani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

CII3B4

Pemrograman Berorientasi Objek

Abstract Class and


Interface
Abstract
Abstract Method
Method with no implementation or specific behavior
Handed over to the child class to implement its own
– Parent class only declare the name of method
Child class must implement or specify the method
– Free to specify

Use keyword "extends"


Abstract Class
A class with at least one abstract method must be
declared abstract class
An abstract class cannot be instantiated
– As the instance must have already implement the abstract
methods

Abstract class will ensure the child implements the


declared method
Abstract Class Diagram
Class diagram of an AbstractStyle
abstract
– Italic class name + normalMethod()
+ abstractMethod()
– Italic method name if
abstract

Child extends the


abstract parent class
– Child must implement all Painting
abstract method
– Or child will be declared + abstractMethod()
abstract too
Example
public abstract class AbstractParent{

public String toString(){


return "this is class Parent";
}

public abstract void abstractMethod();

public class Child extends AbstractParent{

public void abstractMethod(){


// implementing abstract method
}

}
When to use/create Abstract class
The implementation of some methods are too
vary for each child
No concrete/actual object for the parent class
– No such object exists
– Parent doesn’t need to be instantiated

Make a condition that the child class will have to


implement some specific methods
Example: Animal Hierarchy
Animal Both tigers, fish, and
- Weight snakes are animals and
they all can move, but
+ move()
+ breath() their ‘moving’ behavior
are really different

Tiger Fish Snake


We don’t need to
specify the behavior in
+ move() + move() + move() animal class, let the
+ roar() + molting() child classes define
themselves
another view:
We make sure that
every animal can move
Interface
Interface
A special type of class which define a set of
method prototypes
To deliver a rule or mechanism to it’s child (class
that implements)
An interface can only contain
– Public, Abstract method
– Public, static, Final attributes
– No constructor
Interface
Use keyword "implements"
A class
– Can "extend" only one class i.e. only one superclass
– Can "implements" multiple interfaces

Interfaces don’t have instance fields


– they are merely a set of methods that the object
implementing the interface methods must define
Interface Class Diagram
Class diagram of an <<interface>>
Cloneable
interface
Color = white
– Tag <<interface>>
+shootWeapon()

When class A
implements Interface I,
Trooper
the diagram was
denoted as
+ shootWeapon()
Interface
Like an abstract class, interface cannot be
instantiated
A class that implements interface(s) must
implements all abstract methods
An abstract class can also implements interface(s)
An abstract class that implements interface(s)
may not implements all abstract methods
Interface
Practical use of interfaces enables the separation
of the interface and implementation of an object
– The interface is provided by the interface definition
– The implementation is provided by the classes which
implement the interface methods
Interface
Interface usually named "…… - able"
(but not always)
– Serializable
– Runnable
– Cloneable

Meaning that the implementing class able to do


anything as the interface told
– Implement interface to be able of doing some predefined
tasks
Interface
Implementing an interface allows a class to
become more formal about the behavior it
promises to provide
Interfaces form a contract between the class and
the outside world,
– this contract is enforced at build time by the compiler.
Example
Interface Instagramable{

public abstract void takePicture();


public abstract void setFilter();
public abstract void sharePhoto();
}

public class Kitten implements Instagramable{

public void takePicture(){


// implementing abstract method
}

public void setFilter(){


// implementing abstract method
}

public void sharePhoto(){


// implementing abstract method
}
}
When to use/create Interface
Define some rules or mechanisms
Group classes with no inheritance relationship
Create an API
Example: Animal Hierarchy
Animal
- Weight
- Height

Mammal Birds
- Fur/hair - Wings
- mammaryGland - beak

Whale Tiger Eagle Penguin

Bat Ostrich
Example: Animal Hierarchy
Animal whales and penguins
are aquatic animals
and can swim, but
Mammal Birds
bats, tigers, eagles and
ostriches can not

Whale Tiger Eagle Penguin


we can not put a
Bat Ostrich swimming behavior in
mammal, bird or
We also can not put animal class as not all
penguins and whales mammals, birds and
have the same parent animals can swim
class as they both are
different
Example: Animal Hierarchy
Animal Here we can create an
Aquatic Animal
Interface that defines
Mammal Birds
that animals that
implements this
interface will be able to
Whale Tiger Eagle Penguin swim and dive
Bat Ostrich

Then we can make the


whales and penguins
implement Aquatic
<<interface>> interface behavior
Aquatic
+ swim()
+ dive()
Example: Animal Hierarchy
Animal Another example,
tigers, eagles and
penguins are
Mammal Birds
carnivorous, while the
rest are not

Whale Tiger Eagle Penguin Create a Carnivorous


Bat Ostrich Interface, and make
the carnivorous
animals implement the
behaviors

<<interface>> <<interface>>
Aquatic Carnivorous
+ swim() + eatMeat()
+ dive()
Example: Animal Hierarchy
Animal
One class can only have
one parent
One class may implement
Mammal Birds
many interface

Whale Tiger Eagle Penguin

Bat Ostrich

<<interface>> <<interface>> <<interface>>


Aquatic CanFly Carnivorous
+ swim() + fly() + eatMeat()
+ dive()
Example: Taxable Goods
Goods Toys and Books are
- price : int classified as taxable
goods, while foods are not
+ display()

Food Toy Book


- calories - minimumAge - author

<<interface>>
Taxable
- taxRate = 0.06

+ calculateTax()
Question?
THANK YOU

You might also like