1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 CS2200 Software Development Lecture 29: Polymorphism I A. O’Riordan, 2008 Based on notes by K. Brown.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
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.
Coming up: Inheritance
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.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Peyman Dodangeh Sharif University of Technology Spring 2014.
1 Chapter 4 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
1 Chapter 2 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Classes Revisited Chapter 8.
OOP Basics Classes & Methods (c) IDMS/SQL News
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Object-Oriented Concepts
Chapter 11 Inheritance and Polymorphism
03/10/14 Inheritance-2.
Lecture 17: Polymorphism (Part II)
CS 302 Week 11 Jim Williams, PhD.
Computer Science II Exam 1 Review.
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
Inheritance.
Building Java Programs
Special instance methods: toString
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 8 Inheritance Part 2.
Presentation transcript:

1 SSD3 - Unit 2 Java toString & Equals Presentation Class Website:

2 Class Visibility We have two choices with respect to the manner in which we declare class visibility: –As public: public class Student { // Attributes and methods... } –With no explicit visibility: class Professor { // Attributes and methods … } The implications of these two choices are illustrated on the diagrams that follow

3 Package APackage B public class W { … } class X { … } public class Y { … } class Z { … } W.java Y.java

4 Package APackage B public class W { can create objects of type W and X, but not of Y or Z } class X { ditto … } public class Y { can create objects of type Y and Z, but not of W or X } class Z { ditto... } W.java Y.java

5 Package APackage B import B.*; public class W { can create objects of type W, X, and Y, but still not of Z } class X { ditto … } public class Y { can create objects of type Y and Z, but still not of W or X } class Z { ditto … } W.java Y.java

6 Feature Visibility, Revisited We learned about three types of feature visibility early on: public, private, protected We hadn't talked about class visibility or packages at that time, and so we oversimplified what these three forms of visibility really entail - we'll revisit that now

7 Feature Visibility, Cont. Also, if the access type for a given feature is omitted entirely, as in the following example: public class Person { public String name; private String ssn; protected String address; int age; then the feature in question is said to have package visibility by default

8 Feature Visibility, Cont. Here is the effective visibility for a given feature of public class 'A' with respect to a class 'B', depending on where the two classes live: (Person)

9 Feature Visibility, Cont. hmmm... (Person) (Student)

10 Feature Visibility, Cont. hmmm... whoa!!! (Person) (Student) (Course)

11 Feature Visibility, Cont. (assuming import of Person) (Person) (Course) (Student)

12 Feature Visibility, cont. For same-package classes, the bottom line is that if we don’t explicitly declare something to be private, then it essentially becomes public: public class Person { // This first attribute is truly private. private String ssn; // But, all the rest of these attributes are // effectively public from the perspective of // other classes in the same package. public String name; protected String address; // Exception: subclasses cannot see the next one! int age;

13 Determining the Class that an Object Belongs To Another method that all classes inherit from the Object class is: Class getClass() The Class class, in turn, has a method with signature: String getName() Used in combination, we can use these two methods to interrogate an object to find out what class it belongs to

14 Determining the Class that an Object Belongs To, cont. Student s = new Student(); Professor p = new Professor(); Vector v = new Vector(); v.add(s); v.add(p); for (int i = 0; i < v.size(); i++) { // Note that we are not casting the objects here! // We're pulling them out as generic objects. Object o = v.elementAt(i); System.out.println(o.getClass().getName()); } This program produces as output: Student Professor

15 Determining the Class that an Object Belongs To, cont. Another way to test whether a given object reference belongs to a particular class is via the instanceof operator –This is a boolean operator which allows us to determine if some reference variable X is an object/instance of class Y Student x = new Student(); if (x instanceof Professor) … // will evaluate to false if (x instanceof Person) … // will evaluate to true –The classname should be the fully qualified name of the class if it belongs to a package, e.g., java.lang.String

16 The toString() Method We mentioned earlier that all Java classes are descended from the Object class One of the features that all classes inherit from Object is a method with signature: public String toString(); As inherited, however, the method may not prove to be very useful, as the following example illustrates (cont.)

17 toString(), cont. Student s1 = new Student(); s1.setName("Harvey"); System.out.println(s1); // Prints out an object ID … not too useful! It is a good idea to explicitly override the toString() method for any class that you invent public String toString() { return getName(); } // Prints "Fred" (or whatever). System.out.println(s1);

18 Testing for Equality We've seen the use of a double equal sign (==) to test for equality of two values; e.g., int x = 3; int y = 4; if (x == y) do something … We can also use == to test the "equality" of two objects: Person p1 = new Person("Joe"); Person p2 = new Person("Mary"); if (p1 == p2) do something... –What does "equality" mean in the case of two object references? –As we saw from our discussion of String as objects, the == operator tests to see if two references are referring to the same object

19 Testing for Equality, cont. public class Person { private String name; // Constructor. public Person(String n) { name = n; } // Client code: Person p1 = new Person("Joe"); Person p2 = p1; // Second handle on SAME object. Person p3 = new Person("Joe"); // New object, same NAME. if (p1 == p2) System.out.println("p1 equals p2"); if (p1 == p3) System.out.println("p1 equals p3"); Prints out: p1 equals p2

20 The equals() Method Another of the features that all classes inherit from Object is a method with signature: public boolean equals(); This method is used to test the "equality" of two objects in a different way: namely, based on whatever criteria that we establish by overriding the method –Let's look at an example

21 The equals() method, cont. public class Person { private String name; // Constructor. public Person(String n) { name = n; } // Overridden from Object. public boolean equals(Object o) { boolean answer = false; try { Person p = (Person) o; // Note cast. if (p.getName().equals(name)) answer = true; else answer = false; } catch (ClassCastException e) { answer = false; } return answer; }

22 The equals() method, cont. public String getName() { return name; } // Client code. Person p1 = new Person("Joe"); Person p2 = new Person("Mary"); Person p3 = new Person("Joe"); // Different object, same name. Object o = new Object(); System.out.println("Does p1 equal p2? " + p1.equals(p2)); System.out.println("Does p1 equal p3? " + p1.equals(p3)); System.out.println("Does p1 equal o? " + p1.equals(o)); Produces as output: Does p1 equal p2? false Does p1 equal p3? true Does p1 equal o? false

23 Reading from the Command Line Now that we’ve learned about Java Strings and arrays, we can appreciate how to pass data into a Java program when invoking it from the command line When we invoke a program, we can type data after the name of the program on the command line; e.g. java Simple ABC 123

24 Command Line Input, cont. Such data gets handed to the main() method as a String array called ‘args’ (or whatever else we wish to name it), as indicated by the main() method's argument signature: public static void main(String[] args) { … } Inside the main() method, we can do with 'args' whatever we’d do with any other array –E.g, determine its length, manipulate individual String items within the array, and so forth

25 Command Line Input, cont. public class FruitExample { // If this program is run from the // command line as follows: // // java FruitExample apple banana cherry // // then the args array will be automatically // initialized with THREE String values // "apple", "banana", and "cherry", which // will be stored in array 'cells' args[0], // args[1], and args[2], respectively. public static void main(String[] args) { // Let's print out a few things. System.out.println("The args array contains " + args.length + " entries.");

26 Command Line Input, cont. // Only execute this next block of code if // the array isn't empty. if (args.length > 0) { int i = args.length - 1; System.out.println("The last entry is: " + args[i]); System.out.println("It is" + args[i].length() + " characters long."); } // etc. }

27 Command Line Input, cont. When this program is run from the command line as: java FruitExample apple banana cherry it produces the following output: The args array contains 3 entries. The last array entry is: cherry It is 6 characters long.

28 Questions?