ECE122 Feb. 17. 2005. Introduction to Class and Object Java is an object-oriented language. Java’s view of the world is a collection of objects and their.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Department of Computer Science
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
COMP More About Classes Yi Hong May 22, 2015.
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes, Objects, and Methods
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
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
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
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?
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 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.
Classes One class usually represents one type of object –May contain its own member variables –May contain its own methods to operate on the member variables.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
© 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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
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.
Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Written by: Dr. JJ Shepherd
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.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
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.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
2015 Spring Content Class method [review] Access control
Topic: Classes and Objects
Java Primer 1: Types, Classes and Operators
Methods Chapter 6.
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
Classes and Objects in Java
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object-Oriented Programming
Variables and Their scope
Classes Lecture 7 from Chapter /1/11.
Anatomy of a Java Program
Classes One class usually represents one type of object
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Presentation transcript:

ECE122 Feb

Introduction to Class and Object Java is an object-oriented language. Java’s view of the world is a collection of objects and their interactions. Class is a template, or blueprint to build objects. Objects are instantiated (“created”) from the class specification.

What is a class? A class is a template that defines the form of an object. It specifies both the data and the code that will operate on that data. A class is a set of plans that specify how to build an object. A class is a logical abstraction. It is a blueprint that resides in the the Java class file. It has data: variables with certain data types. Some data are associated with each individual object, called instance variables. Other data are associated with each class, called class variables, which are identified by keyword “static”. It has methods: the actions that act on those data. The methods also have two types. Instance methods are associated with each object, and class methods (identified as “static”) are associated with the whole class.

General Forms of class definition class classname { //declare variables type var1; type var2; …. //declare methods type method(parameters) { //body of method, consists of statements } type method2(parameters) { //body of method, consists of statements } …. }

A class example public class Vehicle { //declare variables int passengers; //number of passengers int fuelcap; //fuel capacity in gallons int mpg; //fuel consumption in miles per gallon //omitted method declaration }

What is an object? An object is an instantiation of a class. According to blueprints specified in the class, an object has data (variables) and actions (methods) that act on those data. One or more object of the same class can be created in computer memory according to their blueprint, class.

How is an object created? Objects are created through operator “new”. Vehicle minivan; //declare reference to object minivan = new Vehicle(); //construct a Vehicle object in memory Step 1: declare a variable called minivan of the class type Vehicle. This is just a variable that can refer to an object. At the point, minivan contains value “null”, meaning it is not referring to any object yet. Step2: Allocate memory in computer and construct an object of type “Vehicle” in that memory segment. This is done by using operator “new”. Then, assign to “minivan” a reference to that object.

How do I access an object’s data? Object contains data that you can access with operator “.” in the form of object.variablefor instance variable e.g. minivan.passengers = 7; minivan.fuelcap = 16; minivan.mpg = 21; Or class.variablefor class variable e.g. boolean b = Vehicle.hasEngine; Use variable directly without operator “.” if you reference object data within a method of same object.

Primitive variable and assignment Assignment of primitive type is a straight- forward copy of value from one variable to another. E.g. int i = 1; int j = i;//the value contained in I is copied to j

Reference Variable and Assignment When you assign one reference variable to another reference variable, the second variable begins to reference the object the first reference variable is referencing to. v = minivan; //both v & minivan are objects of Vehicle class After this assignment, object v will reference the memory that object minivan references to. What happens to the memory block the v referenced to before the assignment? It depends. If there are other object variables reference it. That memory block stays untouched. If no more object variables reference it, it is no longer usable, and become a “garbage”. It will wait for the garbage collector to free the memory and recycle it back to system resource.

Illustration of Reference Variable Assignment v minivan v v=minivan; Garbage collected Memory block

Methods Methods are subroutines that act on object’s data. A method can call other methods of its own class, it can also call methods of other classes. Object interacts with each other through method calls. You can model the real world through object interactions. Instance method is created and associated with each object created. Class method is created when the class is loaded and class method is shared among all the objects of the same class.

General Form of a Method ReturnType MethodName(parameter-list) { //body of method } MethodName is the name of the method as a string with no space. Capitalize first letter of each word as a convention. ReturnType is any primitive type, or object reference. If nothing is returned from the method to the method-caller, a “void” is declared. Parameter-list is a comma separated list of type and variable pair. Each pair pass a value of certain data type to the method. The body of a method consists of statements.

Add methods to Vehicle Class Add an instance method to Vehicle class: void range() { System.out.println("Range is " + fuelcap*mpg + " miles."); } Add a class method to Vehicle class: static int calcRange(int fuelCap, int mpg) { int theRange = fuelCap * mpg; return theRange; }

Call a method Call an instance method objectReference.methodName(parameterlist); minivan.range(); Call a class method ClassName.methodName(parameterlist); Vehicle.calcRange(18,33); If the caller of the method is another method of the same object, just use the methodName, without operator dot.

Java methods pass by value A java method can use parameters. A method caller can passes arguments. The data type of argument normally is the same as that of the parameter. Otherwise, an explicit/implicit cast will happen. Java pass everything (primitive type & reference type) by VALUE. For primitive type, this means copy of value. For reference type, this means you cannot change the object the original argument variable reference to, but you can change the data contains within the original object.

Assignments Practice Vehicle.java Read “Java2” P Read “Head First Java” p26 – 29, 32-37,52- 57, 69-77