CLASS DEFINITION (> 1 CONSTRUCTOR)

Slides:



Advertisements
Similar presentations
JAVA Revision Lecture Electronic Voting System Marina De Vos.
Advertisements

Looking inside classes Fields, Constructors & Methods Week 3.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Written by: Dr. JJ Shepherd
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
Understanding class definitions Looking inside classes.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
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.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Copyright by Scott GrissomCh 2 Class Definition Slide 1 Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Topics Instance variables, set and get methods Encapsulation
Understanding class definitions Exploring source code 6.0.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
Introduction to Constructors Lecture # 4. Copyright © 2011 Pearson Education, Inc. 3-2 Arguments Passed By Value In Java, all arguments to a method are.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Chapter 9 A Second Look at Classes and Objects - 2.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Methods Attributes Method Modifiers ‘static’
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Generic array list and casting C&K s7.7
Creating Your OwnClasses
Understanding class definitions
CS 302 Week 11 Jim Williams, PhD.
public class BankAccount{
User-Defined Classes and ADTs
The Basics of Class Diagrams for a single class
Introduction to Classes
Corresponds with Chapter 7
An Introduction to Java – Part II
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 8: User-Defined Classes and ADTs
More on Classes and Objects
Implementing Classes Chapter 3.
Today’s topics UML Diagramming review of terms
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Agenda About Homework for BPJ lesson 36 About practice of last class
Basics of OOP A class is the blueprint of an object.
COS 260 DAY 4 Tony Gauvin.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
JAVA CLASSES.
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Handout-4b More on Classes
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Classes and Objects Systems Programming.
Chapter 7 Objects and Classes
Building Java Programs
Day 11 The Last Week!.
Presentation transcript:

CLASS DEFINITION (> 1 CONSTRUCTOR) public class Point { private int x; private int y; public Point() { x = 0; y = 0; } public Point(int x, int y) { x = x; //Which x? y = y; //Which y? Point p1 = new Point(); // x = 0, y = 0 Point p2 = new Point(1, 2); // x = 1, y = 2 Constructors (basically methods) can be overloaded.

CLASS DEFINITION (> 1 CONSTRUCTOR) public class Point { private int x; private int y; public Point() { x = 0; y = 0; } public Point(int x, int y) { this.x = x; this.y = y; Point p1 = new Point(); // x = 0, y = 0 Point p2 = new Point(1, 2); // x = 1, y = 2 Constructors (basically methods) can be overloaded.

this keyword The variable this is a variable which is implicitly bound within instance methods (methods not marked as static). this refers to the instance of the class on which you are currently “operating”. You can tell what value this is bound to at the call site (it’s to the left of the dot).

CLASS DEFINITION (> 1 CONSTRUCTOR) public class Point { private int x; private int y; public Point() { this(0, 0); // Call the "designated initializer" } public Point(int x, int y) { this.x = x; this.y = y; It is best practice to select one constructor to do initialization. All non-designated constructors should eventually call the "designated" initializer.

CLASS DEFINITION (METHODS) public class Point { private int x; private int y; public boolean isOrigin() { if(x==0 && y==0) return true; else return false; } public boolean isOrigin() { return(x==0 && y==0); } Same? (Instance) methods operate on the state of the instance. Fields of the instance are in-scope within methods and can be read or mutated as local variables can.

CLASS DEFINITION (METHODS) Non-private methods can be called on an instance outside of the defining class class Rectangle{  private int length;   private int width;     public Rectangle (int l, int w){      length=l;      width=w;     }     public int calculateArea(){ return length * width; }   Rectangle rec = new Rectangle(10, 10); Int area1 = rec. calculateArea(); // OK Rectangle rec = new Rectangle();// Is this valid? Why?

METHOD CATEGORIES (GETTERS/ACCESSORS) Methods which simply return an instance variable are called accessors. Accessors generally have little/no logic. public class BankAccount { private int balance; // remainder of implementation omitted public int getBalance() { return balance; } Accessors allow read-access to a private instance variable. This disallows direct assignments from outside. Consider the consequence of a public balance field.

METHOD CATEGORIES (SETTERS/MUTATORS) Methods which simply set an instance variable are called mutators. Mutators generally have little logic. public class BankAccount { private String nickname; public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; Mutators allow write-access to a private instance variable without allowing a direct assignment from outside. If a mutator has validation code or some additional logic, a direct assignment could bypass this code.

METHOD CATEGORIES (SETTERS/MUTATORS) Mutator with validation of input value. public class PositiveIntHolder { private int value; public void setValue(int value) { if (value > 0) { this.value = value; } What if value was public?

'BEST' PRACTICES In Java, it is a best practice to make all fields private and add getters/setters for everything. This is often an example of over-engineering and isn't always necessary. Non-private fields are perfectly acceptable given the right circumstances.

A QUICK INTRO TO UML Unified Modeling Language (UML) is a set of diagramming techniques used in software engineering. We will use class diagrams in this class where applicable A class diagram tersely describes the class, as well as its relationship to other classes (we'll see more about relationships in the following weeks)

A QUICK INTRO TO UML ClassName vis attribute : type vis operation(param. list) : return type vis = visibility (+ for public, - for private) attribute = data member (field) operation = method (or constructor)

Example