5.1 Basics of defining and using classes. 5.1.1 A review of class and object definitions A class is a template or blueprint for an object A class defines.

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
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.
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.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
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.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Java Programming, 4E Y. Daniel Liang.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
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.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
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.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Topics Instance variables, set and get methods Encapsulation
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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
Static data members Constructors and Destructors
Examples of Classes & Objects
Java Primer 1: Types, Classes and Operators
Road Map Introduction to object oriented programming. Classes
Creating Your OwnClasses
Object Based Programming
Chapter 9 Inheritance and Polymorphism
Classes & Objects: Examples
Defining Classes and Methods
Chapter 6 Objects and Classes
OO Programming Concepts
Chapter 6 Objects and Classes
ITE “A” GROUP 2 ENCAPSULATION.
Chapter 7 Objects and Classes
Presentation transcript:

5.1 Basics of defining and using classes

5.1.1 A review of class and object definitions A class is a template or blueprint for an object A class defines an object’s behavior & attributes An object knows something & how to do something An object stores information about what it knows When an object is created from a class it is called an instance of the class Objects are referenced by reference variables Object behavior is controlled by methods

5.2.1 Designing classes

5.2.2 Using classes Use the class keyword to define the class, along with attributes and methods Two steps to create an object: Declare a reference variable, then use the new operator to create an instance of the class Use the dot operator to access methods and attributes of the object

5.3.1 Attributes

5.3.2 Variables Instance variables hold data that is associated with each object, or instance of the class Class data is held in static variables. The class has only one copy of the class data, and it is available to all objects created from the class. To make a value immutable, use the final keyword.

5.4.2 Encapsulation & Access modifiers Encapsulation is the concept of hiding the internal details of an object Access modifiers are used to control access to object or class data

5.5.1 The constructor process Objects are constructed when the new operator is used An objects instance variables are initialized to default variables Method variables are not assigned default values

5.5.2 Constructors Constructor methods are called when the object is created If the class does not define a constructor, the compiler inserts one that does nothing If a class does include a constructor, the compiler does not insert one

5.6.1 Methods & method signatures An object invokes the method of another object to calling it using the method signature The method signature consists of the method name and the arguments and the return type The type of value returned by a method is declared in the method signature To declare that a method returns no value, use the void keyword

5.6.6 Concept of pass-by-value When a method is called, the method receives a copy of the argument Therefore the value can be altered by the method without affecting the original However, if the argument is a reference to an object, the method receives a copy of the reference This means that the method can access the object’s non-private data

5.6.7 Method types Instance methods are methods that are created when the object is created Class methods exist once the class is loaded, not as a part of an instance The static keyword is used to declare methods as class methods A special class method is the main method, which is used as an entry point for the JVM public static void main ( String args[ ] )

5.7.1 Using this in constructors and methods Every instance method has a variable called this this is a reference to the current object

5.8.2 Encapsulation & overloading Encapsulation hides the details of a class In general, instance variables should be private and methods should be public Hidden instance data can be altered and retrieved using getter and setter methods Overloading involves having several methods with the same name The JVM knows the correct method to use by the number and type of the arguments

Initializing data Instance variables are initialized with a default value based on the data type Instance variables can be explicitly initialized when they are declared Initialization blocks are blocks of statements that can initialize instance variables Initialization blocks can be static or non-static

Scope of variables Instance data is available as long as the object is in use or a reference to it exists Class data exists as soon as the class is loaded, and before any objects are created Method variables only exist while the method is in use, and only in the block where they were declared Arguments are available for the life of the method

Finalizers All objects have a finalizer method, that is called just before the JVM disposes of the object Any clean-up code can be included in the finalizer method Because the garbage collection is automatic, use of the finalizer method is often not necessary