Terms and Rules Professor Evan Korth New York University (All rights reserved)

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.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Web Application Development Slides Credit Umair Javed LUMS.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
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.
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
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
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.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
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.
ECE122 Feb. 22, Any question on Vehicle sample code?
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
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.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
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.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
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.
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Class.
Java Programming Language
OO Programming Concepts
Creating and Using Classes
Chapter 7 Objects and Classes
Presentation transcript:

Terms and Rules Professor Evan Korth New York University (All rights reserved)

Class The main building blocks of Java programs. Defines objects of the same type. Like a blueprint. Every.java file has one or more classes. Exactly one of the classes must be a public class. –That class must have the same name as the.java file. If the class has an method called main(), execution can begin in that class. (Therefore, you can test a class by adding a main method to it.) If there are other classes in the file, they cannot be public classes.

Class modifiers No modifier (default) means the class is visible in the package in which it is declared. Public means it is visible to everything. There are two others (final and abstract) which we will discuss later in the semester.

Encapsulation Encapsulation refers to the the process of combining elements to create a new entity. You encapsulate the properties (attributes) and behaviors (activities) of an entity into a class. Encapsulation also enables us to hide the implementation of a class to other classes (information hiding).

Objects An object is an instance of a class. If we think of a class as a blueprint, an object is one model created from that blueprint. You can create any number of objects from one class. An object is distinctly identified by an object reference (except for anonymous objects).

Declaring object references In order to reference an object, we need an object reference variable. To declare an object reference variable we use the syntax: ClassName objectReferenceName; The above statement creates a variable objectReferenceName which can reference a Classname object. It does NOT create an object.

Instantiating objects In order to create an object, we use the new keyword along with a constructor for the class of the object we wish to create. To refer to the object, we “point” an object reference variable to the new object. objectReferenceName = new Constructor(); The decalration and instantiation can be combined as follows: ClassName objectReferenceName = new Constructor(); * More on constructors soon

Anonymous objects An object without a reference is called an anonymous object. It is created, used and immediately marked as garbage.

members A class declaration includes members of the class. A member can be either a data member or a method member. A data member (AKA field) is used to define state (attributes or properties) of the entity. A method member is used to define the behaviors of the entity.

Data members Data members can be a primitive type or a reference to another object. –Primitive types are integer types, float types and char, Boolean. (Note: an int is not the same as an object of type Integer) A data member can be either an instance variable or a static variable (also known as a class variable). The scope of a data member is the entire class it is declared in no matter where within the class it is declared.

Default values for data members 0 for all numeric type variables (including float types and integer types) \u0000 for char variables null for reference variables False for Boolean type variables Note: No default values for method variables.

Static variable (AKA class variable) A static variable has only one value no matter how many objects are instantiated from a class. The value is shared by all instances of the class. A static variable does not need an instance of the class in order to be accessed. You can access a static variable either with ClassName.varName (better style), or objectReference.varName notation. For static variables, every object of the class refers to the same memory location. Static variables can be accessed by static methods OR instance methods. The memory for a static variable is allocated when the class is loaded into memory.

Instance variables An instance variable has a unique value for each object of that class. –This does not mean that two objects cannot have the same value; it does mean that those values will be stored separately in memory. You can access an instance variable only with objectReference.varName notation. No memory is allocated until an object is instantiated. Can be accessed by instance methods only.

Data modifiers No modifier (default) means the data is visible in the package in which it is declared. Public means the data is visible to everything. Private means the data is visible only within the class in which it is defined. –Trying to access private data from another class will result in a compile time error. Static means it is a static variable. Static variables can use other modifiers as well. Final means the variable cannot be changed. There are two others (protected and abstract) which we will discuss later in the semester. There are still others which we will not discuss this semester.

Method members Methods are used to define the behaviors of an object. They can be overloaded. –Having more than one method in a class with the same name is referred to method overloading. –Each of the methods must have a different method signature. That is, they must have different argument lists.

Local method variables Do not automatically get initialized. –Using them without initializing them is a compilation error. Cannot have visibility modifiers. The scope of a local method variable starts where it is declared. It ends at the end of the block where it was declared.

Method modifiers No modifier means the method is visible in the package in which it is declared. Public means the method is visible to everything. Private means the method is visible only within the class in which it is defined. –Trying to call a private method from another class will result in a compile time error. Static means it is a static method. Static methods can use other modifiers as well. There are three others (final, protected and abstract) which we will discuss later in the semester. There are still others which we will not discuss this semester.

Static methods (AKA class methods) Can be called without an instance of the method. All the methods in the Math class are static methods which is why we can call them without a Math object. In fact we cannot instantiate an object of the Math class. You can call a static method either with ClassName.method (args) (better style), or objectReference.method (args) notation.

Instance methods Can only be called after an object is instantiated. You can call an instance method only with the objectReference.method (args) notation. An instance method acts on the specific instance for which it has been called.

Constructors Constructor are special methods that instantiate objects. A constructor is invoked with the new operator. A constructor: –should initialize the instance variables; –can also modify static variables; –can also do anything else but is usually used just for the above. A constructor does not have a return type. A constructor’s identifier (name) is the same as the class it constructs.

Constructors continued Constructors can be overloaded but each one must have its own signature. A constructor with fewer arguments can call a constructor with more arguments. A constructor with no arguments is called a default constructor. –If no constructor is defined, a default constructor is automatically supplied. Variables are initialized to their default values. –If one constructor is explicitly defined, the automatic default constructor is no longer available. In such case, if you want a default constructor, you must define it yourself.

Accessor methods When a data member is declared to be private, we still need a way to refer to that data. A method used to change or retrieve a private data item is referred to as an accessor method. Two kinds of accessor methods are the get method and the set method.

Get methods A method that is used to retrieve the value of a data object is referred to as a get method. Also known as a getter. Get method header should look like this: public returnType getPropertyName () It may just return a data field or it may calculate the value. Remember information hiding.

Predicate methods A get method that returns a Boolean value should have a header like this: public boolean IsProperty () It can simply return a Boolean data field or it can use a Boolean formula to calculate it’s data. Remember, information hiding!

Set methods Methods used to set or change the value of a data method are referred to as set methods. Also known as setters and mutators. Header of set method will look like this: public void setProp (propType)

Garbage collection When an object is no longer referenced by any reference variable, that object is referred to as garbage. Java automatically tracks garbage objects and returns the memory to the operating system when the garbage collector runs. We do not have direct control over when the garbage is collected. We can suggest to the compiler to collect garbage but it is not guaranteed that it will run. To suggest garbage collection we make the following method call: –System.gc();

Keyword this The keyword this is used with a class to refer to the specific instance of the class that is being used. A variable in a class’ method that has the same name as a field will “shadow” the field. You can access the field using the this keyword. This (args) in a constructor will invoke another constructor of that class. –If you call another constructor from a constructor, it must be the first line in the calling constructor. You cannot use the this keyword in static methods. (why?)

Passing variables to methods All variables in Java are passed using call by value. However, since object variables are really references to objects, passing an object is simulated pass by reference. –Objects passed to a function and modified by that function will have the changes reflected in the calling function. –Primitive variables passed to a function and modified by that function will NOT have the changes reflected in the calling function.

Principle of least privilege You should pick the modifier that allows the least privilege for other classes while allowing your code to do what it needs to do. This helps reduce debugging time by localizing potential problem areas.

Exam Our only midterm will be given the week of October 20 th –Either the 21 st or the 23 rd