• No results found

INF101: Object-Oriented Programming. Lecture 5: Abstract Classes and Interfaces

N/A
N/A
Protected

Academic year: 2022

Share "INF101: Object-Oriented Programming. Lecture 5: Abstract Classes and Interfaces"

Copied!
78
0
0

Loading.... (view fulltext now)

Full text

(1)

INF101: Object-Oriented Programming

Lecture 5: Abstract Classes and Interfaces

(2)

Welcome to INF101 – Lecture 4!

• Previously:

– Designing our first duck pond simulator.

– Enums

– Passing by Value

(3)

Enums

(4)

Enums

(5)

Passing by Value

(6)

Passing by Value

(7)

Passing by Value

(8)

Objects though…

(9)

Objects though…

(10)

The reveal

(11)

Today

• Continuing our first duck pond simulator

• Abstraction

• Abstract classes

• Interfaces

(12)

Questions?

(13)

Abstraction

(14)

Four main concepts in OOP

• Encapsulation

• Abstraction

• Inheritance

• Polymorphism

(15)

Four main concepts in OOP

• Encapsulation

• Abstraction

• Inheritance

• Polymorphism

(16)

Encapsulation

• Bundling properties and methods of objects in classes

• Wrapping the implementation (code) and the data it manipulates (variables) in the same class

(17)

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

(18)

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

(19)

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

(20)

Four main concepts in OOP

• Encapsulation

• Abstraction

• Inheritance

• Polymorphism

(21)

Different Types of Abstraction

• Abstraction by parametrization

• Abstraction by specification

• Control abstraction

• Data abstraction

(22)

Abstraction by parametrization

• Abstract away from concrete values in functions by adding parameters

(23)

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

(24)

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

(25)

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

(26)

Questions

(27)

Abstract Classes

(28)

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

(29)

Abstract methods

• Use the abstract keyword

• ; instead of a method body

(30)

Abstract classes

• If your class features at least one abstract method, it is also abstract

(31)

Abstract classes

• If your class features at least one abstract method, it is also abstract

(32)

Abstract classes cannot be

instantiated

(33)

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 

(34)

Abstract Classes

• Can also have non-abstract methods:

(35)

Abstract Classes

• Can also only have non-abstract methods

(36)

So what can you do with them?

• Combine with inheritance

(37)

So what can you do with them?

• Combine with inheritance, using ‘extends’

keyword

(38)

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

(39)

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

(40)

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

(41)

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.

(42)

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.

(43)

Questions?

(44)

Break

(45)

About the labs

(46)

Please do the labs ☺

• Gruppeledere are ready to help you

• Part of optimal preparation for compulsory assignments and exam

(47)

Exercise

(48)

Exercise

(49)

Interfaces

(50)

Return to the Pond

• We want more than just ducks. How about frogs?

(51)

We already had a Duck

(52)

And a Pond

(53)

Now add a Frog

(54)

Frog class

• (copy of duck, but green!)

(55)

Add to the Pond

(56)

Are we going to duplicate code for every new pond inhabitant?

• There’s got to be another way!

(57)

Abstract Classes

(58)

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’

(59)

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

(60)

Back to the pond

<<interface>>

IPondObject

Duck Frog

step() draw(painter)

(61)
(62)
(63)
(64)

Pond update

(65)

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

(66)

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

(67)

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

(68)

Combining interfaces with inheritance

http://www.cs.sjsu.edu/~pearce/modules/lectures/oop/basics/interfaces.htm

(69)

Interface inheritance

http://www.cs.sjsu.edu/~pearce/modules/lectures/oop/basics/interfaces.htm

(70)

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)

(71)

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 ☺

(72)

Default methods

(73)

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

(74)

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

(75)

Questions?

(76)

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

(77)

What’s next?

• Next lecture tomorrow, same time, same place

• Topic: testing

(78)

Thanks!

References

Related documents

• Java 8 introduced static methods into interfaces and also default methods, which are essentially instance methods and are used whenever a method of a class implementing the

¾An abstract class can be used to declare pointers that can access objects derived from the abstract base class. Object Oriented Programming

Outcome competency DC71: Apply advanced Object-Oriented Programming features of Java language such as encapsulation, inheritance, the concept of “static”, interfaces, abstract

Indeed, age-related metal ion dysfunc- tion altered levels of neuronal metal ions in AD-affected areas including accumulation in protein deposits, and the interplay between metal

The OpenStage 30T/40/40G/40T always requires a local power supply (SMPS mains adapter, must be ordered separately) if an OpenStage Busy Lamp Field 40 is connected.. The PNOTE

• No Visible effect for launch of Migraine Drug • Learning will be useful for future products • Cost

Dolazi mi Nikolić i saopštava jedno Ulmanovo pismo, u kome piše da jedan novi konzorcijum hoće da podnese molbu za srpsku železnicu, dalje da Andraši više ne prima

Goals: To teach the student the concepts of static and abstract objects, and of interfaces, important tools in Java for Object-Oriented programming. Outcome: The student will be able