CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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
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
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 Office hours: M-F 11:00-11:
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 Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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 Office hours: M-F 11:00-11:
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,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
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.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance and Polymorphism CS351 – Programming Paradigms.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
CSSE501 Object-Oriented Development
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance in the Java programming language J. W. Rider.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Object Oriented Programming
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.
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance and Polymorphism
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Advanced Programming in Java
Inheritance ITI1121 Nour El Kadri.
Objects as a programming concept
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Advanced Programming Behnam Hatami Fall 2017.
Final and Abstract Classes
Presentation transcript:

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:

Announcements Exam 3 on Wednesday 11/10 –covers material from last exam up to and including today’s material Exam review in lecture on Monday 11/08 –study over the weekend, and bring questions to class on Monday Sample questions on web-site over the weekend

Agenda Inheritance introduction –quick review from last class Inheritance details –object construction –super –constructor chaining

Last time We saw two classes extending from a common superclass. Consequences: –polymorphism (type hierarachy) –reduced code duplication (inheritance)

Code duplication a “code smell” package noninheritance; public class Cat implements Noisy { private String _myName; public Cat(String n) { _myName = n; } public String getName() { return _myName; public String sound() { return "meow"; } package noninheritance; public class Dog implements Noisy { private String _myName; public Dog(String n) { _myName = n; } public String getName() { return _myName; public String sound() { return ”ruff"; }

Refactored code package inheritance; public class Cat extends Noisy { public Cat(String n) { super(n); public String sound() { return "meow"; } package inheritance; public class Dog extends Noisy { public Dog(String n) { super(n); public String sound() { return ”ruff"; } package inheritance; public abstract class Noisy { private String _myName; public Noisy(String name) { _myName = name; } public abstract String sound(); public String getName() { return _myName; }

Flavors of inheritance Inheritance is used for different purposes: –to indicate conceptual relatedness (is-a) –for subtyping (extension of capabilities) –for code sharing Inheritance can take place from –interface to interface –class to class

Inheritance conceptual relatedness (is-a) The is-a relationship exists between two classes A and B if each member of A is also a B. Example 1: since every square is also a rectangle, we say square is-a rectangle. Example 2: since every penguin is also a bird, we say penguin is-a bird.

Inheritance supertype-subtype A subtype has at least as many capabilities as the supertype has: –Java uses the “extends” keyword The compiler uses typing information to ensure that method calls made on expressions of a given type are coherent. “extends” implies an augmentation of capabilities

types (continued) rectangle –vary width and height independently of each other –independent setWidth and setHeight mutators. square must not allow this –width and height are related –setSize mutator only square and rectangle are NOT the same type. rectangle and ellipse might be (!)

Rectangles & Ellipses (x,y) width (w) height (h) (x+w,y+h) (x,y) width (w) height (h) (x+w,y+h) The dimensions of rectangles and ellipses are specified in the same manner.

types (continued) square is-a rectangle conceptually square should not inherit from rectangle (rectangle methods are not a subset of square methods)

Inheritance: sharing implemenation A third use of inheritance is for sharing of implementation (code). This use of inheritance is over-used: composition is often preferable. Advice: –never use inheritance SOLELY for the purpose of sharing implementation –OK if coupled with is-a or typing motivation

Is-a hierarchy (conceptual) is-a hierarchy shape rectangle square rhombus parallelogram polygon ellipse

subtyping: extension of capabilities type hierarchy shape rectangle square rhombus parallelogram polygon ellipse

subtyping: extension of capabilities type hierarchy shape rectangle square ellipse rectangularly-defined shape

Possible code sharing shape rectangle square rhombus parallelogram polygon (one way to achieve) code sharing ellipse

lesson There is no single “right” hierarchy. Arrange hierarchy so it works for your problem/solution.

interface inheritance In Java, an interface can extend zero or more interfaces: extending zero interfaces public interface A { public void foo(); } extending one interface public interface B extends A { public void bar(A a); } extending many interfaces public interface C extends B, ActionListener { public ActionEvent getEvent(); }

implements and extends clauses a class can implement an arbitrary number of interfaces a (user-defined) class extends exactly one class –Object by default

class (implementation) inheritance A (user-defined) class always extends exactly one (other) class: public class Circle extends Shape {…} If class header has no explicit extends clause, parent class is implicitly Object: public class Shape {…} public class Shape extends Object {…} Object is the root of Java’s class hierarchy.

implements clause a class can implement an arbitrary number of interfaces type hierarchy vs. class hierarchy –class: single root – Object –type: many roots all instantiable types fall under Object  all objects are of type Object

interface inheritance, cont’d When an interface A extends another interface B, A inherits the methods specified by B. This means that a class which implements A must define all the methods specified in both A and B. If an interface can have at most one specification for any given method: even if an interface inherits the very same method specification (same name, same parameter list) from two or more parent interfaces, the interface has the method specified just once.

What is inherited? Given what we know, a correct answer is that anything that is not private is inherited. All our properties (instance variables) are private, so they are not inherited. All our methods are public (not private), so they are inherited.

What is effect of inheritance? A method inherited from a superclass to a subclass can be invoked on a subclass instance, even though not defined there: public class Foo { private Bar _bar; public void setBar(Bar b) { _bar = b; } public class FooSub extends Foo { … } This is legal: new FooSub().setBar(new Bar())

multiple inheritance Some languages allow multiple (implementation) inheritance (e.g. C++) Java does not (but Java has interfaces) Issue: –if the same method, defined in different ways, is inherited from different ancestors, which implementation has priority?