JAVA Objects & The Comparable Interface The MyComparable Interface. The MyComparable Interface. The AbstractObject class. The AbstractObject class. Wrapper.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Computer Science 209 Software Development Equality and Comparisons.
AP Computer Science TOPICS TO DISCUSS.equals() == instanceof operator compareTo Interfaces Abstract Classes.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 A_Object I_Comparable App_B Klassehierarki I_Container A_Container I_Stack I_Queue I_Graph I_Tree I_SearchableContainer I_OrderedList I_SortedList I_HashTable.
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
Inheritance Inheritance Reserved word protected Reserved word super
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Enumeration Pattern.
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,
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Hashing Techniques: Implementation Implementing Hash Functions Implementing Hash Tables Implementing Chained Hash Tables Implementing Open Hash Tables.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
1 Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Iterator Pattern.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern.
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
 The general subject of comparing objects, or object references, can be introduced concretely with strings.  Recall that String is a class, and so strings.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces and Inner Classes
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
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.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Object-Oriented Programming: Polymorphism Chapter 10.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
More About Java and Java How to Program By Deitel & Deitel.
Inheritance ITI1121 Nour El Kadri.
Object-oriented Programming in Java
null, true, and false are also reserved.
Introduction to Java Programming
Announcements & Review
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chap 2. Identifiers, Keywords, and Types
Chapter 13 Abstract Classes and Interfaces Part 01
Agenda Types and identifiers Practice Assignment Keywords in Java
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

JAVA Objects & The Comparable Interface The MyComparable Interface. The MyComparable Interface. The AbstractObject class. The AbstractObject class. Wrapper classes. Wrapper classes. The Chr class.The Chr class. The Int class.The Int class. The Dbl class.The Dbl class. The Str class.The Str class. The class Hierarchy. The class Hierarchy. Review Questions. Review Questions.

The MyComparable Interface Being able to compare objects is very important in the study of data structures. Being able to compare objects is very important in the study of data structures. Java has a comparable interface but it is very limited - it only has the compareTo method. Java has a comparable interface but it is very limited - it only has the compareTo method. Thus, we introduce our comparable interface as follows; and most of the data structure classes we shall develope in this course will implement this interface. Thus, we introduce our comparable interface as follows; and most of the data structure classes we shall develope in this course will implement this interface. 1 public interface MyComparable { 2 boolean isLT (MyComparable object); 3 boolean isLE (MyComparable object); 4 boolean isGT (MyComparable object); 5 boolean isGE (MyComparable object); 6 boolean isEQ (MyComparable object); 7 boolean isNE (MyComparable object); 8 int compare (MyComparable object); 9 }

The AbstractObject class All Java classes are derived from the base class Object. All Java classes are derived from the base class Object. The Object class has the equals method that allows objects to be tested for equality, but we need to be able to do more than just test for equality. The Object class has the equals method that allows objects to be tested for equality, but we need to be able to do more than just test for equality. Thus, we define our own base class that implements our comparable interface. Thus, we define our own base class that implements our comparable interface. 1 public abstract class AbstractObject implements MyComparable 2 { 3 public final boolean isLT (MyComparable object) 4 { return compare (object) < 0; } 5 6 public final boolean isLE (MyComparable object) 7 { return compare (object) <= 0; } 8 9 public final boolean isGT (MyComparable object) 10 { return compare (object) > 0; } 11 public final boolean isEQ (MyComparable object) 12 { return compare (object) == 0; } public final boolean isNE (MyComparable object) 15 { return compare (object) != 0; }

The AbstractObject class (contd.) public final boolean equals (Object object) { 18 if (object instanceof MyComparable) 19 return isEQ ((MyComparable) object); 20 else 21 return false; 22 } public final int compare (MyComparable arg) { 25 if (getClass () == arg.getClass ()) 26 return compareTo (arg); 27 else 28 return getClass ().getName ().compareTo ( 29 arg.getClass ().getName ()); 30 } 31 protected abstract int compareTo (MyComparable arg); 32 }

Wrapper classes Just as we could not rely on Java's Object class, we also Just as we could not rely on Java's Object class, we also cannot rely on Java's wrapper classes - not MyComparable. cannot rely on Java's wrapper classes - not MyComparable. Moreover, all the wrapper classes were defined as final, thus, we cannot extend them to make them MyComparable. Moreover, all the wrapper classes were defined as final, thus, we cannot extend them to make them MyComparable. Therefore, we have to define our own wrapper classes. Therefore, we have to define our own wrapper classes. 1 public class Chr extends AbstractObject { 2 protected char value; 3 4 public Chr (char value) { 5 this.value = value; 6 } 7 public char charValue () { 8 return value; 9 } 10 protected int compareTo (MyComparable object) { 11 Chr arg = (Chr) object; 12 return (int) value - (int) arg.value; 13 } 14 }

Wrapper classes (contd.) 1 public class Int extends AbstractObject { 2 protected int value; 3 4 public Int (int value) { 5 this.value = value; 6 } 7 public int intValue () { 8 return value; 9 } 10 protected int compareTo (MyComparable object) { 11 Int arg = (Int) object; 12 if (value < arg.value) 13 return -1; 14 else if (value > arg.value) 15 return +1; 16 else 17 return 0; 18 } 19 }

Wrapper classes (contd.) 1 public class Dbl extends AbstractObject { 2 protected double value; 3 4 public Dbl (double value) { 5 this.value = value; 6 } 7 public double doubleValue () { 8 return value; 9 } 10 protected int compareTo (MyComparable object) { 11 Dbl arg = (Dbl) object; 12 if (value < arg.value) 13 return -1; 14 else if (value > arg.value) 15 return +1; 16 else 17 return 0; 18 } 19 }

Wrapper classes (contd.) 1 public class Str extends AbstractObject { 2 protected String value; 3 4 public Str (String value) { 5 this.value = value; 6 } 7 public String stringValue () { 8 return value; 9 } 10 protected int compareTo (MyComparable object) { 11 Str arg = (Str) object; 12 return value.compareTo (arg.value); 13 } 14 } Implementation of the rest of the wrapper classes are left Implementation of the rest of the wrapper classes are left as exercises. as exercises.

Summary We have introduced MyComparable interface, the AbstractObject class and our wrapper classes. We have introduced MyComparable interface, the AbstractObject class and our wrapper classes. The class hierarchy so far is as shown in the figure below. The class hierarchy so far is as shown in the figure below. The conventions used in drawing the tree are: rounded rectangle is for interfaces; filled rectangle for abstract classes and unfilled rectangle is used for concrete classes. The conventions used in drawing the tree are: rounded rectangle is for interfaces; filled rectangle for abstract classes and unfilled rectangle is used for concrete classes. Dotted lines are for implements and solid lines for extends. Dotted lines are for implements and solid lines for extends. MyComparableAbstractObject Chr Int Dbl Str

Review Questions Suppose we define two concrete classes A and B, both of which are derived from the AbstractObject class. Furthermore, let a and b be instances of classes A and B respectively, declared as follows: 1 public class A extends AbstractObject {... }; 2 public class B extends AbstractObject {... }; 3 Comparable a = new A(); 4 Comparable b = new B();