Inheritance. For example, to zoom in on the deer family (cervid), we could make a tree like the following.

Similar documents
Inheritance and Polymorphism

public Twix() { calories = 285; ingredients = "chocolate, sugar, cookie, caramel"; }

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

Programming Exercise 14: Inheritance and Polymorphism

CIS 110: Introduction to computer programming

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance, Polymorphism, and Interfaces

TaxiBot New attributes Variables Math! TaxiBot

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Java Object Oriented Design. CSC207 Fall 2014

C++ Important Questions with Answers

Data Structures (list, dictionary, tuples, sets, strings)

Chapter 14 Abstract Classes and Interfaces

Inheritance and Polymorphism. CS180 Fall 2007

Inheritance and Polymorphism

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

What is Inheritance?

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS 251 Intermediate Programming Inheritance

Super-Classes and sub-classes

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

More Relationships Between Classes

Principles of Object Oriented Programming. Lecture 4

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Inheritance (Part 2) Notes Chapter 6

CSC 1214: Object-Oriented Programming

9/17/2018 Programming Data Structures. Encapsulation and Inheritance

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Chapter 7. Inheritance

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

CSCE 145 Midterm 2 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!

First IS-A Relationship: Inheritance

COMP 110/L Lecture 19. Kyle Dewey

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

ITI Introduction to Computing II

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

INHERITANCE. Spring 2019

Example: Count of Points

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

A First Object. We still have another problem. How can we actually make use of the class s data?

ITI Introduction to Computing II

Inheritance CSC 123 Fall 2018 Howard Rosenthal

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

PROGRAMMING LANGUAGE 2

Abstract class & Interface

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Inheritance and Polymorphism in Java

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

About 1. Chapter 1: Getting started with oop 2. Remarks 2. Examples 2. Introduction 2. OOP Introduction 2. Intoduction 2. OOP Terminology 3.

Java Programming Lecture 7

Pointers, Arrays and Parameters

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

Carleton University COMP1406/1006, Summer 2013 Tutorial 2

CS313D: ADVANCED PROGRAMMING LANGUAGE

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CISC370: Inheritance

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Example: Count of Points

Building custom components IAT351

Overriding Variables: Shadowing

Classes Classes 2 / 36

Inheritance. Chapter 7. Chapter 7 1

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Inheritance. Transitivity

Inheritance & Polymorphism

Chapter 5. Inheritance

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Informatik II (D-ITET) Tutorial 6

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

Object-Oriented Programming More Inheritance

CS-202 Introduction to Object Oriented Programming

Exercise: Singleton 1

Instance Members and Static Members

Inheritance and Polymorphism

Inheritance -- Introduction

Inheritance in Ruby. You are familiar with the idea of inheritance and how to use this in programming.

Classes, Objects, and OOP in Java. June 16, 2017

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Defining Classes and Methods

Transcription:

Inheritance The concept of inheritance is one of the key features of an object-oriented programming language. Inheritance allows a programmer to define a general class, and then later define more specific classes that share or inherit all of the properties of the more general class. This allows the programmer to save time and energy that might otherwise be spent writing duplicate code. Related to inheritance is the concept of polymorphism. Polymorphism allows us to invoke the correct method in an inheritance hierarchy. We ll look at this in more detail later. The term inheritance comes from the genetic inheritance of traits. For example, you inherit characteristics from your parents. We can explore this idea with the taxonomy of living things. You may recall the following hierarchy from a biology class: For example, to zoom in on the deer family (cervid), we could make a tree like the following.

Note that this hierarchy is an IS-A hierarchy. A deer IS-A cervid. In turn, a cervid IS-A ruminant, and so forth. The term parent is the term used to describe concepts going up the hierarchy, and child is the term when going down the hierarchy (imagine a family tree). For example, a deer is a child of cervid, and cervid is the parent of deer. Note that any property that holds for a parent also holds for a child. For example, if we know that mammals are warm-blooded, then we know that everything listed below mammal is also warm-blooded. We ll take advantage of this concept from a programming perspective to reduce duplicative code. As an example, there is a formula to estimate how many calories a man or woman needs to eat to survive (maintain weight if just resting all day). The formula is a little different for men vs. women. Here is the formula for men to compute how many Twix bars are needed to be eaten to maintain one s weight (where a package of Twix bars has 568 calories): calories = 10 * kilograms + 6.25 * centimeters - 5 * age + 5; numtwix = calories / 568; For women the formula is: calories = 10 * kilograms + 6.25 * centimeters - 5 * age - 161; numtwix = calories / 568; Depending on our application it may be reasonable to make a Man class and a Woman class (this depends on the application, if men and women are basically treated the same then it would be better to have a single Person class). Assuming separate classes make sense, here is how we might create each one:

public class Man private int age; // Private instance variables describing the man private int weight; // Normally there would be accessors/mutators private int feet; // In this case, I only made one for age private int inches; // Default values in the default constructor public Man() age = 20; weight = 150; feet = 5; inches = 8; // Constructor that sets all the instance variables public Man(int age, int weight, int feet, int inches) this.age = age; this.weight = weight; this.feet = feet; this.inches = inches; // Accessor for age public int getage() return age; // Mutator for age public void setage(int newage) this.age = newage; // Compute how many twix bars to maintain weight double kilograms = weight / 2.2; double centimeters = ((feet * 12) + inches) * 2.54; double calories = 10 * kilograms + 6.25 * centimeters - 5 * age + 5; double numtwix = calories / 568; // 568 cals per pack return numtwix;

We might use this class in something like the following, which could be in a main method. It makes a new Man object of age 40, weight 140, that is 5 feet 6 inches tall, then outputs the age and how many twix bars the man needs to eat. The output is: Man kenrick = new Man(40, 140, 5,6); System.out.println("This person of age " + kenrick.getage() + " should eat " + kenrick.howmanytwix() + " twix bars."); This person of age 40 should eat 2.62 twix bars. We can create a Woman class in a similar manner: public class Woman private int age; private int weight; private int feet; private int inches; // Default values in the default constructor public Woman() age = 20; weight = 120; feet = 5; inches = 6; // Constructor that sets all the instance variables public Woman(int age, int weight, int feet, int inches) this.age = age; this.weight = weight; this.feet = feet; this.inches = inches; // Accessor/Mutator for age public int getage() return age; public void setage(int newage) this.age = newage; // Twix calculation double kilograms = weight / 2.2;

double centimeters = ((feet * 12) + inches) * 2.54; double calories = 10 * kilograms + 6.25 * centimeters 5 * age - 161; double numtwix = calories / 568; return numtwix; These two classes will work fine, but it s totally inefficient. The code is almost identical except for some values in the default constructor and the formula for how twix bars are calculated. There are various approaches we could take, such as creating a method that takes a parameter indicating man or woman, but if want to keep the object-based structure then inheritance lets us eliminate lots of duplicate code. In this case we ll make a parent class called Person and define the Man and Woman classes as children of the Person class. In OOP terminology, Person is called the base class and Man and Woman are called the derived classes. Derived classes will inherit all of the public (or protected) methods and instance variables of its parents so we don t have to write these more than once! Private instance variables defined in a parent class exist in derived classes, but can t be accessed directly by name. Instead they must be accessed through a method. Private methods defined in a parent class can t be accessed directly from a derived class. To create a derived class, we add the keyword extends to the class definition, followed by the name of the base class. E.g.: public class Man extends Person Here is the first version of our new class for Person. We only put methods and instance variables in the parent class if they are applicable to all sub-classes.

public class Person private int age; private int weight; private int feet; private int inches; // Default values in the default constructor public Person() age = 20; weight = 150; feet = 5; inches = 8; // Constructor that sets all the instance variables public Person(int age, int weight, int feet, int inches) this.age = age; this.weight = weight; this.feet = feet; this.inches = inches; // Accessor for age public int getage() return age; // Mutator for age public void setage(int newage) this.age = newage; // Don't know so just return 0 // This makes the method just a placeholder return 0; This basically contains all of the common code from the original version of the Man and the Woman class. We made howmanytwix() just return 0 since we don t know which formula to use for a generic Person. Later we ll see a way to define this as a true placeholder (it s called an abstract method). Here is the beginning of the Man class:

public class Man extends Person // Default constructor public Man() super(); // This invokes the default constructor // of the base class. It is done automatically // if you leave it off. // Constructor taking arguments public Man(int age, int weight, int feet, int inches) super(age, weight, feet, inches); // Invoke // the parent constructor that takes arguments If we run the same sample code as before: The output is: Man kenrick = new Man(40, 140, 5,6); System.out.println("This person of age " + kenrick.getage() + " should eat " + kenrick.howmanytwix() + " twix bars."); This person of age 40 should eat 0.0 twix bars. In the constructor for the Man class we are first invoking the method super( ). This invokes the default constructor for the base class, Person. If we do this, it must be the first line in the constructor. If we leave this line out, the default constructor of the base class gets invoked anyway, so often programmers will just leave this out. Note that we do not use the name of the constructor, instead we use the keyword super. The second constructor takes four parameters, one for age, weight, feet, inches. To invoke the base class constructor with four arguments we use super( ) again but fill in the arguments: super(age, weight, feet, inches). Note what we ve done we greatly simplified the Man class and even though there is no getage() method defined, we can invoke it! Similarly for howmanytwix(), we can invoke that too, although it doesn t give us the right answer (yet). We get access to these methods because they are inherited from the parent, or base class.

If we try to add the howmanytwix() code to the Man class we ll get an error: double kilograms = weight / 2.2; double centimeters = ((feet * 12) + inches) * 2.54; double calories = 10 * kilograms + 6.25 * centimeters 5 * age + 5; double numtwix = calories / 568; return numtwix; There will be a compiler error because the weight, feet, inches, and age variables are all private to the Person class. One way to make them available would be to make public accessor methods, like we did for age. Then we can use getage() when we need to access age. Another way is to make the variables protected. This modifier allows access to the instance variable from within the class and from any derived class, or class in the same package. The variables in the Person class now look like this: public class Person protected int age; protected int weight; protected int feet; protected int inches; The complete Man class follows: public class Man extends Person // Default constructor public Man() super(); // Constructor taking arguments public Man(int age, int weight, int feet, int inches) super(age, weight, feet, inches);

double kilograms = weight / 2.2; double centimeters = ((feet * 12) + inches) * 2.54; double calories = 10 * kilograms + 6.25 * centimeters - 5 * age + 5; double numtwix = calories / 568; return numtwix; Our earlier demo code of: Man kenrick = new Man(40, 140, 5,6); System.out.println("This person of age " + kenrick.getage() + " should eat " + kenrick.howmanytwix() + " twix bars."); Now gives the identical output to before: This person of age 40 should eat 2.62 twix bars. You may have noticed that we define the method howmanytwix(), in both Person and Man. How do we know which is invoked? The rule is that the derived class overrides the base class. That is, the most specific method to a particular object is the one that is invoked. In the above example, the Man class definition of howmanytwix() is invoked, not the Person class (which would return 0). We can define the Woman class similarly to the Man class: public class Woman extends Person // Default constructor public Woman() super(20, 140, 5, 6); // Different defaults for women // Constructor taking arguments public Woman(int age, int weight, int feet, int inches) super(age, weight, feet, inches); double kilograms = weight / 2.2; double centimeters = ((feet * 12) + inches) * 2.54;

double calories = 10 * kilograms + 6.25 * centimeters - 5 * age - 161; double numtwix = calories / 568; return numtwix; Class Exercise: We could make the classes even simpler by abstracting out the shared calorie calculation, how would you do that? The takeaways from this example are: 1. A derived class inherits public (or protected) methods from the base class 2. Derived methods override methods of the same name in the base class 3. Use the keyword super to invoke methods from the base class 4. Private variables are inherited by the derived class, but are not directly accessible by name. Public or protected variables are directly accessible by name. Miscellaneous Gotcha s: Private methods are not inherited by the derived class. Private methods are only available in the class they are defined. It might seem like if we have a chain of methods, we could invoke super.super.method() to invoke the base class of a base class. Unfortunately Java does not allow this, one may only use a single super. Multi-Level Classes In our example we only had one parent and one child class, but we could have a chain of multiple parents. It turns out that every class we make is actually a descendant of the predefined class named Object. This means that every class we create will inherit methods from Object. So what methods are available to something so general? Some of the methods we get automatically are methods equals(), clone() and tostring(). The intent is for these methods to check if another object is equal to the current object, correctly copy an object, and to convert the current object into a String. However, these inherited methods will not work correctly for just about every class you create. This means it is your job to override these methods with your own that work properly.