Class Fundamentals BCIS 3680 Enterprise Programming.

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 3 Introduction to Classes, Objects, Methods, and Strings
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Road Map Introduction to object oriented programming. Classes
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)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
LESSON 4 - Variables JAVA PROGRAMMING. Variables Variable is a named storage location that stores data and the value of the data may change while the.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Topics Instance variables, set and get methods Encapsulation
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Topic: Classes and Objects
Classes (Part 1) Lecture 3
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Java Primer 1: Types, Classes and Operators
Creating Your OwnClasses
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Chapter 9 Objects and Classes
Object Oriented Programming in java
Java Programming Language
Java Basics Data Types in Java.
Chap 2. Identifiers, Keywords, and Types
Chapter 7 Objects and Classes
CSG2H3 Object Oriented Programming
Presentation transcript:

Class Fundamentals BCIS 3680 Enterprise Programming

Overview 2  Building Your Own Classes  Members of a class  Variables (instance and static)  Methods (instance and static)  Storing and accessing values in an object  Constructor methods  Getter and setter methods

Class 3  A class defines a number of properties (variables) and actions/behaviors (methods).  An object created from this class will have those variables and methods.  Variables are used to store data.  Methods are used to specify action.

Object vs. Class 4  Object reference  Identifier (name) of the object  Holds the address of the object  A class is a template for making objects.  An object is an instance of a class.  The process of creating an object out of a template (class) is called instantiation, “creating an instance of the class”, etc.  Remember the naming convention:  The name of a class starts with an uppercase letter.  The name of a variable starts with a lowercase letter.

Creating Objects 5  Two things required to create an object:  Declaring an object reference for the new object, ClassName objRef; e.g., Student cobStudent;  Using the new keyword to actually create the object, objRef = new ClassName([arguments]); e.g., cobStudent = new Student("ITDS", "Undergrad");  The two steps may be combined into one: ClassName objRef = new ClassName([arguments]); Student cobaStudent = new Student("ITDS", "Undergrad"); // One step

Using Objects 6  To use an object’s variables and methods, you use the “.” operator (dot notation). objectName.variableName cobStudent.dept = "Marketing"; objectName.methodName([arguments]); cobStudent.regClass("BCIS3680");

Instance Variable 7  Instance variables are used to store data specific to a particular object.  An instance variable doesn’t exist (no memory location is assigned to it) until an object of that class is created.  All objects of the same class have the same instance variables.  However, for each object, the variables store values that are unique to that object.  Example:  The Student class has an instance variable called untId.  alice and bob are two instances of the Student class.  In the alice object, the untId variable contains the value  In the bob object, the untId variable contains the value

Static Variable 8  A class may also contain static variables.  Once a static variable is defined in the class, it comes into being immediately. No instantiation is needed.  Static variables can be accessed from outside of the class by following the ClassName.staticVarName syntax.  Values stored in static variables are not tied to any particular instances of the class. Rather, they are relevant at the class level.  Example:  The Student class has a studentCount static variable.  The number of all Student objects ever created can be stored in the studentCount variable.  It is not related to any particular Student object. If it were, we could have a “the-chicken-or-the-egg” problem.

Instance Method 9  Instance methods are used to define actions that an object of the class is capable of performing.  An instance method cannot be called until an object of that class is created.  An instance method is defined the same way for objects of the same class. However, even if the action is the same, in each object, the action works on data unique to that object (stored in the respective instance variables).  Example:  The Student class has an instance method called showStudentId().  alice and bob are two instances of the Student class.  When called on the alice object, the showStudentId() method displays the value  When called on the bob object, the showStudentId() method displays the value

Static Method 10  A class may also contain static methods.  Once a static method is defined in the class, it comes into being immediately. No instantiation is needed.  Static methods can be accessed from outside of the class by following the ClassName.staticMethod() syntax.  It can be used to perform action that is relevant to all instances of the class.  Example:  The Student class has an addToCount() static method and a getStudentCount() static method.  The addToCount() method increments the student count whenever a new student object is instantiated.  The getStudentCount() method displays the current count of student objects.

Members of A Class 11

The OOP “PIE” 12

Encapsulation  A fundamental concept in OOP.  The values of instance variables inside a class shouldn’t be changed arbitrarily by other classes.  They should be declared as “private”.  Manipulation of their values are done only by special-purpose methods inside the class.  These methods are public and “exposed”.  Setting values is possible only through calling these methods.  If program logic requires that these values to be changed, regardless of where the program execution is done, these special-purpose methods must be called. 13

Encapsulation  For example, suppose the main method is inside the Driver class and it needs to set the value of studentID of a Student object called alice,  It shouldn’t do: alice.studentID = "12345";  Instead, the setter method of that property should be run: alice.setStudentID("12345");  Whether a field or method is accessible (visible) to other classes is controlled by access modifiers.  public (UML sign + ) – All other classes can access the member.  private (UML sign - ) – Other classes must access the member through special methods defined in this class. 14

UML Representation of a Class + public - private 15

Access Modifiers Visibility PublicProtected Private Within the same class Yes From any class in the same package Yes No From a subclass in the same package Yes No From a subclass outside the package YesYes*No From any non-subclass class outside the package YesNo * Through inheritance 16

Storing Values to Instance Variables  Constructor Methods  A constructor is a special method that is run when an object is created. It is typically used to set initial (default) values for instance variables.  A class may have more than one constructor, i.e., constructors often are overloaded.  Setter (mutator) Methods  A setter method is a void method that performs an action (set the value of an instance variable).  It takes a parameter – the value to be stored in the variable.  It does not return a value. 17

Accessing Values of Instance Variables 18  Getter (Accessor) Methods  A setter method is a value-returning method that returns a value (the value of an instance variable).  It does not have parameters.

Constructor  When an object is instantiated, the instantiation process runs the class’ constructor method.  If the business rules of your application require other initial values for (some of all) variables in your class, then you need to write a constructor and assign initial values to those that need them.  A constructor may also perform some initial actions in addition to or instead of setting initial values for variables.  Besides initializing instance variables, a constructor may also work with static variables/methods of the class.  A constructor must use the same name and capitalization as the name of the class. 19

Default Initial Values  If you haven’t defined a constructor for the class, Java will create a default constructor for you Data TypeDefault Value byte, short, int, long 0 float, double 0.0 charspace boolean false Any object reference (e.g., String) null 20

Overloaded Constructor  Often, even though objects are instantiated from the same class, you want their variables to be initialized differently or different sets of variables to be initialized.  To achieve that, you write a few different constructors accordingly.  Constructors often are overloaded. 21

Setter Method 22  The program logic may need to change the value of an instance variable after it is initialized by the constructor. To do that, the setter method of that variable needs to be called.  The design pattern of a setter:  Access modifier: public  Return value: void  Naming: setVarName  Parameter list: one and only one parameter – (dataType paramName)  Method body: although more complicated assignment may be needed sometimes, typically the method contains only one assignment statement that assigns the parameter value to the variable – varName = paramName;

Getter Method 23  A getter method retrieves the value of an instance variable on behalf of methods in external classes.  The design pattern of a getter:  Access modifier: public  Return value:  Naming: getVarName  Parameter list: ()  Method body: although more complicated actions may be needed sometimes, typically the method contains only one statement that returns the value of the variable – return varName;

Access Method Examples  Example: // Declare the property private double unitPrice; … // The setter method public void setUnitPrice (double price) { unitPrice = price; } // The getter method public double getUnitPrice { return unitPrice; } 24

The this Keyword  If inside a constructor or setter method, the parameter name is the same as the name of the instance variable that will be assigned the parameter’s value, there should be a way to differentiate the “namesakes.”  The this keyword is used to refer to the object itself.  Variable name preceded by this and a dot refers to the instance variable itself.  Variable name standing by itself refers to the parameter. public void setUnitPrice (double unitPrice) { this.unitPrice = unitPrice; } 25