More About Inheritance & Interfaces

Slides:



Advertisements
Similar presentations
CS 211 Inheritance AAA.
Advertisements

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.
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.
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)
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
©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.
(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.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
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.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
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.
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.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
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
More About Java and Java How to Program By Deitel & Deitel.
Chapter 7- Inheritance.
Inheritance and Polymorphism
CSC 205 Java Programming II
Software Engineering Fall 2005
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Continuing Chapter 11 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Polymorphism 11-Nov-18.
Object Oriented Programming (OOP) LAB # 8
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Extending Classes.
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism and access control
Announcements & Review
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Fundaments of Game Design
Abstract Classes and Interfaces
Overview of C++ Polymorphism
Lecture 10 Concepts of Programming Languages
Topics OOP Review Inheritance Review Abstract Classes
C++ Object Oriented 1.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

More About Inheritance & Interfaces CSC 143 More About Inheritance & Interfaces

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

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

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

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

instanceof The expression <object> instanceof <classOrInterface> 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); …

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

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

Overriding vs Overloading 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

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

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

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”; }

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

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?

Interfaces Advantages But … 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

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