Abstract Classes and Interfaces

Slides:



Advertisements
Similar presentations
CS1054: Lecture 18 - Inheritance. The DoME example "Database of Multimedia Entertainment" stores details about CDs and videos –CD: title, artist, # tracks,
Advertisements

Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Improving structure with inheritance (Chapters 8 and 9)
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.
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 Chapter 8.
More about inheritance Exploring polymorphism. 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
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.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
More about inheritance Exploring polymorphism 3.0.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
What is inheritance? It is the ability to create a new class from an existing class.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
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.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
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.
1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface.
Improving structure with inheritance 3.0. The media project stores details about CDs and DVDs –CD: title, artist, number of tracks, playing time, got-it,
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object-Oriented Programming: Polymorphism Chapter 10.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting.
Inheritance and Polymorphism
More about inheritance
Lecture 19 - Inheritance (Contd).
Java Programming Language
More about inheritance
Advanced Java Programming
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Comp1202: Inheritance I Super and Sub-classes.
Improving structure with inheritance
Lecture 15 Inheritance.
Presentation transcript:

Abstract Classes and Interfaces Chapter 9

Print in Entertainment ver 1 (from CD class, no inheritance) public void print() { System.out.print("CD: " + title + " (" + playingTime + " mins)"); if(gotIt) { System.out.println("*"); } else { System.out.println(); System.out.println(" " + artist); System.out.println(" tracks:" + numberOfTracks); System.out.println(" " + comment); ver 2 (with inheritance): public void print() { System.out.print("title: " + title + "(" + playingTime + " mins)"); if(gotIt) { System.out.println("*"); } else { System.out.println(); System.out.println(" " + comment); fields not printed in ver 2 {

Print problems Inheritance is one way: CD and Video inherit what is up (if fields/methods are public), but Item cannot inherit DOWN Possible solution: move print method down to CD and Video What do you think will happen? just like draw in Shapes

Print problem Solution #1 CD can't access private fields could make accessor methods. for now, print accessible fields. Database can't access print make print method for Item Compiles. What does it print? Item print method not used

Lessons Problems compile time needs method in statically checked class At run time, method used is from dynamic type Problems prints just kid's fields. still need fields from Item

Solution #2 super.print( ) calls parent's method. Unlike super ( ) constructor: method name is explicitly stated with any parameters can be called anywhere from any method not automatically called

Problem Problem What if we don't want title first Solution: put super.print( ) second Problem What if we want to intermingle Item fields and CD fields Must print some of Item fields, then some of CD fields

Solution: Accessor Methods But should be available just to children Make them protected Why not make fields protected? fields should always be private protected void getTitle( ) { return title; }

Your turn Write protected accessor for fields in Item: public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // methods omitted }

Problem Solution Now, we don't need Item's print method What happens if we delete it? Solution Make print method abstract

Syntax of abstract methods abstract public class MyClass { // regular constructors and methods that are called in a regular way abstract method signature ; //no body. NO {}s }

Abstract Classes Classes can be abstract if they should not have objects instantiating them Classes MUST be abstract if they have any abstract methods or fields methods can be abstract Classes can be abstract without any abstract fields or methods Abstract classes can have non-abstract fields and methods

Why Abstract Methods Abstract Methods require any children to implement that method Compile time error if abstract method not in subclass Allow clients that use the code to compile with the guarantee that that method will be implemented

Syntax of abstract methods abstract public class Item { // no abstract fields private String title; private int playingTime; // rest of fields abstract public void print() ; // contd. next column public Item(String theTitle, int time) { // same constructor. unchanged. // children invoke with super( ) // constructor cannot be used // except by children } // other regular methods

Your turn Write the abstract class Item. Make the print method abstract

Interfaces Chapter 10.5-10.8

Inheritance Review No different than any other class. Has no access to or information about subclasses private in parent is not accessible to children; protected is class SubClass1 extends SuperClass first line of constructor is super( ); Subclasses can have only one parent can use methods from SubClass1 or SuperClass

Inheritance Review Two reasons for inheritance To use the methods of the parent To use polymorphism (e.g. add Item to a library, but each Item is a CD or Video)

Abstract Classes Review Abstract methods enable polymorphism, but have no body Classes with abstract methods must be abstract Abstract classes can not have objects created from them Abstract classes can have useful concrete methods (e.g., setComment) and fields (e.g., Xposition)

Abstract Class Review Two reasons for Abstract Classes Enables polymorphism when methods are not appropriate for superclass (e.g., draw in Shapes or print in Item) Enforces a specification (in order to be a Shape, you must have a draw method)

One more detail: Interfaces Allow multiple inheritance Be an animal AND black (other things are animals and other things are black) Can specify exactly what is needed for a concrete class actionPerformed

Interfaces To implement in interface (parent), put: public interface MyInterface instead of public class MyInterface in class using interface (child), put: public class SubClass1 extends SuperClass implements MyInterface

Two arrows Two arrows

Interfaces concept Just like a class, Like abstract class on speed but no variables (can have static final public fields) no bodies for ANY method Like abstract class on speed Purpose? polymorphism specification

Interface Examples Comparator ActionListener

Abstract Classes vs Interfaces Use interfaces when possible because there can lots of classes following "implements" but only one "extends" Use interfaces if you do not want any fields or methods in your parent class Use abstract classes if you want subclasses to use common defined methods or fields

Problem 10.54 U u; G g; B b; Z z; X x; Look at the code below. You have five types (classes or interfaces) U, G, B, Z, and X, and a variable of each of these types. What can you say about the relationships of the types? U u; G g; B b; Z z; X x; The following assignments are all illegal (they cause compiler errors): u = b; x = g; b = u; z = u; g = x; The following assignments are all legal: u = z; x = b; g = u; x = u;

Wrapper Classes, 8.9.3 Many times an object is wanted instead of a primitive. Use a wrapper class: wraps the primitive around a java.lang wrapper class int i = 20; Integer iwrap = new Integer(i); myCollection.add(iwrap); // added as an Integer int iwrapvalue = iwrap.intValue( 37);