Java Object-oriented Programming 2 Learning Objectives Java - - PowerPoint PPT Presentation

java object oriented programming 2 learning objectives
SMART_READER_LITE
LIVE PREVIEW

Java Object-oriented Programming 2 Learning Objectives Java - - PowerPoint PPT Presentation

Java Object-oriented Programming 2 Learning Objectives Java Class design Inheritance Abstract classes Object class Interfaces Dynamic binding CS 6452: Prototyping Interactive Systems 2 <Review> CS 6452:


slide-1
SLIDE 1

Java Object-oriented Programming 2

slide-2
SLIDE 2

CS 6452: Prototyping Interactive Systems

Learning Objectives

  • Java

− Class design − Inheritance − Abstract classes − Object class − Interfaces − Dynamic binding

2

slide-3
SLIDE 3

CS 6452: Prototyping Interactive Systems

<Review>

3

slide-4
SLIDE 4

CS 6452: Prototyping Interactive Systems 4

class Car

int year; double mpg; Color col; …

instance data client interface externally used methods internally used methods

slide-5
SLIDE 5

CS 6452: Prototyping Interactive Systems

Access

5

public variables methods private

X

natural service
 to clients internal
 class support

Class has access to all private members

slide-6
SLIDE 6

CS 6452: Prototyping Interactive Systems 6 public class Car { private int vin,year; private double speed, mpg; public void drive() { … } public int getYear() { return year; } public void setYear(int y) { year = y; } public Car() { … } private void diagnose() { … } public static void main (String[] args) { … } }

Internal method “Accessor” method “Modifier” method Constructor

slide-7
SLIDE 7

CS 6452: Prototyping Interactive Systems

Methods

  • Nothing in front of speed
  • Which speed?
  • The instance variable within the object

upon which this method was called

7

public double drive(int time) { double distance; distance = time * speed; return distance; }

slide-8
SLIDE 8

CS 6452: Prototyping Interactive Systems

</Review>

8

slide-9
SLIDE 9

CS 6452: Prototyping Interactive Systems

Special Method

  • When Java tries to print an object, it

automatically calls toString() method

  • What is printed is up to you

9

Car c1 = new Car(2004, 3247613237, "Audi", "5000"); System.out.println(c1); // in Car class public String toString() { return vin + " " + make + " " + model; }

slide-10
SLIDE 10

CS 6452: Prototyping Interactive Systems 10

Sample program
 layout

slide-11
SLIDE 11

CS 6452: Prototyping Interactive Systems 11

Suppose ZooDriver's main is main(--) { Person p = new Person(); Tiger t = new Tiger(); t.roar(); and Tiger's roar() is roar() { go(); In Person's talk() method, can you

  • access name
  • access count
  • call foo()
  • call add()

In Person's static add() method, can you

  • access name
  • access count
  • call foo()
  • call add()

Why no object in front of go()?

slide-12
SLIDE 12

CS 6452: Prototyping Interactive Systems

Inheritance

  • Process of deriving a new class from an

existing one

  • Automatically contains some or all of the

methods of original

  • Can add new methods too
  • Child can have only one parent class

12

slide-13
SLIDE 13

CS 6452: Prototyping Interactive Systems

Inheritance

13

parent/super/base class child/subclass keyword extends is-a hierarchy

Vehicle Car

slide-14
SLIDE 14

CS 6452: Prototyping Interactive Systems

Inheritance

14

public class Vehicle { … } public class Car extends Vehicle { … // Car is-a vehicle }

Vehicle can't access data or methods of Car

Car c2 = new Car(); // don't have to instantiate Vehicle // you get Vehicle data & methods too

slide-15
SLIDE 15

CS 6452: Prototyping Interactive Systems

Inheritance

15

Child class can access public data/methods of parent Child class cannot access public data/methods of parent So???

protected – Can be accessed by child class but not outside classes

Child class can access parent's instance data and call
 parent's methods without qualification. It's like they are yours. They actually are!

slide-16
SLIDE 16

CS 6452: Prototyping Interactive Systems

Example

16

public class Person { protected int ssn; protected String name; … } public class GTStudent extends Person { protected int gt_id; protected int credithrs; … public void m1(int num) { if (num == ssn) … }

slide-17
SLIDE 17

CS 6452: Prototyping Interactive Systems

Inheritance

  • Overriding – When child class defines a

method with same name as one in the parent, child's version overrides the parent's

17

public class Person { protected int ssn; protected String name; public void talk() { … } } public class GTStudent extends Person { protected int gt_id; protected int credithrs; … public void talk() { … } }

slide-18
SLIDE 18

CS 6452: Prototyping Interactive Systems

Inheritance

  • Quiz

− Can you do − Yes! (remember is-a) − Can you do − No! (not necessarily true)

18

Vehicle v1 = new Car(); Person p1 = new GTStudent(); Car c1 = new Vehicle(); GTStudent g1 = new Person();

slide-19
SLIDE 19

CS 6452: Prototyping Interactive Systems

Inheritance

  • Quiz

19

Person p1 = new GTStudent(); p1.talk();

Which talk() method is done, Person's or GTStudent's? GTStudent's That's really what's inside the p1 reference Dynamic binding – Which method to perform is 
 determined dynamically at run-time

slide-20
SLIDE 20

CS 6452: Prototyping Interactive Systems

Abstract Class

  • Vehicle class we showed before might be

abstract

− Not real, ie, you never really make one

20

public abstract class Vehicle { protected int year; protected Color col; public abstract void drive(); // Abstract method }

Abstract method – Subclasses must provide (override) it or be abstract themselves

slide-21
SLIDE 21

CS 6452: Prototyping Interactive Systems

Abstract Class

  • Can abstract class have non-abstract

methods?

− Yes!

21

public abstract class Vehicle { protected int year; protected Color col; public abstract void drive(); // Abstract method public int getYear() { return year; } }

slide-22
SLIDE 22

CS 6452: Prototyping Interactive Systems

Abstract class

  • Abstract classes cannot be instantiated

with new()

22

Vehicle v1 = new Vehicle(); // Compile error

slide-23
SLIDE 23

CS 6452: Prototyping Interactive Systems

Object class

  • Object

− In java, all classes ultimately derived from it − What's in it? Look in API − Not an abstract class, a real one

23

slide-24
SLIDE 24

CS 6452: Prototyping Interactive Systems

Interface

  • Different type of construct in Java

− Not a class

  • Set of abstract methods and constants

24

public interface Bank { public void deposit(double dep); public double withdrawl(double wd); public void audit(); }

All goes in Bank.java

slide-25
SLIDE 25

CS 6452: Prototyping Interactive Systems

Interface

  • A class implements an interface if it

provides all the methods of the interface

− Can have other methods too

  • Interface is a contract or specification

− Lists set of services − If class provides those services, then it implements interface − Class can implement more than one interface

25

public class MyMutual implements Bank { … }

slide-26
SLIDE 26

CS 6452: Prototyping Interactive Systems

Interface

  • What for?

− If a class you want to use implements an interface, you know it has to provide those methods − Kind of a "guarantee"

26

slide-27
SLIDE 27

CS 6452: Prototyping Interactive Systems

Scenario

27

Fish Bird Dog Animal

Pet Store program Animal is an abstract class Others are real

slide-28
SLIDE 28

CS 6452: Prototyping Interactive Systems

Scenario

28

public abstract class Animal { public void makeNoise() { System.out.println("I'm an animal"); } } public class Fish extends Animal { public void makeNoise() { System.out.println("Glug glug"); } } public class Bird extends Animal { public void makeNoise() { System.out.println("Tweet tweet"); } } public class Dog extends Animal { public void makeNoise() { System.out.println("Woof woof"); } public void bark() { System.out.println("Arf arf"); } }

Four classes

slide-29
SLIDE 29

CS 6452: Prototyping Interactive Systems

Scenario

  • Want to create a data structure to hold a

bunch of these different animals

− How to do it? What to use? − An array – But how?

− Animal[] a = new Animal[100];

29

slide-30
SLIDE 30

CS 6452: Prototyping Interactive Systems

Scenario

  • Can you do
  • Yes! A Fish is an Animal
  • Same for

30

a[0] = new Fish(); a[1] = new Bird(); a[2] = new Dog();

slide-31
SLIDE 31

CS 6452: Prototyping Interactive Systems 31 public class Driver { public static void main(String[] args) { Animal[] a = new Animal[100]; a[0] = new Bird(); a[1] = new Fish(); a[2] = new Dog(); // … // We want to walk through them all // and have them make their noise }

slide-32
SLIDE 32

CS 6452: Prototyping Interactive Systems 32 public class Driver { public static void main(String[] args) { Animal[] a = new Animal[100]; a[0] = new Bird(); a[1] = new Fish(); a[2] = new Dog(); // … // We want to walk through them all // and have them make their noise for (int i=0; i<a.length; i++) { if (a[i] is a Bird) //then else if (a[i] is a Fish) //then } // Will that work? // No! }

slide-33
SLIDE 33

CS 6452: Prototyping Interactive Systems 33 public class Driver { public static void main(String[] args) { Animal[] a = new Animal[100]; a[0] = new Bird(); a[1] = new Fish(); a[2] = new Dog(); // … // We want to walk through them all // and have them make their noise for (int i=0; i<a.length; i++) { a[i].makeNoise(); } // Will that work? // Yes!!! }

slide-34
SLIDE 34

CS 6452: Prototyping Interactive Systems 34 public class Driver { public static void main(String[] args) { Animal[] a = new Animal[100]; a[0] = new Bird(); a[1] = new Fish(); a[2] = new Dog(); // … // We want to walk through them all // and have them make their noise for (int i=0; i<a.length; i++) { a[i].makeNoise(); } // What if we wanted the Dog ones to bark() too? // Can we do a[i].bark(); // No!!!! Not all a[i] are Dogs }

slide-35
SLIDE 35

CS 6452: Prototyping Interactive Systems 35 public class Driver { public static void main(String[] args) { Animal[] a = new Animal[100]; a[0] = new Bird(); a[1] = new Fish(); a[2] = new Dog(); // … // We want to walk through them all // and have them make their noise for (int i=0; i<a.length; i++) { a[i].makeNoise(); } // What if we wanted the Dog ones to bark() too? Dog d; if (a[i] instanceof Dog) { d = (Dog) a[i]; d.bark(); } }

slide-36
SLIDE 36

CS 6452: Prototyping Interactive Systems

Learning Objectives

  • Java

− Class design − Inheritance − Abstract classes − Object class − Interfaces − Dynamic binding

36