INF101: Object-Oriented Programming
Lecture 5: Abstract Classes and Interfaces
Welcome to INF101 – Lecture 4!
• Previously:
– Designing our first duck pond simulator.
– Enums
– Passing by Value
Enums
Enums
Passing by Value
Passing by Value
Passing by Value
Objects though…
Objects though…
The reveal
Today
• Continuing our first duck pond simulator
• Abstraction
• Abstract classes
• Interfaces
Questions?
Abstraction
Four main concepts in OOP
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
Four main concepts in OOP
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
Encapsulation
• Bundling properties and methods of objects in classes
• Wrapping the implementation (code) and the data it manipulates (variables) in the same class
Encapsulation in Java
• Can also code without encapsulation in Java by making field variables public (but this is not the way of the OOP ninja ;))
• OOP: Private fields, with public methods to get and set them
Abstraction
• Interface concept: Set of methods and fields that the class makes public for other objects to
access
• How it does what it does: hidden in private properties and methods
– Reduces complexity
– We can change it later without affecting other classes
Inheritance
• Eliminate redundant code by inheriting properties and methods from a superclass
• New class that derives from superclass is subclass
• Subclass automatically gets all fields and methods from superclass
• Implements ‘is-a-type-of’ relationships
Four main concepts in OOP
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
Different Types of Abstraction
• Abstraction by parametrization
• Abstraction by specification
• Control abstraction
• Data abstraction
Abstraction by parametrization
• Abstract away from concrete values in functions by adding parameters
Abstraction by specification
• Abstract away from how something is done by precisely describing what is done/what is result
• For instance: Math.sqrt(x) results in Math.sqrt(x)*Math.sqrt(x) == x
• Users are interested in what a function does, but only the implementing class needs to know how it works
Control abstraction
• Abstract away which concrete steps the computer will do
• When you make a method, you make a control abstraction: a new, simple, (hopefully) descriptive name for a series of actions
• Java also has control abstraction, you can use if without knowing how it is implemented and what steps it will do internally exactly
Data abstraction
• Abstract away how data is represented, but only focus on what you can do with it
• You can add() to a list, and move() a duck without knowing how they are made
• When you make a new class, you are doing data abstraction
• Java also does it, you can use an int without knowing how it is implemented
Questions
Abstract Classes
Abstract keyword in Java
• Methods and classes can be abstract:
• abstract method: prototype for a method, has:
– a return type – a name
– a list of parameters
Abstract methods
• Use the abstract keyword
• ; instead of a method body
Abstract classes
• If your class features at least one abstract method, it is also abstract
Abstract classes
• If your class features at least one abstract method, it is also abstract
Abstract classes cannot be
instantiated
Why not?
• If I would make a ball object, and call the hit method, what would it do?
• It wouldn’t know what to do
Abstract Classes
• Can also have non-abstract methods:
Abstract Classes
• Can also only have non-abstract methods
So what can you do with them?
• Combine with inheritance
So what can you do with them?
• Combine with inheritance, using ‘extends’
keyword
Subclassing an Abstract Class
• Subclass must provide implementation for every abstract method (if not, subclass becomes
abstract also!)
• If I make a FootBall class, I need to implement a hit method, or it will be an abstract FootBall
When are abstract classes used?
• Want to create a generic type that is a superclass for two or more subclasses, but the superclass is not an actual object
Back to our Pond
• We had ducks already swimming in the pond
• We want frogs too!
• We could say that both classes are PondObjects:
a PondObject is not an actual object, but its subclasses are
Further abstract ‘rules’
• A private method can’t be abstract (a subclass can’t override a private method, and abstract methods must be overridden)
• Can’t create an instance of an abstract class, but can declare a variable by using an abstract class as its type. The variable to refer to an instance of any of the subclasses of the abstract class.
Further abstract ‘rules’
• Can’t create an instance of an abstract class, but can declare a variable by using an abstract class as its type. The variable can refer to an instance of any of the subclasses of the abstract class.
Questions?
Break
About the labs
Please do the labs ☺
• Gruppeledere are ready to help you
• Part of optimal preparation for compulsory assignments and exam
Exercise
Exercise
Interfaces
Return to the Pond
• We want more than just ducks. How about frogs?
We already had a Duck
And a Pond
Now add a Frog
Frog class
• (copy of duck, but green!)
Add to the Pond
Are we going to duplicate code for every new pond inhabitant?
• There’s got to be another way!
Abstract Classes
What if I just want to describe a recipe?
• For instance, I want to describe what all
PondObjects should be able to do (step and draw)
• Then, if I get an object of a class that implements PondObject, I know it will be able to do those
things ‘by contract’
Interfaces
• Similar to abstract class, but can only have
abstract methods and final variables (constants that cannot be modified)
• A class implements an interface by providing code for each declared method
Back to the pond
<<interface>>
IPondObject
Duck Frog
step() draw(painter)
Pond update
More on Interfaces
• Naming convention: often adjectives (Playable, Readable, Drivable, Pondable?)
• Classes that adhere to the interface contract
should use the keyword implements in their class definition
You can use an interface as a type
• But not instantiate it (similar to abstract classes)
• You can use it in type declaration, parameters, or method return values
Interface definition
• Methods are always public and abstract
• Fields are always public, final and static (can’t change)
• Since they always are, you don’t need to include those keywords
Combining interfaces with inheritance
http://www.cs.sjsu.edu/~pearce/modules/lectures/oop/basics/interfaces.htm
Interface inheritance
http://www.cs.sjsu.edu/~pearce/modules/lectures/oop/basics/interfaces.htm
Exercise
• Take 5 mins to draw a possible UML for the following situation using interfaces:
• Ducks, penguins, and chickens are all birds.
Every bird can walk. Only some of these birds can fly (ducks, and chickens), and some of these birds can swim (ducks, penguins)
What if I later decide to add a method to all pond objects?
• Could add another abstract method, but then all subclasses need to add an implementation… :*(
• In Java 8, you can also add a default method
• If implementing classes do not have this default method, they will use the interface one ☺
Default methods
Abstract classes vs Interfaces
Abstract Class Interface
Declaration abstract class Foo {..} interface Foo {..}
Methods Some or no abstract
methods
All methods have no body (are abstract)
Instantiation - -
Inheritance Abstract class can extend only one class
An interface can extend many interfaces
Implementation A class can extend only one abstract class (if doesn’t implement all methods: also abstract)
A class can implement any number of interfaces
Field types public, protected, private, static, final
Only public, static, final
Method types public, protected, private Only public https://codeandwork.github.io/courses/java/interfaces.html
Interface concept vs. Interfaces
• Interface concept: Set of methods and fields that the class makes public for other objects to
access
• Interface construct: Recipe/contract for what
methods an implementing class should implement
Questions?
What can you do?
• Work on the assignments linked from our wiki (lab 1, 2, and 3 are up!)
• Watch a short video on abstract classes:
https://www.youtube.com/watch?v=hZ1EU-F-0nU
• Watch a short video on interfaces:
https://www.youtube.com/watch?v=fX1xNMBTMfg
• Read more about interfaces in Head First Design Patterns chapter 1: https://www.oreilly.com/library/view/head-first- design/0596007124/ch01.html
What’s next?
• Next lecture tomorrow, same time, same place
• Topic: testing