COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

39
COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014

Transcript of COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Page 1: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110:Introduction to Programming

Tyler JohnsonApr 8, 2009

MWF 11:00AM-12:15PMSitterson 014

Page 2: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20092

Announcements

Lab 8 is due tomorrow by midnight

Page 3: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20093

Questions?

Page 4: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20094

Today in COMP 110

Go over Program 4

More Inheritance

Programming Demo

Page 5: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20095

Program 4

Page 6: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20096

Inheritance

Section 8.3 in text

Page 7: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20097

Inheritance

Define a general class

Later, define specialized classes based on the general class

These specialized classes inherit properties from the general class

Page 8: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20098

Inheritance Hierarchies

Put these classes into an inheritance hierarchy

AnimalReptile MammalHumanCrocodile Whale

Animal

Reptile Mammal

HumanCrocodile Whale

Page 9: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20099

The is-a relationship

This inheritance relationship is known as an is-a relationship

A Doctoral student is a Grad studentA Grad student is a StudentA Student is a Person

Is a Person a Student?Not necessarily!

Page 10: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200910

When to Use Inheritance

Use inheritance when an is-a relationship is present

A Student is a Person, Student inherits from Person

Do not confuse an is-a relationship with a has-a relationship

A Student has a date of enrollment, so an object of class Date should be an instance variable of the Student class, and should not inherit from it

Page 11: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200911

The Keyword extends

public class Derived_Class_Name extends Base_Class_Name { Declaration_of_Added_Instance_Variables Definitions_of_Added_And_Overridden_Methods}

public class Student extends Person { // stuff goes here}

A derived (child) class inherits the public instance variables and public methods of its base (parent) class

Page 12: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200912

Method Overriding

public class Person {

// ...

public void printInfo() { System.out.println("Name: " +

name); }}

public class Student extends Person {

// ...

public void printInfo() { System.out.println("Name: " +

getName()); System.out.println("ID: " + getID()); }}

Overridden

The printInfo method in the class Student overrides the printInfo method inherited from the class Person

Page 13: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200913

Overriding Methods

Java handles this situation as follows:

If a derived class defines a method with the same name, number and types of parameters, and return type as a method in the base class, the derived class’ method overrides the base class’ methodThe method definition in the derived class is the one that is used for objects of the derived class

Page 14: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200914

Overriding Methods

Why is it useful?You often want derived classes to perform custom behavior

public class Shape {

public void draw(Graphics g) {

}}

public class Circle extends Shape {

public void draw(Graphics g) { g.drawOval(…arguments…);

}}

public class Rectangle extends Shape {

public void draw(Graphics g) {g.drawRect(…arguments…);

}}

Page 15: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200915

The Keyword super

Last time we saw that the keyword super could be used to invoke the constructor of the base class

public class Person {

private String name;

public Person() {name = "No

name yet";}

}

public class Student extends Person {

private int id;

public Student() {super();id = -1;

}}

Student student = new Student(); //name is set to No name yet, and id is set to -1

Page 16: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200916

The Keyword super

The use of super must always be the first action taken in the constructor

Not required to be specified, but if it is, it must come firstJava will automatically call the default constructor of the base class if super is not specified

public Student() { super(); id = -1;}

public Student() { id = -1;}

public Student() { id = -1; super();}

OK OK ERROR

Equivalent

Page 17: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200917

The Keyword super

You cannot use repeated supersIf specified, specify only once

public Student() { super(); id = -1;}

public Student() { super(); super("No name yet"); id = -1;}

OK ERROR

Page 18: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200918

The Keyword super

What if the base class is a derived class?

public class Person {

private String name;

public Person() {name = "No

name yet";}

}

public class Student extends Person {

private int id;

public Student() {super();id = -1;

}}public class Undergraduate extends Student {

private int level;

public Undergraduate() {super(); //calls constructor of Student, not Personlevel = -1;

}}

Page 19: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200919

Method Overriding

public class Person {

// ...

public void printInfo() { System.out.println("Name: " +

name); }}

public class Student extends Person {

// ...

public void printInfo() { System.out.println("Name: " +

getName()); System.out.println("ID: " + getID()); }}

Overridden

Is there a way to call the printInfo() method of the class Person from within the class Student?

Yes

Page 20: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200920

Calling Overridden Methods

public class Person {

// ...

public void printInfo() { System.out.println("Name: " +

name); }}

public class Student extends Person {

// ...

public void printInfo() { super.printInfo(); System.out.println("ID: " + getID()); }}

The keyword super can also be used to call overridden methods of a base class

Page 21: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200921

Inheritance and Overloading

public class Person {

private String name;

public void setData(String newName) {

name = newName; }}

public class Student extends Person {

private int id;

public void setData(String newName, int newID) {

setData(newName); id = newID;}

Overloaded

Methods with a different number or type of parameters are overloaded, not overridden

Page 22: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200922

Type Compatibilities

Given this inheritance heirarchy…

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 23: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200923

Is This Code Legal?

Person p = new Person();Yes!

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 24: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200924

Is This Code Legal?

HighJumper h = new HighJumper();Yes!

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 25: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200925

Is This Code Legal?

Person p = new Athlete();Yes! An Athlete is a Person, so this is okay

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 26: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200926

Is This Code Legal?

Person p = new SkyDiver();Yes! A SkyDiver is a Person, so this is okay

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 27: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200927

Is This Code Legal?

Skydiver s = new Person();No! A Person is not necessarily a Skydiver, so this is illegal

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 28: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200928

Is This Code Legal?

Athlete ath = new Athlete();XGamesSkater xgs = ath;

No! An Athlete is not necessarily an XGamesSkater, so this is illegal

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 29: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200929

Dynamic Binding

Person person = new Person("John Smith");person.printlnfo(); //calls printInfo of Person class

Student student = new Student("John Smith", 42352352);student.printlnfo(); //calls printInfo of Student class

Person p = new Student(“Tom Jones", 733234242);p.printInfo(); //which printInfo method is called?

The printInfo method of the Student class is called because p is really a student!

Page 30: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200930

The Operator instanceof

We can test whether an object is of a certain class type using the instanceof operator:public void objectType(Person p) {

if(p instanceof Student) { System.out.println("p is an instance of the class Student");}else if(p instanceof GradStudent) { System.out.println("p is an instance of the class GradStudent");}…

}

Syntax:object instanceof Class_Name

Page 31: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200931

The Class Object

Every class in Java is derived from the class Object

Every class in Java is an Object

Animal

Reptile Mammal

HumanCrocodile Whale

Object

Person

Student Employee

Page 32: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200932

The Class Object

The Java class Object provides methods that are inherited by every class

For exampleequals, toString

These methods should be overridden with methods appropriate for the classes you create

Page 33: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200933

Printing Objects to Screen

The method println is overloaded to accept objects as an argument

System.out.println(int i)System.out.println(double d)System.out.println(Object o)

When printing an object with System.out.println(Object o), the object’s toString() method is called and the result is printed

public void println(Object o) {System.out.println(o.toString()); //dynamic binding

}

Page 34: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200934

Overriding toString

public class Person {

private String name;

public Person() {name = "No name yet";

}

public void setName(String newName) {name = newName;

}

//converts a Person object to a String, overrides toString in the class Objectpublic String toString() {

return "Name: " + name + "\n";}

public static void main(String[] args) {Person person = new Person();person.setName("John Smith");System.out.println(person); //prints "Name: John Smith" to screen (without quotes)

}}

Page 35: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200935

Overriding toString

public class Student extends Person {

private int id;

public Student() {super();id = -1;

}

public void setID(String newName) {name = newName;

}

//converts a Person object to a String, overrides toString in the class Personpublic String toString() {

return "Name: " + name + "\nID: " + id + "\n";}

public static void main(String[] args) {Student student = new Student();student.setName("John Smith");student.setID(342652342);System.out.println(student); //prints "Name: John Smith"

// "ID: 342652342" to screen (without quotes)}

}

Page 36: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200936

Programming Demo

Programming with inheritanceSports

Define the following classesPerson

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 37: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200937

Sports

Each class has a method called jumpSimply prints a message to screen

PersonSystem.out.println("Whee!");

AthleteSystem.out.println("I jump well!");

ExtremeAthleteSystem.out.println("EXTREMEEEEEEEEEE JUMP!");

HighJumperSystem.out.println("I jump REALLY HIGH!");

ShotPutterSystem.out.println("I don't jump very much.");

XGamesSkaterSystem.out.println("360 nollie to frontside air");

Page 38: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200938

Sports

Write a class called Sports that demonstrates

Type compatibilityDynamic bindinginstanceof operator

Page 39: COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200939

Friday

No recitation, enjoy the Holidays