COMP 110-001 Inheritance Basics Yi Hong June 09, 2015.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Apr 13, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
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
Introduction To Scientific Programming Chapter 7 – Inheritance or (More on 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.
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.
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.
Inheritance Recitation - 02/22/2008 CS 180 Department of Computer Science, Purdue University.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Creating Classes from Other Classes Chapter 2. 2 Chapter Contents Composition Adapters Inheritance Invoking constructors from within constructors Private.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance using Java
Intro to OOP with Java, C. Thomas Wu
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.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
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 Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
Inheritance and Polymorphism
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
COMP 110 More about inheritance
ATS Application Programming: Java Programming
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
CSC 143 Inheritance.
Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
Extending Classes.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance.
Announcements Lab 8 Was Due Today Lab 7 Regrade Due Thursday
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Chapter 7 Inheritance.
Presentation transcript:

COMP Inheritance Basics Yi Hong June 09, 2015

Today  Inheritance

Inheritance  We have discussed before how classes of objects can have relationships Person StudentEmployee UndergradGrad MastersDoctoralNondegree FacultyStaff Transportation CarAirplaneAnimal ElephantHorseCamel

Inheritance  Define a general class  Later, define specialized classes based on the general class  These specialized classes inherit properties from the general class Person StudentEmployee UndergradGrad MastersDoctoralNondegree FacultyStaff

Inheritance  What are some properties of a Person? Name, height, weight, age  How about a Student? ID, major  Does a Student have a name, height, weight, and age? Student inherits these properties from Person

The is-a Relationship  This inheritance relationship is known as an is-a relationship  A Doctoral student is a Grad student  A Grad student is a Student  A Student is a Person  Is a Person a Student? Not necessarily! Person StudentEmployee UndergradGrad MastersDoctoralNondegree FacultyStaff

Base Class  Our general class is called a base class Also called a parent class or a superclass  Examples: Person, Transportation

Derived Class  A specialized class that inherits properties from a base class is called a derived class Also called a child class or a subclass  Examples: Student is-a Person Employee is-a Person Car is-a form of Transportation Animal is-a form of Transportation Person StudentEmployee Transportation CarAirplaneAnimal

Child (Derived) Classes Can Be Parent (Base) Classes  Student is a child class of Person  Student is also the parent class of Undergrad and Grad Person StudentEmployee UndergradGrad MastersDoctoralNondegree FacultyStaff

Why Is Inheritance Useful?  Enables you to define shared properties and actions once  Derived classes can perform the same actions as base classes without having to redefine the actions If desired, the actions can be redefined – more on this later

How Does This Work in Java? public class Person { private String name; public Person() { name = “No name yet”; } public void setName(String newName) { name = newName; } public String getName() { return name; } Person - name + setName(String newName): void + getName(): String

How Does This Work in Java? public class Student extends Person { private int id; public Student() { super(); id = 0; } public Student(String stdName, int idNumber) { setName(stdName); setID(idNumber); } public void setID(int idNumber) { id = idNumber; } public int getID() { return id; } Student - id + setID(int idNumber): void + getID(): int Person - name + setName(String newName): void + getName(): String

The extends keyword public class Derived_Class_Name extends Base_Class_Name { Declaration_of_Added_Instance_Variables Definitions_of_Added_And_Overridden_Methods } public class Student extends Person { // stuff goes here }  A derived (child) class inherits the public instance variables and public methods of its base (parent) class

private vs. public  private instance variables and private methods in the base class are NOT inherited by derived classes  This would not work: public Student(String stdName, int idNumber) { name = stdName; // ERROR! name is private to Person setID(idNumber); }

private vs. public  private instance variables of the base class CAN be accessed by derived classes using the base class’ public methods  This works: public Student(String stdName, int idNumber) { setName(stdName); // OK! setName is a public method in Person setID(idNumber); }

The super keyword  A derived class does not inherit constructors from its base class  Constructors in a derived class invoke constructors from the base class  Use super within a derived class as the name of a constructor in the base class (superclass) E.g.: super(); or super(intialName); Person(); or Person(intialName) // ILLEGAL First action taken by the constructor, without super, a constructor invokes the default constructor in the base class

this v.s. super public Person() { this(“No name yet”); } public Person(String initialName) { name = initialName; }  When used in a constructor, this calls a constructor of the same class, but super invokes a constructor of the base class

Overriding Methods  What if the class Person had a method called printInfo? public class Person { // a bunch of other stuff //... public void printInfo() { System.out.println(name); }

Overriding Methods  What if the class Student also had a method called printInfo? public class Student extends Person { // a bunch of other stuff //... public void printInfo() { System.out.println("Name: " + getName()); System.out.println("ID: " + getID()); }

Overriding Methods  If Student inherits the printInfo() method and defines its own printInfo() method, it would seem that Student has two methods with the same signature We saw before that this is illegal, so what’s the deal?

Overriding Methods  Java handles this situation as follows: If a derived class defines a method with the same name, number and types of parameters, and return type as a method in the base class, the derived class’ method overrides the base class’ method The method definition in the derived class is the one that is used for objects of the derived class

Overriding Methods: Example  Both Person and Student have a printInfo() method Student std = new Student("John Smith", 37183); std.printInfo(); // calls Student’s printInfo method, // not Person’s  Output would be: Name: John Smith ID: 37183

Overriding vs. Overloading  If a derived class defines a method of the same name, same number and types of parameters, and same return type as a base class method, this is overriding  You can still have another method of the same name in the same class, as long as its number or types of parameters are different: overloading

The final Modifier  A final method cannot be overridden E.g.: public final void specialMethod()  A final class cannot be a base class E.g.: public final class myFinalClass { … } public class ThisIsWrong extends MyFinalClass { …} // forbidden

 Given this inheritance hierarchy… 25 Type Compatibilities Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

 Person per = new Person(); Yes! 26 Is This Code Legal? Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

 HighJumper hJumper = new HighJumper(); Yes! 27 Is This Code Legal? Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

 Person per = new Athlete(); Yes! An Athlete is a Person, so this is okay 28 Is This Code Legal? Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

 Skydiver sDiver = new Person(); No! A Person is not necessarily a Skydiver, so this is illegal 29 Is This Code Legal? Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

 Athlete ath = new Athlete(); XGamesSkater xgs = ath; No! An Athlete is not necessarily an XGamesSkater, so this is illegal 30 Is This Code Legal? Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

Summary  An object of a derived class can serve as an object of the base class  An object can have several types because of inheritance E.g: every object of the class Undergraduate is also an object of type Student, as well as an object of type person Person StudentEmployee UndergradGrad FacultyStaff

Next Class  Inheritance and Polymorphism