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

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

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CS61B Lecture #8: Object-Oriented Mechanisms

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Introduction to Computation and Problem Solving

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Generic types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:

Closed book but one sheet, both sides, of A4 paper is allowed. Section 2.5 of the text Generics in the Java Programming Languages by Gilad Bracha

CIS 110: Introduction to computer programming

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

More on Objects in JAVA TM

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Java Object Oriented Design. CSC207 Fall 2014

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

! labs last week. ! still time to work through lab 7 (midterm correction) ! can earn back up to 5 out of 70 points

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

CS 116 Week 8 Page 1

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

INSTRUCTIONS TO CANDIDATES

Programming overview

CS111: PROGRAMMING LANGUAGE II

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

CMSC 341. Nilanjan Banerjee

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

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

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Inheritance and Interfaces

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

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

ITI Introduction to Computing II

Abstract class & Interface

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

9/10/2018 Programming Data Structures Inheritance

ECE 122. Engineering Problem Solving with Java

Admin CS41B MACHINE. Midterm topics. Admin 2/11/16. Midterm next Thursday in-class (2/18) SML. recursion. math. David Kauchak CS 52 Spring 2016

Super-Classes and sub-classes

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS/ENGRD 2110 SPRING 2018

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

Carleton University COMP1406/1006, Summer 2013 Tutorial 2

Inheritance, Polymorphism, and Interfaces

Basic Object-Oriented Concepts. 5-Oct-17

Lecture 10: Interfaces

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSSE 220. Interfaces and Polymorphism. Check out Interfaces from SVN

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

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

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

CS 61B Discussion 5: Inheritance II Fall 2014

CS18000: Problem Solving And Object-Oriented Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

ECE 122. Engineering Problem Solving with Java

CS 251 Intermediate Programming Inheritance

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Assignment3 CS206 Intro to Data Structures Fall Part 1 (50 pts) due: October 13, :59pm Part 2 (150 pts) due: October 20, :59pm

1B1b. Classes in Java Part III. Review. Static. Static (2) Example. Static (3) 1B1b Lecture Slides. Copyright 2004, Graham Roberts 1

More On inheritance. What you can do in subclass regarding methods:

VIRTUAL FUNCTIONS Chapter 10

Today. Reading. Homework. Lecture Notes CPSC 224 (Spring 2012) hashcode() method. Collections class. Ch 9: hw 7 out (due in a week)

Lecture 18 CSE11 Fall 2013 Inheritance

Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II. Inheritance

Admin. ! Assignment 3. ! due Monday at 11:59pm! one small error in 5b (fast division) that s been fixed. ! Midterm next Thursday in-class (10/1)

Week 11: Class Design

Lecture 4: Extending Classes. Concept

Instance Members and Static Members

CS61B Lecture #11. Please report problems (missing files, malfunctions of submit, etc.) by , not by the newsgroup, for faster service.

Introduction to Object-Oriented Programming

Week 5-1: ADT Design

INHERITANCE AND TYPE HIERARCHIES. Prof. Chris Jermaine

CSE 401 Final Exam. December 16, 2010

Motivations. Chapter 13 Abstract Classes and Interfaces

Chapter 1: Object-Oriented Programming Using C++

Darrell Bethea June 6, 2011

COS 126 General Computer Science Spring Written Exam 1

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

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

Administrivia. IBM Info Session Date: Wed,, Jan 13 Time: 5:30 7 pm Location: Wesbrook 100

CS111: PROGRAMMING LANGUAGE II

Lecture 5: Methods CS2301

INHERITANCE AND EXTENDING CLASSES

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

2. [20] Suppose we start declaring a Rectangle class as follows:

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

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

Transcription:

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

Announcements Project 1 spec and code is available on the course website. Due July 15 th. Start early! Midterm I is next Wed in class from 11:00 1:00p.m. Open book: lecture notes, lab notes, but no laptop!

Today Constants Preventing Inheritance Abstract Classes Interfaces

class Parent { int x = 0; Static Fields and Methods static int y = 1; static void f() { System.out.printf "Ahem!%n"); static int f(int x) { return x+1; Child dave = new Child(); Parent pdave = dave; class Child extends Parent { String x = "no"; static String y = "way"; static void f() { System.out.printf ("I wanna!%n"); dave.x => no pdave.x => 0 dave.y => way pdave.y => 1 dave.f() =>? pdave.f() =? dave.f(1) =>? pdave.f(1) => 2 Fields hide inherited fields of same name; static methods hide methods of the same signature.

Constants Adding final in front of a variable means that the value of the variable can never be changed. You must initialize the value of the final variable when it is declared. Use final variables to create constants. class Math { public static final double PI = 3.141592653 ; public class System { public static final PrintStream out = ;

Constants (cont.) Numerical values that are used repeatedly should be turned into a final constant. if (month == 3) {... //BAD public static final int MARCH = 3; if (month == MARCH) {... Not all constants need to be static. For an array x, x.length is a final field.

Preventing Inheritance A class declared to be final cannot be extended by others final class Executive extends Manager{ A method declared final cannot be overriden by subclasses All methods in a final class are automatically final class Employee { public final String getname() { return name;

Abstract Classes As we move up the inheritance hierarchy, classes become more general or more abstract. At the some point, the ancestor class becomes so general that it becomes a basis for other classes rather than a class with specific instances that you want to create. Such ancestor classes are declared to be abstract. Their sole purpose is to be extended.

Example Human Employee Student Some attributes and methods make sense for every human, and these can be factored out into the Human class. public class Human {... private int age; private String name; public int getage() { return age; public String getname() { return name;...

Example (cont.) public class Human {... public String introduce() {... //how should we code this? Student david = new Student(... ); david.introduce(); // I m David and I major in Computer Science Employee wendy = new Employee(... ); wendy.introduce(); // I m Wendy I work for Google The Human class knows nothing about the person except the name and age. We can try: public String introduce() { return ; A better way: public abstract String introduce(); //no implementation required

Abstract Class public abstract class Human { private String name; public Human(String n); public String getname() { return name; public abstract String introduce(); Any class with one or more abstract methods must be declared as abstract. abstract classes can have concrete data and methods. abstract methods act as placeholders for methods that are implemented in subclasses.

public abstract class Human { protected String name;... public abstract String introduce(); public class Student extends Human{ private String major; public student(string name, String major) {... public String introduce() { return I m + name + and I major in + major; public class Employee extends Human{ private String company; public student(string name, String company) {... public String introduce() { return I m + name + and I work for + company;

Rules for abstract Classes The concrete classes Student and Employee must implement the abstract method introduce(). The compiler will generate an error if you don t. If you extend an abstract class without implementing an abstract method, the subclass must be declared as abstract. You can create a variable of an abstract class: Human student; //OK You cannot create an object of an abstract class: //Compile time ERROR! Human student = new Human( David ); //OK! Human student = new Student( David ); student.introduce();

Example Human[] human = new Human[2]; human[0] = new Employee(... ); human[1] = new Student(... ); for (int i = 0; i < human.length; i++) { human[i].introduce(); The call human[i].introduce(); is well defined because human[i] never refers to an object of the abstract human class, it must refer to a concrete subclass.

What are Interfaces? Describing what classes should do, without specifying how they would do it. Think of it as a contract for a set of classes. If your class conforms to the requirements set in this contract (or this interface), then I ll perform these services for the objects of your class

Example The sort method of the Array class promises to sort an array of objects, but under one condition: the objects in the array must implement the Comparable interface: public interface Comparable { int compareto(object other); Like an abstract method, no implementation is provided

Example public interface Comparable { int compareto(object other); public class Employee implements Comparable { public int compareto(object other) {... The Employee class must implement the compareto method. i.e., define a method named compareto that takes an Object and returns an int.

compareto When we call x.compareto(y), the method returns an indication whether x or y is larger: return a negative number if y is larger a positive number if x is larger, zero otherwise. Suppose we want to sort by salary: public class Employee implements Comparable { public int compareto(object other) { Employee otheremployee = (Employee) other; if (salary < otheremployee.salary) return -1; if (salary > otheremployee.salary) return 1; return 0;

Example

compareto If the objects are not equal, it does not matter what negative or positive value your return. For instance, to sort by employee ID number: //negative if the first ID is less than the other, //0 if they are the same; //some positive value otherwise. return id other.id; The subtraction trick works only for integers: salary other.salary can round to 0 if the salaries are close enough.

Comparable An interface to describe Objects that have a natural order on them, such as String and Integer. Might use in a general purpose max function: /** The largest value in array A, or null if A empty. */ public static Comparable max (Comparable[] A) { if (A.length == 0) return null; Comparable result = A[0]; for (int i = 1; i < A.length; i += 1) if (result.compareto(a[i]) < 0) result = A[i]; return result; Now max(s) will return maximum value in S if S is an array of Strings, or any other kind of Object that implements Comparable.

Properties of Interfaces Interfaces are not classes, you can t construct objects: x = new Comparable( ); //ERROR You can declare interface variables: Comparable x; //OK An interface variable must reference an object of a class that implements the interface. x = new Employee( ); //OK

Properties of Interfaces All methods in an interface are automatically public. int compareto(object other); was not explicitly declared to be public. The implementation in Employee class must be public int compareto(object other); otherwise the compiler will complain. All fields in are automatically public static final. Interfaces can contain only constants, never instance variables.

Extending Interfaces You can extend interfaces to go from greater degree of generality to a greater degree of specialization. Example: Java Collections Framework public interface Set extends Collection{...

Interfaces and Abstract Classes abstract class Comparable { public abstract int compareto(object other); public class Employee extends Comparable { int compareto(object other) {... public class Employee extends Human, Comparable //ERROR public class Employee extends Human implements Comparable //OK A class can have one and only one superclass.

Implement Multiple Interfaces interface Readable { Object get(); interface Writeable { void put (Object x); void copy (Readable r, Writeable w) { w.put(r.get()); class Source implements Readable { Object get() {... class Sink implements Writeable { void put (Object x) {... class Variable implements Readable, Writeable { Object get() {... Object put() {...

Using Interfaces and Abstract Class Example

The Java Collections Framework

Next Lecture Readings: Head First Java, Chapter 7