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

Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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
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 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.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 / 503 Introduction to Computer Science For Majors I Carl Alphonce 219 Bell Hall.
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.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue 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.
CSE 115 / 503 Introduction to Computer Science For Majors I Carl Alphonce 219 Bell Hall.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSE 115 / 503 Introduction to Computer Science I
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.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
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.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Inheritance in the Java programming language J. W. Rider.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
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.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
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
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
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 Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance and Polymorphism
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
 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.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Inheritance ITI1121 Nour El Kadri.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
MSIS 670 Object-Oriented Software Engineering
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 We will return exams some time next week.

Agenda Today: –Inheritance uses of inheritance overuse of inheritance Coming up: –Inheritance overriding of methods (full, partial, none) –Primitives –More control structures

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 pkg; public class UTA implements TA { private String _name; public UTA(String name) { _name = name; } public String job() { return "teach recitations"; } public String name() { return _name; } package pkg; public class GTA implements TA { private String _name; public GTA(String name) { _name = name; } public String job() { return "grade labs"; } public String name() { return _name; }

Refactored code package pkg; public class UTA extends AbstractTA { public UTA(String name) { super(name); } public String job() { return "teach recitations"; } package pkg; public class GTA extends AbstractTA { public GTA(String name) { super(name); } public String job() { return "grade labs"; } package pkg; public abstract class AbstractTA { private String _name; public AbstractTA(String name) { _name = name; } public abstract String job(); public String name() { return _name; }

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.

is-a can imply a restriction in capabilities

Exhibit ‘A’ (conceptual) is-a hierarchy shape rectangle square rhombus parallelogram polygon ellipse

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)

Exhibit ‘B’, part 1 type hierarchy shape rectangle square rhombus parallelogram polygon ellipse

Exhibit ‘B’, part 2 type hierarchy shape rectangle square ellipse rectangularly-defined shape

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

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

Exhibit ‘C’ shape rectangle square rhombus parallelogram polygon (one way to achieve) code sharing ellipse

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?