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.

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

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.
LECTURE 7: INHERITANCE CSC 212 – Data Structures.
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,
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.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 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.
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.
LECTURE 07 Programming using C# Inheritance
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Intro to OOP with Java, C. Thomas Wu
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
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.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Programming in Java CSCI-2220 Object Oriented Programming.
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?
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.
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.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
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 and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
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.
Inheritance ndex.html ndex.htmland “Java.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Lecture 6: Composition and Inheritance CS202 Fall 2013.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Programming in Java: lecture 7
Modern Programming Tools And Techniques-I
Class Inheritance Part I
OOP: Encapsulation &Abstraction
Lecture 12 Inheritance.
Lecture 7: Reusing Code & Interfaces
Java Inheritance.
Chapter 5 Hierarchies IS-A associations superclasses subclasses
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
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
CS 240 – Advanced Programming Concepts
Presentation transcript:

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 make the working escalator go up. Why?

Sharing Among Classes

Composition has-a  Used when there is “has-a” relationship has a  Student has a name has a  Full name has a first name has an  Car has an engine has an  Rectangle has an upper right vertex  Use a field to compose classes  So we would add name field to Student class  firstName field in FullName class  If must use data externally, add getters & setters

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); }

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  “Is-a” relationships implemented via inheritance is a  Automobile is a Vehicle is a  Car is a Vehicle is an  Truck is an Automobile is an  Car is an Automobile extends  Starts with superclass which subclass extends extends  Automobile extends Vehicle extends  Truck extends Automobile extends  Car extends Automobile (& Vehicle ?)

extends extends Example extends extends extends public class Vehicle {…} public class Automobile extends Vehicle {…} public class Car extends Automobile {…} public class Truck extends Automobile {…} VehicleAutomobileCarTruck

extends extends Example  Each class extends exactly one class  extends  extends explicitly specifies superclass  If not stated, class defaults to subclass of Object VehicleAutomobileCarTruck

extends extends Example  Each class extends exactly one class  extends  extends explicitly specifies superclass  If not stated, class defaults to subclass of Object ObjectVehicleAutomobileCarTruck

extends extends Example  Extended by as many classes as heart desires  Automobile superclass of Car, Truck  Vehicle superclass of Automobile, Car, Truck ObjectVehicleAutomobileCarTruck

Superclass (& Subclass)  Every class is subclass of:  superclass  superclass’s superclass  superclass’s superclass’s superclass  superclass’s superclass’s superclass’s superclass, …  Object is-ais-a  Truck is-a Automobile is-a Vehicle is-a  So Truck is-a Object also

What Gets Inherited And How?  Class inherits all members from superclass  Subclass can use them directly unless they are private  Use as if they were copied into class w/o copying

Inheritance Example public class Sailor { private String str = "Aye, aye"; public String getMyString() { return "Capt."; } } public class Pirate extends Sailor { public String getFullString() { return “not ” + getMyString(); } public Pirate() { str = "Yarr"; } public static void main(String[] args) { Pirate sub = new Pirate(); Sailor ship = new Sailor(); System.out.println(sub.getMyString()); System.out.println(ship.getMyString()); System.out.println(sub.getFullString()); System.out.println(sub.str); System.out.println(ship.str);

Inheritance Example public class Sailor { private String str = "Aye, aye"; public String getMyString() { return "Capt."; } } public class Pirate extends Sailor { public String getFullString() { return “not ” + getMyString(); } public Pirate() { str = "Yarr"; } public static void main(String[] args) { Pirate sub = new Pirate(); Sailor ship = new Sailor(); System.out.println(sub.getMyString()); System.out.println(ship.getMyString()); System.out.println(sub.getFullString()); System.out.println(sub.str); System.out.println(ship.str);

Inheritance Example public class Sailor { protected String str = "Aye, aye"; public String getMyString() { return "Capt."; } } public class Pirate extends Sailor { public String getFullString() { return “not ” + getMyString(); } public Pirate() { str = "Yarr"; } public static void main(String[] args) { Pirate sub = new Pirate(); Sailor ship = new Sailor(); System.out.println(sub.getMyString()); System.out.println(ship.getMyString()); System.out.println(sub.getFullString()); System.out.println(sub.str); System.out.println(ship.str);

What Gets Inherited And How?

Overriding Methods  Subclass can redeclare inherited method  Overloaded when different signature used  Method is called overridden if signatures identical  Specialize method execution in subclass w/this  Changed only for subclass & its subclasses  Original used by superclass & other classes  Actual method called determined by instance’s type

Overriding Methods  Call overriden method as defined in superclass  Only in subclass overriding the method  If method is not overriden then this is not needed  super  super.methodName(…) used to call method supersuper  No multiples: super.super.methodName(…)  Cannot make less accessible when overriding  However, method can make more accessible

Overriding Methods  Call overriden method as defined in superclass  Only in subclass overriding the method  If method is not overriden then this is not needed  super  super.methodName(…) used to call method supersuper  No multiples: super.super.methodName(…)  Cannot make less accessible when overriding  However, method can make more accessible

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

Your Turn  Get into your groups and complete activity

For Next Lecture  1 st quiz in class on Monday  Covers everything from first 3 weeks of school  Focus will be on objects, fields, methods, types,…  Problems like on activities, weekly assignments, more  There is weekly assignment problem on Angel  Due by 5PM Tuesday (via Assignment Submitter)  Both problems graded using provided JUnit tests