Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

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.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
Inheritance Inheritance Reserved word protected Reserved word super
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
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.
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.
Java Class and Inheritance.
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Intro to OOP with Java, C. Thomas Wu
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
Chapter 10 Inheritance and Polymorphism
Peyman Dodangeh Sharif University of Technology Fall 2014.
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Inheritance  Inheritance is a fundamental object-oriented technique  it enhances software design and promotes reuse  We will focus on:  deriving new.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
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)
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Topics Inheritance introduction
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Modern Programming Tools And Techniques-I Inheritance
Java Inheritance.
MSIS 670 Object-Oriented Software Engineering
Advanced Programming Behnam Hatami Fall 2017.
Java Inheritance.
Computer Science II for Majors
Presentation transcript:

Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/15 ▪I▪Inheritance

Inheritance ▪Inheritance is a form of software reuse in which you create a class that absorbs an existing class’s data and behaviors and enhances them with new capabilities ▪When creating a class, you can designate that the new class should inherit the members of an existing class ›The existing class is called the base class (or parent class) ›The new class is called the derived class (or child class) USC CSCI 201L3/15 Inheritance

Single vs Multiple Inheritance ▪Single inheritance allows a derived class to inherit from only one base class ›Java supports single inheritance ▪Multiple inheritance allows a derived class to inherit from more than one base class ›C++ supports multiple inheritance ›There is a potential problem with multiple inheritance if more than one base class provides an implementation for the same function USC CSCI 201L4/15 Inheritance

Multiple Inheritance in C++ USC CSCI 201L5/15 Inheritance 1 #include 2 using namespace std; 3 4 class _Reader { 5 public: 6 void read_ () { 7 cout << "reading " << endl; 8 } 9 }; class Telephone { 12 public: 13 void make_call() { 14 cout << "making call" << endl; 15 } 16 }; class IPhone : public Telephone, public _Reader { 19 public: 20 void buy_app() { 21 cout << "buying app" << endl; 22 } 23 }; void main() { 26 IPhone ip; 27 ip.buy_app(); 28 ip.make_call(); 29 ip.read_ (); 30 }

Multiple Inheritance in C++ USC CSCI 201L6/15 Inheritance 1 #include 2 using namespace std; 3 4 class _Reader { 5 public: 6 void read_ () { 7 cout << "reading " << endl; 8 } 9 void send_ () { 10 cout << " sending "; 11 } 12 }; class Telephone { 15 public: 16 void make_call() { 17 cout << "making call" << endl; 18 } 19 void send_ () { 20 cout << "Telephone sending "; 21 } 22 }; 23 class IPhone : public Telephone, public _Reader { 24 public: 25 void buy_app() { 26 cout << "buying app" << endl; 27 } 28 }; void main() { 31 IPhone ip; 32 ip.buy_app(); 33 ip.make_call(); 34 ip.read_ (); 35 ip.send_ (); // does this line compile? 36 }

Multiple Inheritance in C++ USC CSCI 201L7/15 Inheritance 1 #include 2 using namespace std; 3 4 class _Reader { 5 public: 6 void read_ () { 7 cout << "reading " << endl; 8 } 9 void send_ () { 10 cout << " sending "; 11 } 12 }; class Telephone { 15 public: 16 void make_call() { 17 cout << "making call" << endl; 18 } 19 void send_ () { 20 cout << "Telephone sending "; 21 } 22 }; 23 class IPhone : public Telephone, public _Reader { 24 public: 25 void buy_app() { 26 cout << "buying app" << endl; 27 } 28 }; void main() { 31 IPhone ip; 32 ip.buy_app(); 33 ip.make_call(); 34 ip.read_ (); 35 ip.Telephone::send_ (); 36 ip. _Reader::send_ (); 37 }

Inheritance vs Composition ▪is-a Relationship ›If an object has an “is-a” relationship with another object, inheritance will be used ›Vehicle, Car, Truck, Motorcycle ▪has-a Relationship ›If an object has a “has-a” relationship with another object, composition will be used ›Car, Steering Wheel, Brake Pedal, Speedometer USC CSCI 201L8/15 Inheritance

Access Methods ▪public ›Any other class has access to public member variables and methods ▪protected ›Subclasses and classes within the same package have access to protected member variables and methods ▪ ›Other classes within the same package have access to member variables and (which is the default access) ▪private ›Only the current class has access to the member variables and methods USC CSCI 201L9/15 Inheritance

Instantiating a Child Class ▪To inherit from another class, use the keyword extends immediately following the name of the class, followed by the name of the class from which you would like to inherit ▪When a child class is instantiated, the parent class must be instantiated first in the child class’s constructor ›This will happen automatically by the compiler calling the parent class’s default constructor unless we explicitly instantiate the parent ›Note that if there is no default constructor in the parent, we MUST explicitly call the parent class’s constructor from the child ▪When we call the parent class’s constructor from the child, it must be the first line of code in the child class’s constructor USC CSCI 201L10/15 Inheritance 1 public class TwoDShape extends Shape { 2 public TwoDShape(char name){ 3 super(name); 4 } 5 } 1 public class Shape { 2 protected char name; 3 public Shape(char n) { 4 name = n; 5 } 6 }

Inheritance Example USC CSCI 201L11/15 Inheritance 1 class Parent { 2 private int num; 3 public Parent(int num) { 4 this.num = num; 5 } 6 public int meth() { 7 return num; 8 } 9 } public class Child extends Parent { 12 public Child() { } 15 public static void main(String [] args) { 16 Child c = new Child(); 17 System.out.println(c.meth()); 18 } 19 }

Inheritance Example USC CSCI 201L12/15 Inheritance 1 class Parent { 2 private int num; 3 public Parent(int num) { 4 this.num = num; 5 } 6 public int meth() { 7 return num; 8 } 9 } public class Child extends Parent { 12 public Child() { 13 super(10); 14 } 15 public static void main(String [] args) { 16 Child c = new Child(); 17 System.out.println(c.meth()); 18 } 19 }

Inheritance Hierarchy ▪To show a child class and a parent class in a diagram, we draw a line connecting the child class to the parent class where the parent class is above the child ▪Assume the following hierarchy for the rest of this lecture USC CSCI 201L13/15 Inheritance Shape 2-D Shape3-D Shape TriangleRectangle CircleSquareSphere ConeBox Cube EllipseEllipsoid

Inheritance Example USC CSCI 201L14/15 Inheritance 1class Shape { 2 protected char name; 3 public Shape(char n) { 4 name = n; 5 } 6 public void printName() { 7 System.out.println(name); 8 } 9} 1class TwoD extends Shape { 2 public TwoD(char name) { 3 super(name); 4 } 5} 1class Triangle extends TwoD { 2 private float base; 3 private float height; 4 public Triangle(char nm, float b, float h) { 5 super(nm); 6 base = b; 7 height = h; 8 } 9 public float getArea() { 10 return 0.5f * base * height; 11 } 12} 1class Rectangle extends TwoD { 2 protected float width, length; 3 public Rectangle(char nm, float w, float l) { 4 super(nm); 5 width = w; 6 length = l; 7 } 8 public float getArea() { 9 return width * length; 10 } 11} 1class Square extends Rectangle { 2 public Square(char nm, float s) { 3 super(nm, s, s); 4 } 5}

Inheritance Example (cont.) 1 public class Test { 2 public static void main() { 3 Triangle t = new Triangle(‘t’, 3.0f, 4.0f); 4 t.printName(); 5 System.out.println(t.getArea()); 6 Rectangle r = new Rectangle(‘r’, 5.0f, 6.0f); 7 r.printName(); 8 System.out.println(r.getArea()); 9 Square sq = new Square(‘s’, 3); 10 sq.printName(); 11 System.out.println(sq.getArea()); 12 } USC CSCI 201L15/15 Inheritance