Implementing Non-Static Features

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Road Map Introduction to object oriented programming. Classes
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
UML Basics & Access Modifier
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
10-Nov-15 Java Object Oriented Programming What is it?
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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…..
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1  lecture slides online
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
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.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Topics Instance variables, set and get methods Encapsulation
CSE 1020:Using Objects Mark Shtern 1-1. Summary Read API Method binding and overloading Development process Input validation Assert 1-2.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Lecture 5:Interfaces and Abstract Classes
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
CS 200 Creating Classes Jim Williams, PhD.
Introduction to Classes
Defining Classes and Methods
Java Programming Language
CSE 1030: Implementing Mixing static and non-static features
CSE 1030: Implementing Non-Static Features
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
CSE 1030: Data Structure Mark Shtern.
Today’s topics UML Diagramming review of terms
Defining Classes and Methods
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Chapter 4 Defining Classes I
Defining Classes and Methods
Chapter 11 Inheritance and Polymorphism
CS 200 Creating Classes Jim Williams, PhD.
Chapter 8 Class Inheritance and Interfaces
Classes, Objects and Methods
CS 200 More Classes Jim Williams, PhD.
2009 Test Key.
OO Programming Concepts
CS 200 Creating Classes Jim Williams, PhD.
Review for Midterm 3.
Chapter 7 Objects and Classes
CS 240 – Advanced Programming Concepts
Presentation transcript:

Implementing Non-Static Features Ahmed Sabbir Arif

Utilities vs. Non-Utilities A special kind of class: Attributes are associated with the class itself rather than with its instances. Can’t have different instances with different states. Most classes are non-utility: Hence, we usually drop the “non-utility” qualifier.

Non-Utilities In this lecture we focus on classes with not static features. Remember that when a method is declared static, it can be used without having to create a object first. We need to instantiate a class before using it: Its client must begin by creating an instance.

The Rectangle Class

Example 1 Rectangle empty = new Rectangle(); // def. constructor 2 output.println(empty); 3 4 int width = 0; 5 int height = 1; 6 Rectangle rectangle = new Rectangle(width, height); 7 output.println(rectangle); 8 9 width = 2; 10 rectangle.setWidth(width); // mutator 11 output.println(rectangle);

UML Diagram Drop the <<utility>> stereotype. Here, the attribute box is empty, and The return of void methods is left blank.

Memory Diagram Implicit parameter Explicit parameter Invocation block 1 Rectangle empty = new Rectangle(); 2 output.println(empty); 3 4 int width = 0; 5 int height = 1; Rectangle rectangle = new Rectangle(width, height); 7 output.println(rectangle); 8 9 width = 2; 10 rectangle.setWidth(width); 11 output.println(rectangle); Implicit parameter Explicit parameter Invocation block

Class Structure The 3 sections of the class definition can appear in any order. If a class implements one/more interface then its header must declare this fact by using the keyword implements. 1 // any needed package statement 2 // any needed import statements 3 4 public class SomeName 5 { 6 // the attribute section 7 // the constructor section 8 // the method section 9 }

The Attribute Section Make sure the attribute meets the specification stated in the API of the class. The final outcome may not be unique: However, all implementations are correct. access type name; access final type name;

The Attribute Section, cont. The access can be either public: If it is a field. Or private: Means it is not visible to the client. Keep all non-final attributes private: Forces the client to use a mutator. Simplifies the API.

The Attribute Section, cont. Use final if the attribute is a constant. Constant per object, not per class! type specifies the type of this attribute: Primitive or non-primitive.

The Rectangle Class

The Attribute Section 1 public class Rectangle 2 { 3 private int width; 4 private int height; 5 ... 6 } Initialize static attributes as you declare them and leave the initialization of non-static ones to the constructor section. The scope of an attribute is the entire class. this.a refers to the attribute a of the (non static) object on which the method is invoked.

The Constructor Section A class constructor is invoked when the class is instantiated (the new operator). The default constructor of the Rectangle class: access signature; 1 public Rectangle() 2 { 3 this.width = 0; 4 this.height = 0; 5 }

The Constructor Section, cont. Example: A two-parameter constructor: Remember the client code fragment from the first example? 1 public Rectangle(int width, int height) 2 { 3 this.width = width; 4 this.height = height; 5 }

Example 1 Rectangle empty = new Rectangle(); // def. constructor 2 output.println(empty); 3 4 int width = 0; 5 int height = 1; 6 Rectangle rectangle = new Rectangle(width, height); 7 output.println(rectangle); 8 9 width = 2; 10 rectangle.setWidth(width); // mutator 11 output.println(rectangle);

Another Example 1 int width = 1; 2 int height = 2; 3 Rectangle rectangle; rectangle = new Rectangle(width, height);

The Method Section Contains one definition per method. The type is either void or the actual type of the return. The signature consists of its name followed by its parameter list. access type signature;

The Method Section, cont. Common themes of methods: State-related methods, i.e. accessors, mutators, Obligatory methods that override those of the Object class, i.e. toString, equals, and hashcode, Interface-related methods, i.e. compareTo, and Class-specific methods, i.e. getArea and scale.

Accessors Allows the client to gain access to the value of one of the attributes of an instance of the class. 1 public int getWidth() 2 { 3 return this.width; 4 }

Mutators Allows the client to modify the value of one of the attributes of an instance of the class. 1 public void setWidth(int width) 2 { 3 this.width = width; 4 }

Obligatory Methods Java expects certain methods, such as equals, hashCode and toString, to be in all or almost all classes. The purpose of equals, a boolean valued method, is to compare two objects of the class to see if they satisfy the notion of "being equal“ Note: You cannot use == to compare objects public boolean equals(ClassName objectName) The purpose of the toString method is to return a String value that represents the data in the object public String toString() The purpose of the hashCode method is to return an integer that can serve a hash code for the object – it should be the same for objects that are equals public int hashCode()

Testing a Class Different from testing a utility class: Need to check if the constructors behave according to their specifications. May have multiple constructors. A test case not only test the values for the parameters but also the value for the implicit parameter this.

Example 1 Random random = new Random(); 2 final int MAX = 100; 3 int width = random.nextInt(MAX + 1); 4 int height = random.nextInt(MAX + 1); 5 6 Rectangle rectangle = new Rectangle(width, height); 7 8 if (rectangle.getWidth() != width) 9 { output.print("Either the two-parameter constructor or the method getWidth failed the test case: "); 11 output.println("width = " + width + ", height = " + height); 12 } 13 if (rectangle.getHeight() != height) 14 { getHeight failed the test case: "); 16 output.println("width = " + width + ", height = " + height); 17 }

Further Reading How to avoid code duplication? Immutable Objects Attribute Caching Class Invariants Validation and the Implementer

Lab Exercises Due: before 12am on Friday Grades: Code (50%) Syntax (25%) Testing (25%)

Lectures Tuesday and Thursday: Labs: 2:30pm – 4:00pm 5:00pm – 6:30pm I will check if I can create another (lab) section.