1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 11 - Maria Litvin, Gary Litvin

31 22 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 31
Dung lượng 380,12 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Chapter 11 - Class hierarchies and interfaces. In this chapter, the learning objectives are: Understand class hierarchies and polymorphism, learn about abstract classes, learn the syntax for calling superclass’s constructors and methods, understand interfaces.

Trang 1

Class Hierarchies and Interfaces

Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All

rights reserved.

Java Methods

Object-Oriented Programming

and Data Structures

Maria Litvin ● Gary Litvin

2nd AP edition with GridWorld

C  h  a  p 

t  r 

Trang 2

Objectives:

polymorphism

constructors and methods

Trang 3

Inheritance

between objects: an object of a subclass IS-A(n) object of the superclass

Superclass (Base class)

Subclass (Derived class)

Subclass extends

Superclass

Trang 5

Class Hierarchies (cont’d)

of code by factoring out

common code from

similar classes into a

common superclass

Actor Constructor Accessors

putSelfInGrid removeSelfFromGrid setColor

moveTo act

Bug Constructor

canMove move turn act (redefined)

Flower Constructor

act (redefined)

Trang 6

Class Hierarchies (cont’d)

write more general methods in client classes

public void add

(Location loc, Bug occupant)

{

occupant.putSelfInGrid(getGrid(), loc);

}

public void add

(Location loc, Flower occupant)

{

occupant.putSelfInGrid(getGrid(), loc);

}

public void add

(Location loc, Actor occupant)

{ occupant.putSelfInGrid(getGrid(), loc); }

Works for a Bug or a Flower any Actor

Trang 7

Abstract Classes

defined

public abstract class Actor

Trang 8

Abstract Classes (cont’d)

superclasses for more specific classes

for the compiler to do additional error

checking

hierarchy; they describe more abstract

objects

Trang 9

JTextComponent AbstractButton JPanel

JTextArea JTextField JMenuItem JButton

A fragment of Java library GUI class hierarchy (abstract classes are boxed)

Trang 10

Abstract Classes (cont’d)

create objects of) abstract classes

they can be called from constructors of

subclasses

concrete

Trang 11

• Object is a concrete class

public class Object

{

public String toString { }

public boolean equals (Object other) { }

public int hashCode() { }

// a few other methods

}

Methods redefined (overridden)

as necessary

Trang 12

Calls Bug’s

constructor

If present, must be

the first statement

The number / types of parameters passed to

super must match parameters of one of the

superclass’s constructors.

Trang 13

Invoking Superclass’s

Constructors (cont’d)

always called, but you don’t have to have an

superclass’s no-args constructor is called by default

Must be defined then If not defined syntax error:

Trang 14

constructor, and so on, all the way up to

Object’s constructor

Actor Flower

Trang 15

Calls Flower’s act

super.someMethod refers to someMethod in

the nearest class, up the inheritance line, where

someMethod is defined.

Trang 16

Calling Superclass’s

Methods (cont’d)

• super-dot calls are often used in

subclasses of library classes:

public class Canvas extends JPanel

Trang 17

Polymorphism

an object of a specific type, even when that object is disguised as a reference to a more generic type, that is, the type of the object’s superclass or some ancestor higher up the inheritance line

polymorphism is just there no need to do anything special

Trang 18

The correct act method is

called automatically for any specific type of actor.

Trang 19

Interface

Trang 20

Interfaces (cont’d)

but it does not have any fields or constructors, and all its methods are abstract

public interface Dance

{ String getName ();

String getSteps (int m);

int[] getBeat ();

}

Trang 21

Interfaces (cont’d)

implements an interface

must supply all the methods of that interface

public class Waltz implements Dance

{

.

public String getName( ) { return "Waltz"; }

public String getSteps(int m) { }

public int[] getBeat( ) { }

.

}

Trang 22

Interfaces (cont’d)

secondary data type to objects of a class that implements that interface

an interface type

disguised as interface types

Dance d = new Waltz( );

Trang 23

method is called for any specific

type of Edible (e.g., a Pancake)

public class Pancake implements Edible

{

}

Trang 24

instantiated.

Similarities

Trang 25

Classes Interfaces

an abstract class must

define all the inherited

abstract methods.

another class A

subclass can add

methods and override

some of its superclass’s

methods.

implements an interface must define all the

methods specified by the interface

another interface (called

its superinterface) by

adding declarations of abstract methods.

Similarities

Trang 26

Classes Interfaces

one class

constructors (or gets a

default constructor)

any number of interfaces

have fields (except, possibly, some public static final constants).

constructors

Differences

Trang 27

Classes Interfaces

its methods defined An

abstract class usually

has one or more

abstract methods.

a hierarchy of classes

an interface are abstract

to a small hierarchy of interfaces, but this is not very common

Differences

Trang 28

RightShoe

LeftSandal RightSandal

DancingBug

DancingCrab

Maracas

EasySound

GridWorld’s framework

Trang 29

Review

code using class hierarchies

rather than an empty method?

Is superclass’s constructor called?

Trang 30

Review (cont’d)

paintComponent(g);

instead of

super.paintComponent(g);

?

Trang 31

Review (cont’d)

abstract class and an interface?

interface type What type of value can be

assigned to that variable?

abstract classes?

Ngày đăng: 04/11/2020, 23:16

TỪ KHÓA LIÊN QUAN