LECTURE 7: INHERITANCE CSC 212 – Data Structures.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Chapter 1 Inheritance University Of Ha’il.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
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
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
(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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
Object-Oriented Design CSC 212. Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing.
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Computer Science I Inheritance Professor Evan Korth New York University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
LECTURE 07 Programming using C# Inheritance
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Intro to OOP with Java, C. Thomas Wu
CSC 212 – Data Structures Lecture 12: Java Review.
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.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CSC 142 Computer Science II Zhen Jiang West Chester University
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
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.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
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 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.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
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.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Lecture 6: Composition and Inheritance CS202 Fall 2013.
OOP: Encapsulation &Abstraction
Lecture 12 Inheritance.
Lecture 7: Reusing Code & Interfaces
Inheritance and Polymorphism
Java Inheritance.
Week 4 Object-Oriented Programming (1): Inheritance
Lecture 8: Polymorphism & Class Design
Road Map Inheritance Class hierarchy Overriding methods Constructors
ATS Application Programming: Java Programming
Chapter 9 Inheritance and Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Inherited Classes in Java
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
CS 240 – Advanced Programming Concepts
Presentation transcript:

LECTURE 7: INHERITANCE CSC 212 – Data Structures

Sharing Among Classes  Classes often share actions & data  Writing code once is laziest option  Cutting-and-pasting still requires effort  Increase bugs with multiple copies of code  Two ways to not rewrite code Composition & Inheritance

Composition  Used when there is “has-a” relationship  Student has a name  FullName has a firstName  Car has an engine  Rectangle has a upperRightVertex  Use a field to compose classes  So name would be field in Student class  firstName would be field in FullName class…  Use getters & setters to access field’s data

Composition Example public class FullName { private String firstName; private String lastName; // constructor, getters & setters also here } public class Student { private FullName name; // Brevity is the soul of wit- Shakespeare public String getFirstName() { return name.getFirstName(); } public void setFirstName(String newName) { name.setFirstName(newName); }

Inheritance  “Is-a” relationships implemented via inheritance  Automobile is a Vehicle  Car is an Automobile  Truck is an Automobile  Car is a Vehicle  Starts with superclass which subclass extends  Automobile extends Vehicle  Car extends Automobile  Truck extends Automobile  Car extends Vehicle

extends Keyword  Java example of inheritence public class Vehicle {…} public class Wagon extends Vehicle {…} public class Automobile extends Vehicle {…} public class Truck extends Automobile {…} VehicleAutomobileTruckWagon

extends Keyword  Each class extends exactly one class  extends explicitly specifies the superclass  Otherwise, defaults to subclass of Object  Can be extended by multiple classes  Vehicle superclass of Automobile & Wagon  Automobile superclass of Truck  So, Vehicle also superclass of Truck VehicleAutomobileTruckWagon

Subclass-Superclass  Every class is subclass of:  superclass,  superclass’s superclass  superclass’s superclass’s superclass  superclass’s superclass’s superclass superclass, …  Object  Truck is-a Automobile which is-a Vehicle  So Truck is-a Vehicle also  Superclass variables CAN refer to subclass instances  But subclass CANNOT refer to superclass instances

What Gets Inherited And How?  Inherit all fields & methods from superclass  Subclass can use them unless they are private  Inheritance is automatic – DON’T RETYPE IN SUBCLASS  Redeclaring field or method leads to bad things

Inheritance Example public class SuperClass { protected String str = “PARENT”; public String getMyString() { return “SUPER”; } } public class SubClass extends SuperClass { public String getFullString() { String s = getMyString(); return “sub” + s; } public SubClass() { str = “kid”; } public static void main(String[] args) { SubClass sub = new SubClass(); SuperClass super = new SuperClass(); System.out.println(sub.getMyString()); System.out.println(super.getMyString()); System.out.println(sub.getFullString()); System.out.println(sub.str); System.out.println(super.str); super = sub;

Chaining Constructors  Often want to reuse superclass’s constructor  Already initializes fields in superclass  Unfortunately, constructors not inherited  Maximize laziness: refuse to lose (the constructor)  May also want multiple constructors in class  Often do similar work initializing fields  Still want to avoid copying code  Increase laziness through chaining

Chains Explained  Constructors chained by this() or super()  Must be first command in constructor  Call into same class using this()  super() calls superclass constructor  Executes just like a normal method call  Must match types listed for the parameters  Call cannot violate access protection (e.g., private )  Constructors are not inherited  Still need to write constructors  Copying code limited by doing this, however

Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }

Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public CSticker(String typ) { this(“black”, typ); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }

Stickers public class Sticker { private String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public CSticker(String typ) { this(“black”, typ); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }

Stickers public class Sticker { private String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { super(type); color = clr; } public CSticker(String typ) { this(“black”, typ); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }

Overriding Methods  Subclass can reuse method names from superclass  Overloaded when different signature used  When signatures identical its overridden  Subclass can modify exiting methods in this way  Changed only for subclass and any of its subclasses  Original used by superclass & other classes  Method definition used determined by instance’s type

Overriding Methods  Can call overriden method as defined in superclass  Only in subclass overriding the method  Call using super.methodName  super.super.methodName illegal (only 1 super legal)  Overriden method cannot become less accessible  Can make more accessible, however

Overriding Example public class SuperClass { public String getMyString() {return “SUPER”;} public SuperClass() { } } public class SubClass extends SuperClass { public String getMyString() {return “sub”;} public SubClass() { } public static void main(String[] args) { SubClass sub = new SubClass(); SuperClass super = sub; System.out.println(sub.getMyString()); System.out.println(super.getMyString()); super = new SuperClass(); System.out.println(super.getMyString());

For Next Lecture  Project #1 has been released  Week #3 available on Web/Angel  Do not let it wait, stop procrastinating  Class moving into new material  Java review is now over; get help if you need more  I have a cool office with a full candy jar  Come by & ask questions while getting sugar fix