Inheritance and Polymorphism

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Chapter 9 Inheritance and Polymorphism. Object-oriented programming is based on a paradigm in which objects are used to model a specification. Objects.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
One class is an extension of another.
Inheritance Basics Programming with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Computer Programming with JAVA
Java Inheritance.
Agenda Inheritance Polymorphism Type compatibility Homework.
Presentation transcript:

Inheritance and Polymorphism

Inheritance Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is the mechanism whereby a new class, called a subclass. The subclass is created from an existing class called a superclass.

A subclass is bigger than a superclass – it contains more data and more methods. Inheritance provides an effective mechanism for code reuse.

A subclass can be a superclass for another subclass, leading to an inheritance hierarchy of classes.

Relationship between Objects Person Student Employee GradStudent UnderGrad

The arrows designates the is-a relationship The arrows designates the is-a relationship. The Employee is–a Person, a Student is-a Person, a GradStudent is-a Student; an UnderGrad is-a Student. The opposite is not necessarily true; a Person may not be a Student, nor is a Student necessarily an UnderGrad.

The is-a relationship is transitive: If a GradStudent is-a Student and a Student is-a Person, then a GradStudent is-a Person. Suppose the Person class has instance variables name, socialSecurityNumber, and age and instance methods getName, getSocSecNum, getAge, and printName.

Then every one of the derived classes shown inherits these variable and methods. The student class may have additional instance variables, like studentID and gpa, plus a method computeGrade. All of these are inherited by the subclasses GradStudent and UnderGrad.

Suppose GradStudent and UnderGrad use different algorithms for computing the course grade. The computeGrade implementation can be redefined in these classes. This is called method overriding.

The inheritance relationship between a subclass and a superclass is specified in the declaration of the subclass, using the keyword extends. public class Superclass { } public class Subclass extends Superclass

Inheriting Instance Methods & Variables The UnderGrad and GradStudent subclasses inherit all of the methods and variable of the Student superclass. The student instance variables myName, myTests, and myGrade are private and not directly accessible to the methods in the UnderGrade and GradStudent subclasses.

The subclass can, directly invoke the public accessor and mutator methods of the superclass. Both GradStudent and UnderStudent use getTestAverage.

If instead of private, access specifier for the instance variable in Student were protected, then the subclasses could directly access these variable. The keyword protected is not part of the AP Java subset.

Classes on the same level in a hierarchy diagram do not inherit anything from each other. All they have in common is the identical code they inherit from their superclass.

A method in a superclass is overridden in a subclass by defining a method with the same return type and signature. Sometimes the code for overriding a method includes a call to the superclass method. This is called partial overriding. Typically this occurs when the subclass method wants to do what the superclass does, plus extra.

Constructors and super Constructors are never inherited. If no constructor is written for a subclass, the superclass default constructor with no parameters is generated. If the superclass does not have a default constructor, but only a constructor with parameters, a compiler error will occur.

If there is a default constructor in the superclass inherited data members will be initialized for the superclass. A subclass constructor can be implemented with a call to the super method, which invokes the superclass constructor.

Rules for Subclasses A subclass can add new private instance variables. A subclass can add new public, private or static methods. A subclass can override inherited methods A subclass may not redefine a public method as private. A subclass should define its own constructors A subclass cannot directly access the private members of its superclass. It must use accessor or mutator methods.

When a variable of a superclass is declared in a client program, that reference can refer not only to an object of the superclass, but also to objects of any of its subclasses. Each of the following is legal: Student s = new Student(); Student g = new GradStudent(); Student u = new UnderGrad();

Student s = new Student(); Student g = new GradStudent(); Student u = new UnderGrad(); This works because a GradStudent is-a Student. GradStudent g = new Student(); UnderGrad u = new Student(); Since a Student is not necessarily a GradStudent or an UnderGrad the above is not valid.

Are these valid declarations: Student s = new Student(“Brian Lorenzen”, new int[] {90, 94, 99}, “none”); Student u = new UnderGrad(“Tim Broder”, new int [] {90, 90, 100}, “none”); Student g = new GradStudent(“Kevin Cristella”, new int[], {85, 70, 90}, “none”, 1234);

Suppose you make the method call s Suppose you make the method call s.setGrade(“Pass”); The appropriate method in Student is found and the new grade assigned. The method calls g.setGrade(“Pass”); And u.setGrade(“Pass”); achieve the same effect on g and u since GradStudent and UnderGrad both inherit the setGrade method from Student.

Do the following method calls work. int studentNum = s Do the following method calls work? int studentNum = s.getID(); int underGradNum = u.getID();

s.computeGrade(); g.computeGrade(); u.computeGrade();

Polymorphism A method that has been overridden in at least one subclass is said to be polymorphic. An example is computeGrade, which is redefined for both GradStudent and underGrad. Polymorphism is the mechanism of selecting the appropriate method for a particular object in a hierarchy.

The correct method is chosen because, in Java, method calls are always determined by the type of the actual object, not the type of the object reference. For example, even though s, g and u are all declared as type Student, s.computeGrade(), g.computeGrade();, and u.computerGrade() will all perform the correct operations for their particular instances.

Dynamic Binding Making a run-time decision about which instance method to call is known as dynamic binding. In polymorphism, the actual method that will be called is not determined by the compiler.

Interfaces An interface is a class with method declarations that have no implementations. An interface cannot be inherited. An interface can add behavior to a class, but it does not provide a hierarchy for the class. It may only be implemented in a class. Methods defined in an interface are by default public and abstract. The methods in an interface are only declarations followed by a semicolon.

The comparable interface is part of the java.lang.package. The method is compareTo(Object obj). Returns 0 when obj is the same as the object Returns a negative integer when obj is less than the object Returns a positive integer when obj is greater than the object. Based on this if you missed it on the last test. I will add the points to your score.

When an interface in implemented in a class, the class must implement each method defined in the interface.

Critical Thinking Page 235 – Critical Thinking Question 6 and Question 7. Turn in when you finish. Move the following files: Clarinet Music Piccolo Woodwind Instrument Vocal Performance Draw out the relationship between these files Review: Music – part 1 of 2

Puck Create a Puck class that inherits the Disk class. The Puck class should include member variables weight, standard and youth. The standard and youth variables should be boolean variables that are set to either true or false depending on the weight of the puck. A standard puck weighs between 5 and 5.5 ounces. A youth puck weighs between 4 and 4.5 ounces. Official hockey pucks, regardless of weight, are one inch-thick with a three-inch diameter. The Puck class should also contain member methods getWeight(), getDivision(), which returns a string stating whether the puck is standard or youth, and equals() and toString(), which override the same methods in the Disk class. The Puck constructor should require an argument for weight. Be sure that the constructor initializes other variables to appropriate values as necessary. Create a Hockey application that tests the Puck class.