CS 302 Week 11 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
Written by: Dr. JJ Shepherd
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Programming With Java ICS Chapter 8 Polymorphism.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
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.
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.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
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.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Classes and Objects Introduced
Chapter 11 Inheritance and Polymorphism
Lecture 17: Polymorphism (Part II)
Chapter 8 Classes and Objects
Continuing Chapter 11 Inheritance and Polymorphism
Advanced Programming in Java
CS Week 14 Jim Williams, PhD.
Advanced Programming in Java
CS Week 13 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Computer Science II Exam 1 Review.
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
CS 200 More Classes Jim Williams, PhD.
Chapter 9: Polymorphism and Inheritance
Variables and Their scope
CSE 1030: Implementing Mixing static and non-static features
CS 302 Week 9 Jim Williams.
Object Oriented Programming Review
Lecture 18: Polymorphism (Part II)
CIS 199 Final Review.
CS 200 Creating Classes Jim Williams, PhD.
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
CS 200 More Classes Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Review for Midterm 3.
CS 240 – Advanced Programming Concepts
Announcements Lab 5 due Wednesday at noon.
Presentation transcript:

CS 302 Week 11 Jim Williams, PhD

Notes Midterm 2 Thursday Topics: Locations Inheritance, overriding, Object class, polymorphism Misconceptions

Review Members of a Class new Dog( “Fido”); new Dog( “Sadie”); Dog() ? Save to instance var Assign final in ctor? Static vs instance Public vs private Accessor vs mutator toString, equals public class Dog { public String name; private final int ID; static int numIds = 0; public Dog( String name) { this.name = name; ID = ++numIds; } public String toString() { return name + ID; No there isn't a no-argument constructor. The default isn't available since a constructor is provided. is the constructor parameter saved to the instance var? no. need this.name = name; Yes, a value can be assigned to a final instance variable in the constructor A static (class) variable there is one. An instance variable there is one per instance. public accessible outside the class and package, private accessible inside the class An accessor retrieves (reads, gets) information, a mutator changes (writes, sets) information. toString and equals methods are inherited from Object and may be overridden.

Demo Equals method Object class inheritance overriding polymorphism is-a vs has-a @Override public boolean equals(Object obj) { if ( obj == this) { return true; } else if ( obj instanceof Apple){ Apple objApple = (Apple)obj; System.out.println("objApple: " + objApple); if ( this.name.equals(objApple.name)) { } else { return false; }

Does equals() _______ Object's method? override overload neither error class Apple { private String brand; public Apple(String brand) { this.brand = brand; } public boolean equals(Apple obj) { return this.brand.equals( obj.brand); overload, since Object's equals method is defined: public boolean equals(Object obj)

Does equals() _______ Object's method? override overload neither error class Apple { private String brand; public Apple(String brand) { this.brand = brand; } public boolean equals(Object obj) { Apple anApple = (Apple)obj; return this.brand.equals( anApple.brand); } override is true, but is not safe. Before doing the downcast of obj to Apple, the safe was is to use instanceof to check whether obj is an instance of Apple.

Exam Today

Draw what is happening in memory. public class Cup{ private int count = 0; private double percentFull= 0; public Cup( double percentFull) { this.percentFull = percentFull; count++; } public static void main(String []args) { Cup[] cups = new Cup[5]; for ( int i = 0; i < cups.length; i++) { cups[i] = new Cup((double)i / cups.length);

Drawing Program Create a class Shape with methods getArea and draw. Extend Shape with classes Circle and Rectangle and implement those methods. Recall for Circle: Area = π * radius * radius; Circumference = π × diameter Create a Board class that provides methods for adding shapes and showing shapes. Create a main method for adding and showing shapes.

Misconceptions

True or False, Why? (counter example?) The computer knows the intention of a piece of code and acts accordingly. Assignment moves a value from one variable to another. Assignment works in both directions. Assignment copies a value from the right side to the variable on the left side. 1 false 2 false 3 false 4 true

True or False, Why? (counter example?) Expressions (not their values) are passed as parameters. A variable can hold multiple values at a time. A variable remembers old values. In Java, objects and instances are the same thing (synonymous) and are different from classes. 1 false 2 false 3 false 4 true

True or False, Why? Two objects with the same value of a “name” attribute are the same object. true false depends error False, two objects may have the same value of any attribute

Two different variables must refer to two different objects. True or False, Why? Two different variables must refer to two different objects. true false depends error False, two different variables may have the reference to the same object

True or False, Why? Two objects with the same class and the same state are the same object. true false depends error False, two objects are different objects regardless of their state. The state of an object is the values of its attributes. Two instances of a Stoplight class, for example, could both have state 'red' but would not be the same stoplight.

A set (such as “team” or “the species of birds”) cannot be a class. True or False, Why? A set (such as “team” or “the species of birds”) cannot be a class. true false depends error False, anything can be the name of a class.

True or False, Why? Constructors can include only assignment statements to initialize attributes. true false depends error False. Frequently constructors contain assignment statements, but they can include other statements as well.

True or False, Why? Instantiation involves only the execution of the constructor method body, not the allocation of memory. true false depends error False, instantiation includes the allocation of memory and calling the constructor.

Declaring a variable also creates an object. True or False, Why? Declaring a variable also creates an object. true false depends error False. Duck d; declares a variable but does not create an object(instance) of Duck.

True or False, Why? There is no need to invoke the constructor because its definition is sufficient for object creation. true false depends error False

An object cannot be the value of an attribute. True or False, Why? An object cannot be the value of an attribute. true false depends error False. The instance of the string is an object that is the value of an attribute. class Person { String name = "Gilmore"; }

Methods do not affect object state. True or False, Why? Methods do not affect object state. true false depends error False, mutators specifically affect an object's state