Computer Science I Inheritance Professor Evan Korth New York University.

Slides:



Advertisements
Similar presentations
1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
Advertisements

Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Chapter 1 Inheritance University Of Ha’il.
OOP: Inheritance By: Lamiaa Said.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
Road Map Introduction to object oriented programming. Classes
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
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.
© 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.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Road Map Introduction to object oriented programming. Classes
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Comp 249 Programming Methodology
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Advanced Programming Behnam Hatami Fall 2017.
Presentation transcript:

Computer Science I Inheritance Professor Evan Korth New York University

2 Evan Korth New York University Road Map Inheritance –Class hierarchy –Overriding methods –Constructors –Modifier: protected –Keyword: super Reading: –Liang 5: 8.1 – 8.4 –Liang 6: 9.1 – 9.4

3 Evan Korth New York University Introduction to Inheritance Often programmers find themselves designing classes with many similarities. –How can we take advantage of these similarities to reduce the amount of code we write? –How can we take advantage of these similarities to write code which is less likely to contain errors? –How can we take advantage of existing code when we want to design something similar to the existing code?

4 Evan Korth New York University Introduction to Inheritance Inheritance is an object oriented construct which promotes code reuse by allowing you to create a new class using an existing classes as a “starting point”. –You simply state which class you want to inherit from in order to gain its members (fields and methods). –You can add or modify its members once you have inherited them. Terminology –Superclass (parent class, base class) The original class which has its members inherited Typically, this is the more general class –Subclass (child class, derived class) The class which inherits members from the superclass In this class, we can specialize the general class by modifying and adding functionality.

 2003 Prentice Hall, Inc. All rights reserved. 5 Inheritance Abstraction –Focus on commonalities among objects in system “is-a” vs. “has-a” –“is-a” Inheritance subclass object treated as superclass object Example: Car is a vehicle –Vehicle properties/behaviors also car properties/behaviors –“has-a” Composition Object contains one or more objects of other classes as members Example: Car has a steering wheel

 2003 Prentice Hall, Inc. All rights reserved Superclasses and Subclasses Superclasses and subclasses –Object of one class “is an” object of another class Example: Rectangle is quadrilateral. –Class Rectangle inherits from class Quadrilateral –Quadrilateral : superclass –Rectangle : subclass –Superclass typically represents larger set of objects than subclasses Example: –superclass: Vehicle Cars, trucks, boats, bicycles, … –subclass: Car Smaller, more-specific subset of vehicles

 2003 Prentice Hall, Inc. All rights reserved Superclasses and Subclasses (Cont.) Inheritance examples

 2003 Prentice Hall, Inc. All rights reserved Superclasses and Subclasses (Cont.) Inheritance hierarchy –Inheritance relationships: tree-like hierarchy structure –Each class becomes superclass –Supply data/behaviors to other classes AND / OR subclass –Inherit data/behaviors from other classes

 2003 Prentice Hall, Inc. All rights reserved. 9 Fig. 9.2Inheritance hierarchy for university CommunityMember s. CommunityMember EmployeeStudent StaffFaculty AdministratorTeacher Alumnus

 2003 Prentice Hall, Inc. All rights reserved. 10 Fig. 9.3Inheritance hierarchy for Shapes. Shape TwoDimensionalShapeThreeDimensionalShape CircleSquareTriangleSphereCubeTetrahedron

11 Evan Korth New York University Inheritance in Java The keyword extends is used in Java for inheritance. A class that extends another class is said to be a subclass. It is also known as a child class or a derived class. The class it extends is called a superclass. Sometimes the superclass is called a base class or a parent class. A subclass can also modify its method members. This is referred to as overriding. A subclass inherits the members of it’s superclass. Syntax: public class Student extends CommunityMember

12 Evan Korth New York University Overriding methods Often a subclass does not want to have the same exact method implementations as it’s superclass. In this case, the subclass can override the methods that it wants to change. To override a method, you must re-declare the method in the subclass. The new method must have the exact same signature as the superclass’ method you are overriding. Only non-private methods can be overridden because private methods are not visible in the subclass. Note: You cannot override static methods. Note: You cannot override fields. In the above two cases, when you attempt to override them, you are really hiding them.

13 Evan Korth New York University Overload vs Override Overloading a method refers to having two methods which share the same name but have different signatures. Overriding a method refers to having a new implementation of a method with the same signature in a subclass.

14 Evan Korth New York University Keyword super The keyword super can be used in a subclass to refer to members of the subclass’ superclass. It is used in two ways: –To invoke a constructor of the superclass (more about this on the next slide). For example (note: the keyword new is not used): super (); super (params); –To call a superclass’ method (it is only necessary to do so when you override (more on overriding soon too) the method). For example: super.superclassMethodName(); super.superclassMethodName(params);

15 Evan Korth New York University Inheritance and constructors Constructors do not get inherited to a subclass. Instead, the subclass’ constructor automatically calls it’s superclass’ zero parameter constructor before it executes any of the code in it’s own constructor. If you do not want to use the parent’s zero parameter constructor, you must explicitly invoke one of the superclass’ other constructors with the keyword super. –If you do this, the call to the superclass’ constructor must be the first line of code in the subclass’ constructor. e.g: Student (String name) { super (?params?); … If the superclass has no zero parameter constructor, the first line of any subclass constructor MUST explicitly invoke another superclass constructor.

16 Evan Korth New York University Indirect Inheritance It is possible in Java to have an arbitrarily long line of ancestors (i.e. many superclasses on your way to Object) The members you inherit from classes above your superclass are said to be indirectly inherited. It does not matter where a class’ members come from – a client program always uses similar syntax to access the fields and methods (whether you define it in a class, inherit it directly from your superclass or indirectly from another class in the hierarchy).