CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Chapter 1 Inheritance University Of Ha’il.
OOP: Inheritance By: Lamiaa Said.
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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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 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 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:
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 343 Davis 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.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
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)
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.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
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 using Java
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 14, Mar 27/28, 2013.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Superclasses and Subclasses in Java Computer Science 3 Gerb Objective: Understand superclass methods and the “this” keyword in Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
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.
Inheritance.
Comp 249 Programming Methodology
Inheritance, Polymorphism, and Interfaces. Oh My
Inherited Classes in Java
Chapter 10: Method Overriding and method Overloading
Method Overriding and method Overloading
More on Creating Classes part 3
Presentation transcript:

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall

Agenda Inheritance – our last relationship! Primitives & control structures

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())

total overriding A subclass can override a definition inherited from superclass How: by providing an alternate definition. public class Foo { private Bar _bar; public void setBar(Bar b) { _bar = b; } public class FooSub extends Foo public void setBar(Bar b) { b.setColor(java.awt.Color.CYAN); }

partial overriding A subclass can add something to a definition inherited from superclass simply first invoking the superclass’ definition, then adding extra code in an augmenting definition of the method in the subclass’ definition: public class Foo { private Bar _bar; public void setBar(Bar b) { _bar = b; } public class FooSub extends Foo public void setBar(Bar b) { super.setBar(b); // let superclass method do: _bar = b; b.setColor(java.awt.Color.CYAN); }

overriding summary total (complete) overriding –a subclass provides an entirely new definition for a method which would otherwise have been inherited from the superclass partial overriding –a subclass provides a definition for a method which would otherwise have been inherited from the superclass, but calls the superclass version via super. inheritance –a subclass does not provide an alternate defintion for a method defined in the superclass, which is inherited.

constructor and method overloading A class can define multiple methods with the same name, as long as they differ in their parameter lists. A class can therefore define multiple constructors (which all MUST share their name), as long as they differ in their parameter lists. Providing multiple method definitions with the same name is called overloading: the name is overloaded with multiple definitions. Selection of correct definition is based on the argument list in a given method call.

overloading vs. overriding overloading: –same name is used for many different method definitions –parameter lists of methods must all be different –all definitions co-exist overriding: –same name is used for a subclass method also defined in a superclass –parameter list must be exactly the same in subclass definition as in superclass definition –subclass definition supplants superclass definition

constructor overriding Constructors can (and should generally) be overridden when extending classes other than Object. A superclass constructor is always called, implicitly if not explicitly. Implicit call is to super(). Use super with argument list to explicitly call one of the superclass’ other constructors. (Cf. use of this to call another constructor in same class.) Call to superclass constructor is ALWAYS the first statement. If not, compiler complains.