© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Introduction To Scientific Programming Chapter 7 – Inheritance or (More on Classes)
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
Inheritance Recitation - 02/22/2008 CS 180 Department of Computer Science, Purdue University.
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS102--Object Oriented Programming Lecture 9: – Polymorphism Copyright © 2008 Xiaoyan Li.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Constructors. Defining Constructors  A constructor is a special kind of method that is designed to perform initializations, such as giving values to.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
ITM 352 Class inheritance, hierarchies Lecture #.
© 2013 Ken Howard, Southern Methodist University Object-Oriented Programming: Inheritance CSE 1341.
UML (U NIFIED M ODELING LANGUAGE ) Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University.
Unit 2 Review (Part 1) Arrays, Inheritance, and the Ethical Use of Computers.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
COMP Inheritance Basics Yi Hong June 09, 2015.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Software Development Java Classes and Methods
Polymorphism 2nd Lecture
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CS 302 Week 11 Jim Williams, PhD.
Inheritance 2nd Lecture
ارث بری 2 استفاده ی مجدد از کلاس توسط وراثت
Inheritance Basics Programming with Inheritance
Polymorphism 2nd Lecture
ارث بری 2 استفاده ی مجدد از کلاس توسط وراثت
Extending Classes.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
Inheritance, Polymorphism, and Interfaces. Oh My
Comp 249 Programming Methodology
Inheritance 2nd Lecture
Inheritance.
Announcements Lab 8 Was Due Today Lab 7 Regrade Due Thursday
Computer Programming with JAVA
Inheritance 2nd Lecture
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Programming
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
CMSC 202 Inheritance II.
Presentation transcript:

© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any other purpose without the written permission of Walter Savitch.

Midterm Warning In lecture Tuesday February 4. Quiz grades are not so great. Pick up graded quizzes from TA. Do Self-Test Exercises. Sample midterm on web page and in public. Can bring notes on one index card.

You Must Have an Approved Picture ID at the Midterm or You Get Zero Only the following will do A UCSD Picture ID A Current Picture Drivers License A Passport Other ID approve (in person not via ) by instructor at least 48 hours before the midterm

Inheritance Make new class (derived class) from an old class (base class). Derived class automatically has (inherits) all the methods and instance variables of the base class. Derived class normally also adds instance variables and/or methods.

A (Base) Class public class Person { private String name; public Person( ) { name = "No name yet.";} public Person(String initialName) { name = initialName; }

public void setName(String newName) { name = newName;} public String getName( ) { return name;} public void writeOutput( ) { System.out.println("Name: " + name);} public boolean sameName(Person otherPerson) { return (this.name.equalsIgnoreCase(otherPerson.name);} }

A Derived Class public class Student extends Person { private int studentNumber; public Student( ) { super( ); //Invokes base class constructor //to initialize namme instance variable studentNumber = 0 } public Student(String initialName, int initialStudentNumber) { super(initialName); studentNumber = initialStudentNumber; }

A Derived Class Continued public void reset(String newName, int newStudentNumber) { setName(newName); studentNumber = newStudentNumber; } public int getStudentNumber( ) { return studentNumber; } public void setStudentNumber(int newStudentNumber) { studentNumber = newStudentNumber; }

A Derived Class Continued public void writeOutput( ) { System.out.println("Name: " + getName( ) ); System.out.println("Student Number: " + studentNumber); } public boolean equals(Student otherStudent) { return (this.sameName(otherStudent) && (this.studentNumber == otherStudent.studentNumber)); }

Private instance variables in the base class Are inherited by the derived class. Cannot be accessed by name in the definition of the derived class. Must be handled using public accessor and mutator methods inherited from the base class.

Overriding a Method Definition In a derived class, if you include a method definition that has the same name and the exact same number and types of parameters as a method already in the base class, then for the derived class, this new definition replaces the old definition of the method.

public class Person { private String name; public void writeOutput( ) { System.out.println("Name: " + name); }

public class Student extends Person { private int studentNumber; public void writeOutput( ) { System.out.println("Name: " + getName( ) ); System.out.println("Student Number: " + studentNumber); }

Student s = new Student(); … s.writeOutput( ); //uses the def. in Student, // not the one in Person

Overriding Versus Overloading If a method defined in the derived class has the exact same parameter list as a method of the same name in the base class, that is overriding. In the derived class the definition of the method is changed. If a method defined in the derived class does not have the exact same parameter list as a method of the same name in the base class, that is overloading. The derived class has both methods.

Call to an Overridden Method public class Student extends Person { private int studentNumber; public void writeOutput( ) { System.out.println("Name: " + getName( ) ); System.out.println("Student Number: " + studentNumber); }

Call to an Overridden Method public class Student extends Person { private int studentNumber; public void writeOutput( )//Alternative def. { super.writeOutput( ); //uses def. in Person System.out.println("Student Number: “ + studentNumber); }

The Class Object In Java, every class is a descendent of the predefined class Object

public class Species { private String name; private int population; private double growthRate; … MEANS

MEANS: public class Species extends Object { private String name; private int population; private double growthRate; …

Methods in the Class Object equals clone toString

equals Method public boolean equals(Object other)

public boolean equals(Object otherObject) { if (otherObject == null) return false; else if (!(otherObject instanceof Student)) return false; else { Student otherStudent = (Student)otherObject; return (this.sameName(otherStudent) && (this.studentNumber == otherStudent.studentNumber)); } }