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

COMP 110: Introduction to Programming

Embed Size (px)

DESCRIPTION

COMP 110: Introduction to Programming. Tyler Johnson Apr 6, 2009 MWF 11:00AM-12:15PM Sitterson 014. Announcements. Lab 7 has been graded Program 5 assigned Milestone 1 due Apr 15th by 5pm Final Submission due Apr 27th by 5pm. Questions?. Today in COMP 110. Go over Lab 7 Inheritance - PowerPoint PPT Presentation

Citation preview

Page 1: COMP 110: Introduction to Programming

COMP 110:Introduction to Programming

Tyler JohnsonApr 6, 2009

MWF 11:00AM-12:15PMSitterson 014

Page 2: COMP 110: Introduction to Programming

COMP 110: Spring 20092

Announcements

Lab 7 has been graded

Program 5 assignedMilestone 1 due Apr 15th by 5pm Final Submission due Apr 27th by 5pm

Page 3: COMP 110: Introduction to Programming

COMP 110: Spring 20093

Questions?

Page 4: COMP 110: Introduction to Programming

COMP 110: Spring 20094

Today in COMP 110

Go over Lab 7

Inheritance

In-Class Exercise

Page 5: COMP 110: Introduction to Programming

COMP 110: Spring 20095

Lab 7

Page 6: COMP 110: Introduction to Programming

COMP 110: Spring 20096

Inheritance

Section 8.2 in text

Page 7: COMP 110: Introduction to Programming

COMP 110: Spring 20097

Inheritance

What are some properties of a Person?

Name, height, weight, age

How about a Student?ID, major

Does a Student have a name, height, weight, and age?

Student inherits these properties from Person

Page 8: COMP 110: Introduction to Programming

COMP 110: Spring 20098

Inheritance

Person

- name: String

+ getName(): String+ setName(String newName): void

Student

- name: String- id: int

+ getName(): String+ setName(String newName): void+ getID(): int+ setID(int id): void

A Student is a Person!

This is called an is-a relationship

Page 9: COMP 110: Introduction to Programming

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

COMP 110: Spring 200910

Inheritance

Define a general classSuch as Person

Later, define specialized classes based on the general class

Such as Student

These specialized classes inherit properties from the general class

Page 11: COMP 110: Introduction to Programming

COMP 110: Spring 200911

Inheritance

Examples of class inheritance

Person

Student Employee

Undergrad Grad

Masters Doctoral Nondegree

Faculty Staff

Transportation

Car Airplane Animal

ElephantHorse Camel

Page 12: COMP 110: Introduction to Programming

COMP 110: Spring 200912

Base Class

Our general class is called a base class

Also called a parent class or a superclass

Examples:Person, Transportation

Page 13: COMP 110: Introduction to Programming

COMP 110: Spring 200913

Derived Class

A specialized class that inherits properties from a base class is called a derived class

Also called a child class or a subclass

Examples:Student is-a PersonEmployee is-a PersonCar is-a formof TransportationAnimal is-a formof Transportation

Person

Student Employee

Undergrad Grad

Masters Doctoral Nondegree

Faculty Staff

Page 14: COMP 110: Introduction to Programming

COMP 110: Spring 200914

Derived Classes

Derived classes can also be base classes

Student is a child class of PersonStudent is also the parent class of Undergrad and Grad

Person

Student Employee

Undergrad Grad

Masters Doctoral Nondegree

Faculty Staff

Page 15: COMP 110: Introduction to Programming

COMP 110: Spring 200915

Why is Inheritance Useful?

Enables you to define shared properties and actions once

If two classes share the same properties, why define them twice? Use inheritance.

Derived classes can perform the same actions as base classes without having to redefine the actions

If desired, the actions can be redefined – more on this later

Page 16: COMP 110: Introduction to Programming

COMP 110: Spring 200916

Inheritance in Java

public class Person {

private String name;

public Person() { name = “No name yet”; }

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

public String getName() { return name; }}

Person

- name: String

+ getName(): String+ setName(String newName): void

Page 17: COMP 110: Introduction to Programming

COMP 110: Spring 200917

Inheritance in Java

public class Student extends Person {

private int id;

public Student() { super(); id = 0; }

public Student(String stdName, int idNumber) { setName(stdName); setID(idNumber); }

public void setID(int idNumber) { id = idNumber; }

public int getID() { return id; }}

Person

- name: String

+ getName(): String+ setName(String newName): void

Student

- id: int

+ getID(): int+ setID(int newID): void

Derived classes only define the functionality that is added to the base class!

Page 18: COMP 110: Introduction to Programming

COMP 110: Spring 200918

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 19: COMP 110: Introduction to Programming

COMP 110: Spring 200919

Private vs. Public

private instance variables and private methods in the base class can NOT be accessed by derived classes

This would not work:

public Student(String stdName, int idNumber) { name = stdName; // ERROR! name is private to Person setID(idNumber);}

Page 20: COMP 110: Introduction to Programming

COMP 110: Spring 200920

Private vs. Public

private instance variables of the base class CAN be accessed by derived classes using the base class’ public methods

This works:public Student(String stdName, int idNumber) {

setName(stdName); // OK! setName is a public method in Person

setID(idNumber);}

Page 21: COMP 110: Introduction to Programming

COMP 110: Spring 200921

Example

public class A {

private int var1;public int var2;

public void setVar1(int newVar1) {var1 = newVar1;

}

public void setVar2(int newVar2) {var2 = newVar2;

}

private int foo() {System.out.println("foo");

}}

public class B extends A {

private int var3;

public B(int newVar1) {var1 = newVar1;setVar1(newVar);

}

public void setVar3(int newVar3) {var3 = newVar3;

}}

public static void main(String[] args) {B b = new B();b.setVar3(7);b.setVar2(4);b.setVar1(5);b.foo();

}

//LEGAL, method in class B//LEGAL, public method inherited from class A//LEGAL, public method inherited from class A//ILLEGAL, private method NOT inherited from class

A

//ILLEGAL//LEGAL

Page 22: COMP 110: Introduction to Programming

COMP 110: Spring 200922

The Keyword super

Used to call a constructor of the base class (remember, a base class is also known as a superclass)

//constructor for the Student classpublic Student() {

super(); //call the constructor of the base classid = 0;

}

Page 23: COMP 110: Introduction to Programming

COMP 110: Spring 200923

Overriding Methods

What if the class Person had a method called printInfo?

public class Person {

// ...

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

}

Page 24: COMP 110: Introduction to Programming

COMP 110: Spring 200924

Overriding Methods

What if the class Student also had a method called printInfo?

public class Student extends Person {

// ...

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

}

Page 25: COMP 110: Introduction to Programming

COMP 110: Spring 200925

Overriding Methods

Student inherits the printInfo() methodStudent also defines its own printInfo() method

Student would seem to have two methods with the same signature

We saw before that this is illegal, so what happens?

Page 26: COMP 110: Introduction to Programming

COMP 110: Spring 200926

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 27: COMP 110: Introduction to Programming

COMP 110: Spring 200927

Example

When using an object of class Person, we call the printInfo method of the Person class

Person person = new Person("John Smith");person.printInfo(); // calls Person’s printInfo method

Output would be:Name: John Smith

Page 28: COMP 110: Introduction to Programming

COMP 110: Spring 200928

Example

When using an object of class Student, we call the printInfo method of the Student class

Student std = new Student("John Smith", 37183);std.printInfo(); // calls Student’s printInfo method,

// not Person’s

Output would be:Name: John SmithID: 37183

Page 29: COMP 110: Introduction to Programming

COMP 110: Spring 200929

Overriding vs. Overloading

If a derived class defines a method of the same name, same number and types of parameters, and SAME return type as a base class method, this is overriding

You can also have another method of the same name in the derived class, that differs in the number or types of parameters, this is overloading

Page 30: COMP 110: Introduction to Programming

COMP 110: Spring 200930

Overriding vs. Overloading

What if you define a method in the derived class that has the same name, number and types of parameters, and a DIFFERENT return type?

This is an errorIt’s not overloading because the signatures are the same (return type is not part of signature)It’s not overriding because the return type is different

Page 31: COMP 110: Introduction to Programming

COMP 110: Spring 200931

In-Class Exercise

Turn in for class participation credit

Page 32: COMP 110: Introduction to Programming

COMP 110: Spring 200932

Wednesday

More Inheritance