Inheritance, part 2: Subclassing COMP 401, Fall 2014 Lecture 8 9/11/2014.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Object Oriented Programming with Java
Abstract Class and Interface
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Sub and superclasses using extends
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program.
OOP Languages: Java vs C++
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
What is inheritance? It is the ability to create a new class from an existing class.
Lecture 8: Object-Oriented Design. 8-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Good object-oriented programming is really.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Inheritance and Access Control CS 162 (Summer 2009)
JAVA METHODS and CONSTRUCTORS. 2 JAVA Classes The class is the fundamental concept in JAVA (and other OOPLs) A class describes some data object(s), and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
A Java program consists of a set of class definitions, optionally grouped into packages. Each class encapsulates state and behavior appropriate to whatever.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
1 COMP9024: Data Structures and Algorithms Week One: Java Programming Language (II) Hui Wu Session 2, 2014
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Lecture 12 Inheritance.
Inheritance and Polymorphism
03/10/14 Chapter 9 Inheritance.
CSC 143 Inheritance.
Java Programming Language
CIS 110: Introduction to computer programming
Presentation transcript:

Inheritance, part 2: Subclassing COMP 401, Fall 2014 Lecture 8 9/11/2014

Motivating Example lec8.ex1.v1 Suppose we’re writing a university management system. Interfaces: – Person get first and last name get/set address – Student add credits get / set status (i.e., freshman, sophomore, junior, senior) – Professor promote get rank (i.e., assistant, associate, full) Classes: – StudentImpl implements Person, Student – ProfessorImpl implements Person, Professor

lec8.ex1.v1 Notes Student and Professor interfaces really should be subinterfaces of Person – Presumably implementing Student or Professor implies also being a Person lec8.ex1.v2

lec8.ex1.v2 Notes Casting no longer necessary – Anything that is a Student is also a Person – Anything that is a Professor is also a Person Notice how StudentImpl and ProfessorImpl implement the Person part of Student and Professor – Essentially the same implementation Private fields for first, last, and address Same definitions for Person methods – When two or more classes implement the same interface in the same way, then subclassing can help.

Extending Classes Declared with “extends” keyword Original class – Parent, parent class, superclass New class – Child, child class, subclass, extended class Subclasses inherit fields and methods from the parent class. – Purpose is to collect common implementation details from related classes into a single parent class. – Define each related class as a subclass that just adds the details that are not in common.

lec8.ex1.v3 Notes Notice parallel with interface structure – PersonImpl implements Person – StudentImpl extends PersonImpl and implements Student which extends Person – ProfessorImpl extends PersonImpl and implements Professor which extends Person Subclass constructor should call superclass constructor using “super” – Must be first line of subclass constructor – Alternatively, can chain to a different constructor that does. – If you don’t, then compiler will implicitly call super() with no arguments.

Subinterface vs. Subclass Extending interface only added behavior to contract. – Since interfaces don’t specify (and don’t care) how contract is fulfilled. Extending class creates a new class that shares internal implementation details of its super class. 7

public class Bicycle { // the Bicycle class has three fields public int cadence; public int gear; public int speed; // the Bicycle class has one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // the Bicycle class has four methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } } public class MountainBike extends Bicycle { // the MountainBike subclass adds one field public int seatHeight; // the MountainBike subclass has one constructor public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { cadence = startCadence; speed = startSpeed; gear = startGear; seatHeight = startHeight; } // the MountainBike subclass adds one method public void setHeight(int newValue) { seatHeight = newValue; } MountainBike m = new MountainBike(10, 15, 0, 1); m.setCadence(20); m.setGear(2); m.applyBreak(5); m.speedUp(8); m.setHeight(12);

9 public class MountainBike { public int cadence; public int gear; public int speed; public int seatHeight; public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } public void setHeight(int newValue) { seatHeight = newValue; } } public class MountainBike extends Bicycle {.... } Extending a class is like writing a new class that has all the same details of the original class plus adding additional stuff specific to subclass

Is-A Relationships for Subclasses Like interfaces, “is a” relationship is transitive up the subclass hierarchy. – A MountainBike object “is a” Bicycle – A StudentImpl “is a” PersonImpl Because you inherit everything your parent provides, by definition you also implement any interfaces your parent implements. – And so on all the way up the hierarchy. 10

Is-A For Subclasses COMP 401 :: Spring class A implements InterA {... } class B extends A implements InterB {... } class C extends B implements InterC {... } Objects of type A, implement interface InterA. A “is a” InterA Objects of type B, implement interface InterB and InterA. B “is a” A B “is a” InterA B “is a” InterB Objects of type C, implement interface InterC, InterB, and InterA. C “is a” A C “is a” B C “is a” InterA C “is a” InterB C “is a” InterC

Object All classes inherit from Object – Top of the class hierarchy. – Since every class must inherit from Object, don’t actually need to specify it. COMP 401 :: Spring public class MyClass {... } So when we say this:We were implicitly doing this: public class MyClass extends Object {... }

Object, cont’d. Because all classes implicitly have Object as a superclass ancestor... – A variable with data type Object can hold anything. But then restricted to just the methods that are defined at the level of Object Public methods that all objects have: – public boolean equals(Object o) – public String toString() COMP 401 :: Spring

Instance Fields Subclass has direct access to public and protected fields/methods of parents class, but not private ones. – Public:Everyone has access Generally not a good idea. Breaks encapsulation. – Private: Only class has access Generally recommended as default. Subclasses, however, also shut out. – Protected: Class and subclasses have access. Like private (i.e., appropriate use of encapsulation) but allows subclasses to directly manipulate these fields. lec8.ex2 COMP 401 :: Spring

Access Modifier Chart ClassPackageSubclassWorld publicYES protectedYES NO no modifierYES NO privateYESNO

The dread pirate null… null is a legal value for any reference type variable. – Indicates a “lack” of value (i.e., points nowhere) Attempting to use a null reference, however, will result in program error Upshot: if a reference could possibly be null, you need to check it before using it. – This might happen with methods that return null as a possibility. – Can see this in kmp.a2testers as an example

Midterm Logistics Tuesday 9/23 Closed book – No need for exam book or scantron We will end up split across two rooms – Details TBD Learning accommodations – Let me know if this applies. – Take the test at learning services. – Drop it off at my office.

Midterm Part 1: Multiple choice, true/false – Basic Java language syntax – OO concepts – More than one answer may be correct. Think of it as a group of related true/false questions. – Example: Which of these is NOT a valid built-in Java data type? – a) int – b) unsigned short – c) char – d) double – e) true

Midterm Part 2: Short answer / calculation – Evaluate small snippets of code Example: – What is the value of the variable bar after the following code executes? int bar; int[] a = {1, 2, 3, 4, 5}; int[] b = a; b[2] = a[1]; bar = a[2];

Midterm Part III: Understanding Code I’ll provide the code for one or more classes and then ask questions like: – Identify all of the instance fields of class A. – Identify all of the class methods of class A. – What is the return type of method m of class B?

Midterm Part IV: Writing Code Asked to define one or more simple classes according to some specification. – May be provided with a partial implementation that you have to complete.

Topics / Concepts Basic Java syntax – Variable names, built-in data types Reference types vs. value types Arrays Concepts of abstraction and encapsulation JavaBeans conventions Interfaces – Including extending interfaces covered today – Is-A relationship with interfaces Enumerations Constructor Chaining / Method Overloading Subclassing / Method Overriding