Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Similar documents
We are on the GUI fast track path

Chapter 9 Abstract Classes and Interfaces

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Chapter 13 Abstract Classes and Interfaces

Abstract Classes Interfaces

Chapter 13 Abstract Classes and Interfaces

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Chapter 13 Abstract Classes and Interfaces

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Abstract Classes and Interfaces. CSE 114, Computer Science 1 Stony Brook University

Lecture Notes Chapter #9_b Inheritance & Polymorphism

CS171:Introduction to Computer Science II. Li Xiong

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

CS1150 Principles of Computer Science Objects and Classes

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Motivations. Chapter 13 Abstract Classes and Interfaces

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Inheritance and Polymorphism

Chapter 14 Abstract Classes and Interfaces

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

What is an interface? Why is an interface useful?

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

C20a: Selected Interfaces in Java API

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Chapter 11 Inheritance and Polymorphism

Chapter 11 Inheritance and Polymorphism

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

ITI Introduction to Computing II

ITI Introduction to Computing II

PIC 20A Number, Autoboxing, and Unboxing

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

25. Generic Programming

CS-202 Introduction to Object Oriented Programming

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Polymorphism and Interfaces. CGS 3416 Spring 2018

Inheritance (continued) Inheritance

Chapter 11 Inheritance and Polymorphism

Introduction Programming Using Python Lecture 8. Dr. Zhang COSC 1437 Fall 2017 Nov 30, 2017

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Java Classes, Objects, Inheritance, Abstract and Interfaces Recap

JAVA WRAPPER CLASSES

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Introduction to Inheritance

Abstract Classes and Interfaces

COMP 250. inheritance (cont.) interfaces abstract classes

For this section, we will implement a class with only non-static features, that represents a rectangle

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

CSE 143 Lecture 20. Circle

Object Oriented Programming

Generics. CSE260, Computer Science B: Honors Stony Brook University

Chapter 6: Inheritance

Interfaces. James Brucker

COP 3330 Final Exam Review

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Classes and Inheritance Extending Classes, Chapter 5.2

Lesson11-Inheritance-Abstract-Classes. The GeometricObject case

Class, Variable, Constructor, Object, Method Questions

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CH. 2 OBJECT-ORIENTED PROGRAMMING

ITI Introduction to Computing II

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

1 Shyam sir JAVA Notes

ITI Introduction to Computing II

Inheritance and Interfaces

ITI Introduction to Computing II

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

CS 162, Lecture 25: Exam II Review. 30 May 2018

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Programming overview

Making New instances of Classes

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

C18a: Abstract Class and Method

CS Week 13. Jim Williams, PhD

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

index.pdf January 21,

Programming 2. Inheritance & Polymorphism

Computer Science II (20073) Week 1: Review and Inheritance

Transcription:

Lecture Notes Chapter #9_c Abstract Classes & Interfaces Abstract Classes parent class child class more abstract more concrete, i.e., less abstract abstract class o class with an abstract modifier o class containing abstract methods o cannot create instances, i.e., objects, with the new operator Listing 11.1 GeometricObject.java public abstract class GeometricObject private String color = white ; private boolean filled; private java.util.date datecreated; geometricobject constructor can only be used by subclasses protected geometricobject( ) datecreated = new java.util.date( ); public String getcolor( ) return color ; public void setcolor(string color) this.color = color; public boolean isfilled( ) return filled; public void setfilled( boolean filled ) this.filled = filled; public java.util.date getdatecreated( ) return datecreated; public String tostring( ) return date created: + datecreated + \ncolor: + color + filled: + filled; // Abstract Methods public abstract double getarea( ); public abstract double getperimeter( ); method signatures without bodies

italics abstract GeometricObject -color: String (object) -fllled: boolean (color) -datecreated: java.util.date # protected italics abstract #GeometricObject( ) +getcolor( ): String +setcolor(color: String ): void +isfilled( ): boolean +setfilled(filled: boolean): void +getdatecreated( ): java.util.date +tostring( ): String +getarea( ): double +getperimeter( ): double -radius: double Circle +Circle( ) +Circle(radius: double) +getradius( ): double +setradius(radius: double): void +getparimeter( ): double +printcircle( ): void -width: double -height: double Rectangle1 +Rectangle( ) +Rectangle(width: double, height: double) +getwidth( ): double +setwidth(width: double): void +getheight( ): double +setheight(height: double): void Listing 11.4 TestGeometricObject.java public class TestGeometricObject public static void main(string [ ] args) GeometricObject geoobject1 = new Circle (5); GeometricObject geoobject2 = new Rectangle (5, 3); System.out.println( Object 1 ); displaygeometricobject(geoobject1); System.out.println( and Object 2 ); displaygeometricobject(geoobject2); System.out.println( have the same area: + equalarea(geoobject1, geoobject2)); public static boolean equalarea(geometricobject object1, GeometricObject object2) return object1.getarea( ) == object2.getarea( ); public static void displaygeometricobject(geometricobject object) System.out.println( Area: + object.getarea( ) + \tperimeter: + object.getperimeter( )); Remarks equalarea & displaygeometricobject methods are possible since the abstract class GeometricObject contains the abstract methods getarea and getperimeter

abstract methods cannot be contained in non-abstract classes if a subclass of an abstract class does not implement all the abstract methods, that subclass must be declared to be abstract abstract methods cannot be static methods abstract classes cannot be instantiated by the new operator, but it can be invoked by the creation of an instantiation of a subclass object, hence it should be provided with constructors classes that contain abstract methods are abstract it is possible to declare a class which contains no abstract methods to be abstract a concrete superclass may have abstract subclasses, e.g., the Object class is concrete but all other classes, i.e., abstract classes, are subclasses of the Object class a subclass may override a concrete method from its superclass and declare it to be abstract; in this case the subclass must be declared to be abstract an abstract class can be used as a data type, e.g., GeometricObject [ ] objects = new GeometricObject [10]; Liang pages 366-368 java.util.date( ) is a concrete method that represents a specific instance in time java.util.calendar is an abstract base class which contains methods for extracting detailed calendar information, e.g., year, month, etc. java.util.gregoriancalendar is a concrete class which implements the abstract methods inherited from the abstract base class java.util.calendar java.lang.number Abstract Number class superclass for numeric wrapper classes Double Short bytevalue( ) floatvalue( ) Float Byte shortvalue( ) doublevalue( ) Long BigInteger intvalue( ) Integer BigDecimal longvalue( )

Interfaces class-like construct that contains only constants and abstract methods used to specify common behaviors for objects each interface is compiled into a separate ByteCode file interface inheritance multiple interfaces possible public interface Edible public abstract String howtoeat( ); abstract method Listing 11.6 TestEdible.java public class TestEdible public static void main(string [ ] args) Object[ ] objects = new tiger( ), new Chicken( ), new Apple( ) ; for ( int i = 0; i < objects.length; i++) if ( objects[ i ] instanceof edible ) System.out.println(((Edible)objects[ i ]).howtoeat( )); class Animal class Chicken extends Animal implements Edible public String howtoeat( ) return Chicken: Fry it ; class Tiger extends Animal Fruit does not implement howtoeat method; thus it must be declared to be abstract abstract class Fruit implements Edible class Apple extends Fruit public String howtoeat( ) return Apple: Make Apple Cider ; Concrete classes Apple & Orange need to implement the howtoeat method class Orange extends Fruit public String howtoeat( ) return Orange: Make Orange Juice ; Remark: Since all data fields specified in interfaces are public final static all methods specified in interfaces are public abstract Java allows these modifiers to be omitted

public interface T public static final int K = 1; public abstract void p( ); public interface T int K = 1; void p( ); The constant K defined in the interface T can be accessed by T.K Comparable Interface interface Comparable is defined in java.lang, e.g., package java.lang; public interface Comparable public int compareto(object o); compareto( ) determines the order of this object with the specified object and returns a negative integer, zero, or a positive integer depending upon the rank order of the objects Remark: Java Library objects often implement the Comparable interface to define a natural order for the objects, e.g., public class String extends Object implements Comparable public class Date extends Object implements Comparable Generic Max Methods public class Max public static Comparable max(comparable o1, Comparable o2) if(o1.compareto(o2) > 0) preferable return o1; else return o2; simpler, more robust restricted to Comparable objects compiler detects violations public class Max public static Object max(object o1, Object o2) if(((comparable)o1).compareto(o2) > 0) return o1; else return o2; complex, less robust method may be invoked by objects that have not implemented the Comparable interface hence the compiler does not detect violations; violations are detected at runtime producing the ClassCastException

Listing 11.7 ComparableRectangle.java public class ComparableRectangle extends Rectangle implements Comparable public class ComparableRectangle(double width, double height) super(width, height); public int compareto(object o) if (getarea( ) > ((CompareRectangle)o).getArea( )) return 1; else if (getarea( ) < ((CompareRectangle)o).getArea( )) return -1; else return 0; Geometric Object Rectangle <<interface>> java.lang.comparable +compareto(o: Object): int ComparableRectangle Remark: Object class contains the equals method Comparable interface provides the compareto method Strong Recommendation: the implementation of the compareto method should be consistent with the equals method,i.e., o1.compareto(o2) == 0 o1.equals(o2) is true

Cloneable Interface package java.lang; public interface Cloneable Remarks: marker interface empty interface method has an empty body objects created from classes that implement the Cloneable interface can be copied by the clone( ) method Java Library objects often implement the Cloneable interface, e.g., Date, Calendar, ArrayList Calendar calendar = new GregorianCalendar(2003, n2, 1); Calendar calendarcopy = (calendar)calendar.clone( ); calendar == calendarcopy false calendar.equals(calendarcopy) true two different objects with identical contents Implementing the Cloneable Interface public class House implements Cloneable, Comparable private int id; private double area; private java.util.date whenbuilt; public House(int id, double area) this.id = id; this.area = area; whenbuilt = new java.util.date( ); public double getid( ) return id; public double getarea( ) return area; public java.lang.date getwhenbuilt( ) return whenbuilt; public Object clone( ) throws CloneNotSupportedException return super.clone( ); public int compareto(object o) if (area > ((House)o).area) return 1; else if (area < ((House)o).area) return -1; else return 0; Override the protected clone method defined in the Object class, i.e., protected native Object clone( ) throws CloneNotSupportedException native implemented in the native platform, i.e., not written in Java protected restricts method to same package or subclass access override changes the visibility to public

Shallow Copy House house1 = new House(1, 1750.50); House house2 = (House)house1.clone( ); clone( ) copies the data value of each field in the original object to the equivalent field in the target object house1: House House2: House id 1 id 1 area 1750.50 area 1750.50 whenbuilt whenbuilt: Date date object contents whenbuilt Deep Copy override the clone method with custom cloning operations after the super.clone( ) constructor has been invoked Variables Constructors Methods Abstract Class no restrictions invoked by subclasses via constructor chaining; cannot be invoked directly via the new operator no restrictions Interface public static final variables do not exist; cannot be instantiated via the new operator public abstract instance methods public class NewClass extends BaseClass implements Interface1, Interface2,, InterfaceN SubInterfaces public interface NewInterface extends Interface1, Interface2,, InterfaceN Class Names -- Nouns Interface Names -- Nouns or Adjectives