Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

OOP: Inheritance By: Lamiaa Said.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
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.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 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.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
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.
Inheritance and Access Control CS 162 (Summer 2009)
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
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance and Polymorphism
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
MSIS 670 Object-Oriented Software Engineering
Inheritance, Polymorphism, and Interfaces. Oh My
Java – Inheritance.
Java Programming, Second Edition
Chapter 11 Inheritance and Polymorphism Part 1
Agenda Inheritance Polymorphism Type compatibility Homework.
Presentation transcript:

Inheritance and Polymorphism

Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism whereby a new class, called a subclass, is created from an existing class, called a superclass, by absorbing its state and behavior and augmenting these with features unique to the new class. We say that the subclass inherits characteristics from its superclass.

Inheritance Hierarchy A subclass can itself be a superclass for another subclass, leading to an inheritance hierarchy of classes. For example, consider the relationship between these objects: Person, Employee, Student, GradStudent, UnderGrad.

Inheritance Hierarchy Person EmployeeStudent UnderGradGradStudent

Inheritance Hierarchy For any of these classes, an arrow points to its superclass. The arrow designates the is-a relationship. Thus, an Employee is-a Person; a Student is-a Person; a GradStudent is-a Student; an UnderGrad is-a Student. Notice that the opposite is not necessarily true: A Person may not be a Student, nor is a Student necessarily and UnderGrad. Note that the is-a relationship is transitive: If a GradStudent is-a Student and a Student is-a Person, then a GradStudent is-a Person.

Inheritance Hierarchy A subclass inherits all of the superclasses instance variables and methods. A subclass may have additional instance variables and methods. Any method of a superclass can be redefined in the subclass. This is called method overriding. If part of the original method implementation from the superclass is retained, we refer to the rewrite as partial overriding.

Extends Keyword The inheritance relationship between a subclass and a superclass is specified in the declaration of the subclass, using the keyword extends. The general format looks like this: public class Superclass { instance variables, constructors, methods, etc…} public class Subclass extends Superclass { additional instance variables, constructors, methods, etc…}

Inheritance Example Look at Inheritance Example on Website

Method Overriding and the super Keyword A method in a superclass is overridden in a subclass by defining a method with the same return type and signature(name and parameter types). For example, the computeGrade method in the UnderGrad subclass overrides the computeGrade method in the Student superclass. 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 something extra. This is achieved by using the keyword super in the implementation. The computeGrade method in the GradStudent subclass partially overrides the matching method in the Student class

Constructors and super 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 compile error will occur. Whenever the keyword super() is used in a constructor, it must be the first line.

Constructors and super If there is a default constructor in the superclass, inherited data members will be initialized as for the superclass. Additional instance variables in the subclass will get a default initialization – 0 for primitive types and null for reference types. A subclass constructor can be implemented with a call to the super method, which invokes the superclass constructor. For example, the default constructor in the UnderGrad class is identical to that of the Student class. This is implemented with the statement – super();

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 may not override static methods of the superclass. A subclass should define its own constructors. A subclass cannot access the private members of its superclass.

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 class 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.computeGrade() will all perform the correct operations for their particular instances.