CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

Characteristics of OOP Encapsulation chap.5 Inheritance chap.6 Polymorphism chap.7.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
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.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
1 Lecture 2: Object Oriented Programming in Java.
Cs3180 (Prasad)L8Packages1 Packages Organizing large programs => From monolithic programs to partitioning class name space Access control of names => Enforcing.
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
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.
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
10-Nov-15 Java Object Oriented Programming What is it?
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Classes Dwight Deugo Nesa Matic
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
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.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
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.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
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.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
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.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
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.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Packages - allow you to group classes as a collection Standard Java library has packages: java.lang and java.util … Also hierarchical: java and javax.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Chapter 11 Inheritance and Polymorphism
Packages When we name a program , we name it by class name…
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
Inheritance 2nd Lecture
Extending Classes.
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Packages and Interfaces
Comp 249 Programming Methodology
Inheritance 2nd Lecture
Lecture 16 - Interfaces Professor Adams.
Building Java Programs
Inheritance 2nd Lecture
CMSC 202 Inheritance II.
Chapter 5 Classes.
Presentation transcript:

CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun

Announcements Project 1 is out! Due July 15 th. Check the course website. Reminder: the class newsgroup ucb.class.cs61b should be considered as required reading.

Today Object equals() Packages

Object Object class is the ultimate ancestor – every class in Java extends from the Object class. StudentEmployee Human Object class Human extends Object class Employee extends Human

Object A variable of class Object can be used as a generic placeholder for a reference to an object of any type. Object obj = new Employee(“Hacker Joe”, 35000); To do anything specific with obj, you need to cast it to some type: ((Employee)obj).raiseSalary(0.4); All array types extend the object class: Employee[] staff = new Employee[10]; obj = staff; obj = new int[10];

From Lecture 3: Key characteristics: – Object’s Behavior: what can you do with the object, what methods can you call? – Object’s State: how does the object react to methods applied to it? What does it look like? – Object’s Identity: how to distinguish the object from others with same behavior and state?

equals() A method in the Object class that tests whether one object is considered equal to another. x y Employee Reference based equality (default implementation in Java) x y Employee name = “dave” id = 101 name = “dave” id = 101 State-based equality

equals() class Employee {... public boolean equals(Object otherObject) { //a quick test to see if two objects are equal if (this == otherObject) return true; //must return false if the explicit parameter is null if(otherObject == null) return false; //if the classes don’t match they can’t be equal if (getClass() != otherObject.getClass()) return false; //now we know otherObject is a non-null employee Employee other = (Employee) otherObject; //test wehther the fields have identical values return name.equals(other.name) && salary == other.salary && hireDate.equals(hireDate); } }

equals() in Subclass When defining the equals method of a subclass, first call the superclass equals, then compare the instance fields of the subclass: class Manager{... public boolean equals(Object otherObject) { if (!super.equals(otherObject)) return false; //super.equals checked that this and otherObject are in the same //subclass Manager other = (Manager) otherObject; return bonus = other.bonus; } }

A Common Mistake public class Employee { public boolean equals(Employee other) { return name.equals(other.name) && salary == other.salary && ID == other.ID; } } The explicit parameter other is declared to be Employee, not Object. So public boolean equals(Employee other) will not override the method public boolean equals(Object other) The compiler and the run time will not pick this up because equals(Employee other) is treated using method overloading (not overriding).

@Override Since Java 5.0, you can protect yourself against this type of error with the public boolean equals(Object other) If you make a mistake and the new method does not override any method in the superclass then the compiler will pick up the public boolean equals(Employee other)

toString() Another method in Object that returns a string representation of the value of this object. When you do : Employee x = new Employee(…); System.out.println(x); //calls the toString() //method on x returns a //string object to //Systme.out.println Default implementation in Java prints the class name and the hash code of the object: System.out.println(System.out)

Packages Java organizes “related” of classes into collections called packages. Packages are used to separate your work from code provided by others:. – Guarantee the uniqueness of class names. – Packages can contain hidden classes that are used by the package but are not visible or accessible outside the package. – Classes in packages can have fields and methods that are visible by all classes inside the package, but not outside.

Examples of Packages The Java Standard Library is contained in java and javax; In the standard library: – java.lang; – java.util, – java.net – A nesting/hierarchy of classes like the nested subdirectories on your harddrive. Package names are hierarchical. java.awt.image.Model refers to the class Model inside the package image inside the package awt inside the package java.

Using Packages A class can use all classes from its own package and all public classes from other packages. Two ways to access public classes from other packages: 1.Using a fully qualified name: java.lang.System.out.println(“Here is an example”); 2.Using the import statement to give you a shorthand way of referring to files in the packages. import java.util.ArrayList; import java.util.*; //now you can refer to everything //in.util Every Java program implicitly imports java.lang.*, so you don't have to import it explicitly to use System.out.println().

Naming Conflict If you import multiple packages that contain classes with the same name, you’ll get an error: import java.util.*; import java.sql.* Date today; //ERROR Two solutions: 1.import java.util.*; import java.sql.* import java.util.date.*; 2.java.util.Date birthday= new java.util.Date();

Static Import Java 5.0 introduced syntactic sugar to abbreviate calls to Static (class) methods such as System.out and Math.pow : – import static java.lang.System.out ; means “within this file, you can use out as an abbreviation for System.out. – import static java.lang.System.*; means “within this file, you can use any static method in System without mentioning the package.

Adding a Class to a Package package cs61b.class.day8 public class Employee { … } If you don’t specify the package statement – The classes in that source belongs to the default package. – The default package has no package name. If you specify the package statement – You must place files in a package into a subdirectory that matches the full package name. – Employee.class must appear in the directory cs61b/class/day8 (or cs61b\class\day8 on Windows).

How does javac and java Locate Files? They can appear in any of the directories on your CLASSPATH environment variable (in Unix). – % printenv CLASSPATH.:/home/ff/cs61b/lib/ucb.jar – Java first looks in ".", the current directory, and then looks in /home/ff/cs61b/lib/ucb.jar (which is a file of classes in a compressed format. – The CLASSPATH does not include the location of the Java standard library packages (those beginning with java). The Java compiler knows where to find them.

javac When compiling a source file, the compiler often needs information about a type (a class or interface) it does not yet recognize. Javac will search through the directories in the CLASSPATH: – Search produces a class file but no source file: javac uses the class file. – Search produces a source file but no class file: javac compiles the source file and uses the resulting class file. – Search produces both a source file and a class file: javac determines whether the class file is out of date. If the class file is out of date, javac recompiles the source file and uses the updated class file. Otherwise, javac just uses the class file.

From Day 4 package MyList; public class ListNode { int item; ListNode next; } package MyList; public class List {... public void insertTail(int item) { if (head == null) head = tail = new ListNode(item, null); else { tail.next = new ListNode(item, null); tail = tail.next; } } }

Package Visibility Same PackageSubclassEverywhere publicYes protectedYes No defaultYesNo privateNo public declarations represent specifications—what clients of a package are supposed to rely on. package private declarations are part of the implementation of a class that must be known to other classes that assist in the implementation. protected declarations are part of the implementation that subtypes may need, but that clients of the subtypes generally won’t. private declarations are part of the implementation of a class that only that class needs.

Next Time Head First Java – pp , ,