1 Object-Oriented Software Engineering CS288. 2 Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
UniS 1 Object-Oriented Software Engineering CS288.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
UniS 1 Object-Oriented Software Engineering CS288.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
1 Object-Oriented Software Engineering CS Multiple Classes Contents Defining multiple classes Using objects of one class in another Multiple constructor.
Guide To UNIX Using Linux Third Edition
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
1 Object-Oriented Software Engineering CS Multiple Classes Contents Lists of objects Vectors Growing and shrinking Vectors Iteration over Vector.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
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.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
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.
PART 1 Part 1: The Material. Whether you knew it or not, all the programming that you have performed until now was in the boundaries of procedural programming.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CLASSES AND OBJECTS Object-Oriented Basics. Vocabulary Review – C++ Class Object Instance this new Constructor Default constructor Access (private/public/protected)
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
Classes - Intermediate
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Class Fundamentals BCIS 3680 Enterprise Programming.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Problem of the Day  Why are manhole covers round?
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Topic: Classes and Objects
User-Written Functions
Classes and Objects.
Static data members Constructors and Destructors
Class Definitions and Writing Methods
Classes and Objects in Java
Chapter 3: Using Methods, Classes, and Objects
Classes and Objects in Java
Classes and Objects in Java
Java LESSON 7 Objects, Part 1
Classes, Encapsulation, Methods and Constructors (Continued)
CHAPTER 6 GENERAL-PURPOSE METHODS
Object Oriented Programming in java
Lecture 5- Classes, Objects and Methods
Object Oriented Programming in java
Dr. R Z Khan Handout-3 Classes
References Revisted (Ch 5)
Corresponds with Chapter 5
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

1 Object-Oriented Software Engineering CS288

2 Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating fields Manipulating object field values Static Declarations

3 Classes and Objects Class: Template that defines how to make an object. Object: Is an instance of a class. Car int numDoors Double engineSize String make void start ( ) { code } void stop ( ) { code } Car bigFlashJag numDoors = 5 engineSize = 3.5l make = Jaguar start ( ) stop ( ) Car ecoCar numDoors = 2 engineSize = 0.5l make = Smart start ( ) stop ( ) ClassObject

4 Building Objects Computer Memory Class Definition (acts like template for new objects) Object1Object2Object3Object4 Each object has copies of fields and methods defined by class template, but initialised differently each time

5 Very Simple Class Example public class SimpleClass { public SimpleClass(String newFieldVal) { uselessField = newFieldVal; } private String uselessField; public static void main(String[ ] args) { /* Code Goes Here */ } Main method executed first. This is where objects can first be set up. Constructor, defines how new objects are initialised.

6 Syntax for defining new object SimpleClass ob1 = new SimpleClass("Fluffy"); object type object name new keyword class constructor to set up object semicolon terminates ALL statements (just like C++)

7 Very Simple Class Example public class SimpleClass { public SimpleClass(String newFieldVal) { uselessField = newFieldVal; } private String uselessField; public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); }

8 Executing the Code Five new objects are created in memory, each with their own copy of ‘uselessField’, and each copy has a different value. Slide shows view of data from NetBeans debugging console.

9 Setter and Getter Methods public class SimpleClass { /* declare class field */ /* define class constructor */ public String getUselessField () { return uselessField; } public void setUselessField (String newUselessField) { uselessField = newUselessField; } /* main method */ }

10 Getter method for field public String getUselessField () { return uselessField; } public keyword allows this method to be called from other classes type of object returned by method name and parameters of method body of method, what it does

11 Setter method for field public void setUselessField (String newUselessField) { uselessField = newUselessField; } has String object parameter does not return value changes value of field

12 Extending main method public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); } Example shows use of setting and getting methods for uselessField

13 Execution of main method public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); } When execution gets to here objects have these values

14 Execution of main method public static void main(String[] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); } When execution gets to here objects have these values

15 Execution of main method public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); } When execution finishes here objects have these values

16 Backup Method for Field Value Modify SimpleClass to include new field and methods: private String previousVal; public void setUselessField (String newUselessField) { previousVal = uselessField; uselessField = newUselessField; } public void restoreField () { uselessField = previousVal; }

17 Experiment with main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); } When execution gets to here objects have these values

18 Experiment with main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); } When execution gets to here objects have these values

19 Experiment with main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); } When execution gets to here objects have these values

20 Experiment with main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); } When execution gets to here objects have these values

21 Is there a problem with the restore method? public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); ob1.setUselessField ("I wish I'd done C instead"); ob2.restoreField (); } When execution gets to here objects have these values

22 Static Declarations Not all methods and fields belong to Objects Static methods and fields stay inside the class There is only ever one copy of a static method or field Static methods and fields are `shared’ between all the objects of that class

23 Static Declarations When we declare a new variable with a statement we create a reference to the thing we want the variable to name E.g SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob1 = ob2; Object: SimpleClass("Fluffy") ob1 ob2

24 Static Declarations Static fields stay with the class not the object E.g change declaration in SimpleClass private static String uselessField = "oooo"; Then in main method create two objects: SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob1 = new SimpleClass("Fluffy"); Object: SimpleClass("Fluffy") field: ob1 ob2 Class: SimpleClass field: uselessField Object: SimpleClass("Fluffy") field:

25 Static Declarations Changing the value of the field within one object now affects all the other objects of that class: SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); NetBeans Debugger Output

26 Static Methods Static methods are more complex than static fields This is NOT allowed: public class SimpleClass { private String uselessField = "oooo"; private String initialVal = ""; public static void setUselessField(String newUselessField) { uselessField = newUselessField; } /* and so on */ If we attempt to compile this we get error: SimpleClass.java: non-static variable uselessField cannot be referenced from a static context uselessField = newUselessField; 1 error BUILD FAILED (total time: 0 seconds)

27 Static Methods Static methods are more complex than static fields E.G. this is NOT allowed: public class SimpleClass { private String uselessField = "oooo"; private String initialVal = ""; public static void setUselessField(String newUselessField) { uselessField = newUselessField; } /* and so on */ Static methods may only refer to static fields. Whereas here uselessField is NOT declared static. WHY NOT?

28 Static Methods Static methods are more complex than static fields E.G. this is NOT allowed: public class SimpleClass { private static String uselessField = "oooo"; private String initialVal = ""; public static void setUselessField(String newUselessField) { uselessField = newUselessField; } /* and so on */ Now uselessField is declared static. Code compiles without error.

29 Static Methods Can reference static methods and fields directly from the class, do not need to create an object first. E.G. public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("Fluffy"); SimpleClass.setUselessField ("I wish I'd done C instead"); } To use the static method here we can access it directly from the class. (Notice that the main method is always declared static. WHY?)

30 Static Methods Consider adding a new method to store previous value used for uselessField: Consider adding a new method to store previous value used for uselessField, which needs a new field to hold that value in: private String lastValueField; public void storeLastValue () { lastValueField = uselessField; } Then could use this whenever set value of uselessField so that we can track the last value

31 Static Methods Then could use this whenever set value of uselessField so that we can track the last value. public static void setUselessField(String newUselessField) { uselessField = newUselessField; /* this keeps track of the last value used for uselessField */ storeLastValue(); } Problem: Wont Compile, get error: non-static method storeLastValue() cannot be referenced from a static context: storeLastValue();

32 Static Methods Problem: We must have setUselessField declared static for some reason. For some other reason we must have that storeLastValue is not declared static. But we cant access storeLastValue in method body setUselessField of as it is not static.

33 Static Methods Solution: public static void setUselessField(String newUselessField, SimpleClass obj) { uselessField = newUselessField; obj.storeLastValue(); } public void storeLastValue ( ) { lastValueField = uselessField; } Adds new argument Now refer to method via objects name.

34 `this’ keyword New Problem, the constructor method. public SimpleClass( ) { setUselessField("Some String", ????????); initialVal = uselessField; } What value goes here? Solution: keyword `this’ `this’ is a reference to the current object. The object whose method or constructor is being called.

35 `this’ keyword public SimpleClass( ) { setUselessField("Some String", this); initialVal = uselessField; } public void resetField() { setUselessField(initialVal, this); } public SimpleClass(String newUslessField) { setUselessField(newUslessField, this); } Reference with `this’ must then be introduced into other methods that call setUselessField:

36 `this’ keyword `this’ can also be used to make methods more readable. When a method argument is to be used to update an existing field, then it is often the case that the argument is given the same name as the field name. This leads to a conflict that can be resolved by using `this’. For example instead of: public static void setUselessField(String newUselessField, SimpleClass obj) { uselessField = newUselessField; obj.storeLastValue(); } we can have: public static void setUselessField(String uselessField, SimpleClass obj) { this.uselessField = uselessField; obj.storeLastValue(); }

37 Summing Up Every object has copies of the class fields and methods. We have seen example field values for multiple objects. Seen examples of methods for accessing and changing field values. We have seen how objects are created and manipulated during execution of the main method. Seen how static methods and fields are only created once within the class and are not copied to new objects.