CLASS DEFINITION (FIELDS)

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

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.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Road Map Introduction to object oriented programming. Classes
. Plab – Tirgul 6 Classes. Point.h class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
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.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
ECE122 Feb. 22, Any question on Vehicle sample code?
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Introduction to Object Oriented Programming Chapter 10.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Procedural and Object-Oriented Programming
Topic: Classes and Objects
GC211 Data structure Lecture 3 Sara Alhajjam.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Constructor & Destructor
This pointer, Dynamic memory allocation, Constructors and Destructor
Building Java Programs
Building Java Programs
Object Oriented Programming
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Object Oriented Programming in java
Java Programming Language
Chapter 9 Objects and Classes Part 01
Building Java Programs
Building Java Programs
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Constructors & Destructors
More C++ Classes Systems Programming.
Inheritance in C++ Inheritance Protected Section
Creating and Using Classes
Chapter 7 Objects and Classes
SPL – PS3 C++ Classes.
Chapter 5 Classes.
Presentation transcript:

CLASS DEFINITION (FIELDS) public class Point { public int x; public int y; } Point p1 = new Point(); // currently x = 0, y = 0 Point p2 = new Point(); // currently x = 0, y = 0 p1.x = 16; p1.y = 32; p2.x = 25; p2.y = 50; System.out.println("(" + p1.x + ", " + p1.y + ")"); // (16, 32) System.out.println("(" + p2.x + ", " + p2.y + ")"); // (25, 50) Again, every instance has a distinct copy of fields

CLASS DEFINITION (FIELDS) package java.util; public class Point { int x; // Is protected by default (can only be read by java.util.*) int y; // Is protected by default (can only be read by java.util.*) } // Meanwhile, in another package package edu.just.cpe.MyNeatProject; Point p1 = new Point(); Point p2 = new Point(); p1.x = 16; // NOP. Compile error. p1.y = 32; // NOP. Compile error. p2.x = 25; // NOP. Compile error. p2.y = 50; // NOP. Compile error. System.out.println("(" + p1.x + ", " + p1.y + ")"); // ERROR. System.out.println("(" + p2.x + ", " + p2.y + ")"); // ERROR. Non-private fields can be read from an instance outside of the defining class, at a program point with field access

Constructors When you say new you allocate space for an object’s data. The result is an instance of your class, which has a unique personal copy of variables and shares the same behaviors (methods) -Implicitly, this also calls the class’s constructor

CLASS DEFINITION (CONSTRUCTOR) public class Point { private int x; private int y; public Point(int mx, int my) { x = mx; // assign parameter to field y = my; // assign parameter to field } Point p1 = new Point(3, 4); // currently, x = 3, y = 4 Point p2 = new Point(4, 8); // currently, x = 4, y = 8 Point p3; // p3 = null, no object constructed Constructors are special methods which are invoked after object creation (after new) By the time the constructor executes, the fields already have their default values (never uninitialized)