CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice 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,
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.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
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.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Polymorphism & Interfaces
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
What is inheritance? It is the ability to create a new class from an existing class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
CSC 205 Java Programming II
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism and access control
More About Inheritance & Interfaces
Java Inheritance.
Presentation transcript:

CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]

CSC 142 O 2 Overview An assortment of topics related to inheritance Class Object toString instanceof Overloading and overriding Abstract and concrete classes Inheritance vs composition: which to use? Abstract classes vs interfaces

CSC 142 O 3 Inheritance Reviewed A class can be defined as an extension another one Inherits all behavior and state from base (super-) class But only has direct access to public or protected methods/variables Use to factor common behavior/state into classes that can be extended/specialized as needed Useful design technique: find a class that is close to what you want, then extend it and override methods that aren’t quite what you need

CSC 142 O 4 Class Object In Java’s class model, every class directly or indirectly extends Object, even if not explicitly declared class Foo{ … } has the same meaning as class Foo extends Object{ … } Class Object is the root of the class hierarchy contains a small number of methods which every class inherites (often overridden with something more suitable) toString( ), equals( ), clone( ), …

CSC 142 O 5 Aside – toString( ) Most well-designed classes should override toString( ) to return a meaningful description of an instance Rectangle[height: 10; width: 20; x: 140; y: 300] Color[red: 120; green: 60; blue: 240] (BankAccount: owner=Bill Gates, Balance = beyond your imagination) Called automatically whenever the object is used in a context where a String is expected Use with System.out for a crude, surprisingly effective debugging tool System.out.println(unusualBankAccount); System.out.println(suspectRectangle);

CSC 142 O 6 instanceof The expression instanceof is true if the object is an instance of the given class or interface (or any subclass or subinterface of the one given) Use should be rare in well-written code Often overused by inexperienced programmers when method override and dynamic dispatch should be used One common use: checking types of generic objects before casting Object o = aList.get(i); if (o instanceof ThingThatCanJump) { ThingThatCanJump t = (ThingThatCanJump) o; t.jump(veryHigh); …

CSC 142 O 7 Overriding and Overloading In spite of the similar names, these are very different Overriding: Redefinition of a method in a derived (sub-) class Replaces the method that would otherwise be inherited class One { … public void doIt(…) { … } … } class Two extends One { … public void doIt(…) { … } … } Parameter lists must match exactly (number and types) Method called depends on actual (dynamic) type of the object

CSC 142 O 8 Overloading A class may contain multiple definitions for constructors or methods class Many { public Many( ) { … } public Many(int x) { … } public Many(double x, String s) { … } public void another(Many m, String s) { … } public void another(String[ ] names) { … } Known as overloading Parameter lists must differ in number or type of parameters or both Method calls are resolved automatically depending on number and types of arguments – must be a unique best match

CSC 142 O 9 Overriding vs Overloading Overriding Provides an alternative implementation of an inherited method Overloading Provides several implementations of the same method These are completely independent of each other Mixing the two – potentially confusing – avoid! Pitfall: attempt to override a method, but something is slightly different in the parameter list. Result: new method overloads inherited one, doesn’t override; new method doesn’t get called when you expect it

CSC 142 O 10 What is a generic Animal? Example: class Animal (base class for Dog and Cat) What noise should a generic Animal make? Answer: doesn’t really make sense! Purpose of class Animal provide common specification for all Animals provides implementation for some methods intended to be extended, not used directly to create objects

CSC 142 O 11 Abstract Classes Idea: classes or methods may be declared abstract Meaning: meant to be extended; can’t create instances If a class contains an abstract method, it must be declared abstract A class that extends an abstract class can override methods as usual A class that provides implementation for all abstract methods it inherits is said to be concrete If a class inherits an abstract method and doesn’t override it, it is still abstract

CSC 142 O 12 Example: Animals public abstract class Animal {// abstract class // instance variables …. /** Return the noise an animal makes */ public abstract String noise( ) ; } public class Cat extends Animal {// concrete class /** Return the noise a cat makes */ public String noise( ) { return “purrr”; } }

CSC 142 O 13 Using Inheritance Java inheritance limitation: a class can only extend one class Use of inheritance, with or without abstract classes is only appropriate when the classes are related conceptually Never use inheritance just to reuse code from another class Composition is normally appropriate if you want to use code in another class, but the classes are otherwise unrelated class SomeClass { private ArrayList localList;// class used to implement SomeClass // Does not make sense for SomeClass // to extend ArrayList

CSC 142 O 14 Abstract Classes vs Interfaces Both of these specify a type Interface Pure specification, no implementation Abstract class Specification plus, optionally, partial or full default implementation Which to use?

CSC 142 O 15 Interfaces Advantages More flexible than inheritance; does not tie the implementing class to implementation details of base class Classes can implement many interfaces Can make sense for classes that are not related conceptually to implement the same interface (unrelated Things in a simulation, mouse click listeners in a user interface) But … Can’t inherit (reuse) a default implementation

CSC 142 O 16 A Design Strategy These rules of thumb seem to provide a nice balance for designing software that can evolve over time (Might be a bit of overkill for some CSC143 projects) Any major type should be defined in an interface If it makes sense, provide a default implementation of the interface Client code can choose to either extend the default implementation, overriding methods that need to be changed, or implement the complete interface directly We’ll see this frequently when we look at the Java libraries