Top Banner
1 Programming in Java: lecture 7 Inheritance Polymorphism Abstract Classes this and super Interfaces Nested Classes and other detail Example Slides made for use with ”Introuction to Programming Using Java, Version 5.0” by David J. Eck Some figures are taken from ”Introuction to Programming Using Java, Version 5.0” by David J. Eck Lecture 3 covers Section 5.5 to 5.7
25

Programming in Java: lecture 7 - people.cs.aau.dk

Feb 18, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming in Java: lecture 7 - people.cs.aau.dk

1

Programming in Java: lecture 7

Inheritance Polymorphism Abstract Classes this and super Interfaces Nested Classes and other detail Example

Slides made for use with ”Introuction to Programming Using Java, Version 5.0” by David J. EckSome figures are taken from ”Introuction to Programming Using Java, Version 5.0” by David J. EckLecture 3 covers Section 5.5 to 5.7

Page 2: Programming in Java: lecture 7 - people.cs.aau.dk

2

Last time

Objects, Classes and Instanes Getters and setters Constructors and object initialization Wrapper Classes and Autoboxing Garbage collection and the heap Object oriented analysis and design Example

Page 3: Programming in Java: lecture 7 - people.cs.aau.dk

3

Classes and Objects

A Class is a template Objects are objects Objects are instances of a given class

Integer Classstatic members

parseInt(String s)

Integer Objectnon-static member

equals(int i)

Page 4: Programming in Java: lecture 7 - people.cs.aau.dk

4

Inheritance

Objects are instances of a given class

Class Asuperclass

Object of type A

Class B extends Asubclass

Object of type B

Page 5: Programming in Java: lecture 7 - people.cs.aau.dk

Inheritance

Vehicle

HasWheels Flying Vehicle

Plane HelicopterTruck Car

Page 6: Programming in Java: lecture 7 - people.cs.aau.dk

6

Inheritance

Syntax

Extending existing classes new methods override methods new instance variables

Page 7: Programming in Java: lecture 7 - people.cs.aau.dk

7

Class hierarchy

Everything extends Object

Page 8: Programming in Java: lecture 7 - people.cs.aau.dk

8

Polymorphism

Two concepts We can write code that can handle all future

subclasses We can have variables without knowing the

exact type of the object that it refers to

Page 9: Programming in Java: lecture 7 - people.cs.aau.dk

9

Abstract class

Cannot make objects from abstract classes Can make variables from abstract classes

Abstract Class A Object of type A

Class B extends A Object of type B

Page 10: Programming in Java: lecture 7 - people.cs.aau.dk

10

Abstract example

Page 11: Programming in Java: lecture 7 - people.cs.aau.dk

11

this and super

special variables cannot be assigned to this – the object we are currently in super – used to call methods of the super class

forgets the exact type of the object

special use in constructors Used as a method name Calls other constructors

Page 12: Programming in Java: lecture 7 - people.cs.aau.dk

12

this – example

Page 13: Programming in Java: lecture 7 - people.cs.aau.dk

13

super – example

Page 14: Programming in Java: lecture 7 - people.cs.aau.dk

14

Constructor example

Page 15: Programming in Java: lecture 7 - people.cs.aau.dk

15

Multiple inheritance

Not allowed in Java

Page 16: Programming in Java: lecture 7 - people.cs.aau.dk

16

Interfaces

Describes an aspect Completely abstract class

nothing can be implemented

Page 17: Programming in Java: lecture 7 - people.cs.aau.dk

17

Interfaces

Implementing multiple interfaces (serializable)

Use of objects

Page 18: Programming in Java: lecture 7 - people.cs.aau.dk

18

Nested classes

Classes inside classes Static

Only one new type Non-static

One new type per object

Class

static nested class

Object o1

same type

Object o2

same type

Page 19: Programming in Java: lecture 7 - people.cs.aau.dk

19

Nested classes

Classes inside classes Static

Only one new type Non-static

One new type per object

Class

nested class

Object o1

different types

Object o2

different types

Page 20: Programming in Java: lecture 7 - people.cs.aau.dk

20

Example – static

Page 21: Programming in Java: lecture 7 - people.cs.aau.dk

21

Example – non static

Page 22: Programming in Java: lecture 7 - people.cs.aau.dk

22

Anonymous Inner Classes

If you only need it in one place

Page 23: Programming in Java: lecture 7 - people.cs.aau.dk

23

Static import

Page 24: Programming in Java: lecture 7 - people.cs.aau.dk

24

Enums

Enums are classes each enumerated type is a public static final

member

Page 25: Programming in Java: lecture 7 - people.cs.aau.dk

25

Example

Team programming