26
COMP 110: Spring 2009 1 Announcements Assignment 4 Due Today Lab 8 Due Wednesday Assignment 5 Online

Announcements

Embed Size (px)

DESCRIPTION

Announcements. Assignment 4 Due Today Lab 8 Due Wednesday Assignment 5 Online. Today in COMP 110. Inheritance In-Class Exercise. Inheritance. What are some properties of a Person? Name, height, weight, age How about a Student? ID, major - PowerPoint PPT Presentation

Citation preview

COMP 110: Spring 20091

Announcements

Assignment 4 Due TodayLab 8 Due WednesdayAssignment 5 Online

COMP 110: Spring 20092

Today in COMP 110

Inheritance

In-Class Exercise

COMP 110: Spring 20093

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

COMP 110: Spring 20094

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

COMP 110: Spring 20095

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!

COMP 110: Spring 20096

Inheritance

Define a general class

Later, define specialized classes based on the general class

These specialized classes inherit properties from the general class

COMP 110: Spring 20097

Inheritance

Examples of class inheritance

Person

Student Employee

Undergrad Grad

Masters Doctoral Nondegree

Faculty Staff

Transportation

Car Airplane Animal

ElephantHorse Camel

COMP 110: Spring 20098

Base Class

Our general class is called a base class

Also called a parent class or a superclass

Examples:Person, Transportation

COMP 110: Spring 20099

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

COMP 110: Spring 200910

Derived Classes

Student is a child class of Person

Student is also the parent class of Undergrad and Grad Person

Student Employee

Undergrad Grad

Masters Doctoral Nondegree

Faculty Staff

COMP 110: Spring 200911

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

COMP 110: Spring 200912

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

COMP 110: Spring 200913

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!

COMP 110: Spring 200914

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

COMP 110: Spring 200915

Private vs. Public

private instance variables and private methods in the base class are NOT accessible by derived classes

This would not work:

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

COMP 110: Spring 200916

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);}

COMP 110: Spring 200917

Protected visibility

protected instance variables of the base class CAN be accessed by derived classes just like public variables, but cannot be accessed by other external classes

protected String name;

COMP 110: Spring 200918

The Keyword super

Used to call a method 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;

}

public setAttributes(String name, int id) {super.setName(name); //call the setName methodid = 0;

}

COMP 110: Spring 200919

Overriding Methods

What if the class Person had a method called printInfo?

public class Person {

// ...

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

}

COMP 110: Spring 200920

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());}

}

COMP 110: Spring 200921

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?

COMP 110: Spring 200922

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

COMP 110: Spring 200923

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

COMP 110: Spring 200924

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

COMP 110: Spring 200925

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

COMP 110: Spring 200926

Wednesday

More Inheritance