CSC 205 Java Programming II

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Lecture 11: Polymorphism and Dynamic Binding Polymorphism Static (compile-time) binding Dynamic (run-time) binding.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
©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.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
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.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance and Polymorphism
CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
The Object class Object package java.lang Object clone equals hashCode toString aCopy toThis hash string ! yesOrNo.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
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.
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.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Object-Oriented Concepts
Sections Inheritance and Abstract Classes
Inheritance In the real world, objects aren’t usually one-of-a-kind.
Lecture 12 Inheritance.
Object-oriented Programming in Java
Chapter 11 Inheritance and Polymorphism
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Lecture 17: Polymorphism (Part II)
Continuing Chapter 11 Inheritance and Polymorphism
Computer Science II Exam 1 Review.
Extending Classes.
Java Programming Language
CSC 113: Computer programming II
Polymorphism and access control
Building Java Programs
Generic Classes and Methods
More About Inheritance & Interfaces
Overriding methods from the Object class: equals, toString
Lecture 18: Polymorphism (Part II)
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Presentation transcript:

CSC 205 Java Programming II The Object Class

The Object Class Every class—with the exception of a special class named Object—is required to have a superclass. If no superclass is specified in the declaration of a new class, Java uses Object as the default superclass. Because of this rule, all classes (other than Object itself) have Object as a superclass, either directly or indirectly.

The Java Class Hierarchy Java’s classes belong to a single “family tree,” known as a class hierarchy:

The Java Class Hierarchy The existence of a single class hierarchy has some important consequences: Every class (other than Object itself) inherits methods from the Object class. A variable of type Object can store a reference to any object whatsoever. A method with an Object parameter will accept any object as its argument.

Object Methods A partial list of methods in the Object class: clone()—Returns a copy of this object. equals(obj)—Indicates whether the object obj is “equal” to this object. toString()—Returns a string representation of this object. These methods are inherited by the subclasses of Object, so that every class in Java has these methods. These methods are frequently overridden.

The equals Method The equals method is provided to solve the problem of testing whether two objects are equal. equals has one parameter, an arbitrary object, and returns a boolean result: public boolean equals(Object obj) { … } By default, the equals method behaves like the == operator. The call x.equals(y) returns true only if x and y refer to the same object.

The equals Method If the default behavior of the equals method is unsatisfactory, a class can override the method. An equals method for the Fraction class: public boolean equals(Object obj) { if (!(obj instanceof Fraction)) return false; Fraction f = (Fraction) obj; return (numerator == f.numerator && denominator == f.denominator); }

The equals Method A statement that tests whether two Fraction objects are equal: if (f1.equals(f2)) … The inherited version of equals would have tested whether f1 and f2 refer to the same object. The new version will test whether f1 and f2 have matching numerators and denominators.

The equals Method The equals method needs to behave in the manner expected of an equality test. In particular, if x.equals(y) returns true, then y.equals(x) should also. It’s not necessary to provide equality testing for every class: Equality testing may not be meaningful for a class. It may not be worthwhile to take the time to write the equals method.

The toString Method Because toString is declared in the Object class, all classes must have a toString method. The print and println methods are overloaded, with one version of each method requiring an Object parameter. When supplied with an object as its argument, print (or println) calls the toString method that belongs to the object and prints the string returned by toString.

The toString Method If an object belongs to a class that doesn’t have a toString method, print will use an inherited version of toString. The version of toString in the Object class returns a string containing the name of the object’s class, the @ character, and a hexadecimal number (the object’s “hash code”).

The toString Method If the Fraction class didn’t contain a toString method, printing a fraction would cause Object’s version of toString to invoked. The resulting output would have the form Fraction@1cc78a This information isn’t very useful, so it’s a good idea for most classes to provide their own toString method.

The clone Method The contract of the clone() method is the following The cloned object must not be the same as the original  obj.clone() != obj The cloned object and the original object are instances of the same class The cloned object must be equal to the original object  obj.clone().equals(obj) For an object to be cloneable, the class should implement the Cloneable interface

Interface An interface is a purely abstract class It doesn’t have any method implementation, just method headers Classes implement an interface need to implement part or all methods defined in the interface If only a subset of the methods are implemented, the class needs to be labeled abstract

The Cloneable Interface The Cloneable interface is a marker interface It doesn’t have any method defined! obj.clone() will cause an exception to be thrown if obj is an instance of a class that doesn’t implement the Cloneable interface

Shallow Copy The clone() method as defined in the Object class just performs a shallow copy For instance variables that are object references, only the references will be copied, not the objects acct:Account acct:Account acctNo:String balance:double acctNo:String balance:double “A-101”