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.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 22 Implementing lists: linked implementations.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
Chapter 6 Data Types
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Road Map Introduction to object oriented programming. Classes
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
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.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Ch 4. Memory Management Timothy Budd Oregon State University.
OOP Languages: Java vs C++
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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.
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.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
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.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Defining a ClasstMyn1 Defining 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.
© 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.
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.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
CSI 3125, Preliminaries, page 1 Compiling the Program.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Chapter 6 Methods Chapter 6 - Methods.
Classes, Interfaces and Packages
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Learners Support Publications Constructors and Destructors.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Constructors and Destructors
Topic: Classes and Objects
Static data members Constructors and Destructors
Unit-2 Objects and Classes
Classes and Objects in Java
University of Central Florida COP 3330 Object Oriented Programming
Classes and Objects in Java
group work #hifiTeam
Constructor Overloading
Introduction to Classes
Object Oriented Programming in java
Constructors and Destructors
Dynamic Memory.
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

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 new data type. Once defined, this new type can be used to create objects of that type. Thus, a class is a template for an object, and an object is an instance of a class. Because an object is an instance of a class, you will often see the two words object and instance used interchangeably.

CSI 3125, Preliminaries, page 3 The General Form of a Class When define a class, declare its exact form and nature. Specifying the data that it contains and the code that operates on that data. A class is declared by use of the class keyword. The general form of a class definition is shown here:

CSI 3125, Preliminaries, page 4 The General Form of a Class class class name { type instance variable; type methods(parameter list) { method body; }

CSI 3125, Preliminaries, page 5 The General Form of a Class The data or variables defined within a class are called instance variables Methods and variables defined within the class are called members in the class All methods have the same form as main()

CSI 3125, Preliminaries, page 6 The Class Example class check { int a,b,c; } Here class called check that defines 3 instance variables : a,b & c Here class check defines a new type of data The new data type is called check Class declaration is only a template To create check object Check obj = new check();

CSI 3125, Preliminaries, page 7 The General Form of a Class After this statement executes, obj will be an instance of check. the new operator dynamically allocates memory for an object. Check obj = new check(); The class name followed by parentheses specifies the constructor for the class. A constructor defines what occurs when an object of a class is created.

CSI 3125, Preliminaries, page 8 Add methods in the class Syntax type method name(parameter list) { method body; } -- Methods with arguments Methods with return type

CSI 3125, Preliminaries, page 9 Constructors Initialized the class variables It has the same name as the class in which it resides and is syntactically similar to a method Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes. They have no return type, not even void.

CSI 3125, Preliminaries, page 10 Constructors eg class check { int a,b; check(){a=b=10;} void show(){system.out.println(“a=“+a+”b=“+b); } … Check obj=new check(); Then the constructor method automatically executed

CSI 3125, Preliminaries, page 11 Parameterized Constructors Constructor method with parameters Theses value can be passed when object is created and initialized to class variables eg class check { int a,b; check(int x, int y ){a=x; b=y;} void show(){system.out.println(“a=“+a+”b=“+b); } … check obj=new check(10,20); Then the constructor method assigns a=10 and b=20

CSI 3125, Preliminaries, page 12 The this Keyword Method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. This can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. Can use this anywhere a reference to an object of the current class’ type is permitted.

CSI 3125, Preliminaries, page 13 The this Keyword eg class check { int a,b; check(int x, int y ){this.a=x; this.b=y;} void show(){system.out.println(“a=“+a+”b=“+b); } … check obj=new check(10,20);

CSI 3125, Preliminaries, page 14 Instance Variable Hiding it is illegal in Java to declare two local variables with the same name When a local variable has the same name as an instance variable, the local variable hides the instance variable. This is why a & b were not used as the names of the parameters to the check ( ) constructor inside the check class. If they had been, then a would have referred to the formal parameter, hiding the instance variable a. check(int a, int b ){this.a=a; this.b=b;}

CSI 3125, Preliminaries, page 15 Garbage Collection Since objects are dynamically allocated by using the new operator how such objects are destroyed and their memory released for later reallocation. In C++, dynamically allocated objects must be manually released by use of a delete operator. in Java automatically deallocate. The technique is called garbage collection. It works like this: when no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed. There is no explicit need to destroy

CSI 3125, Preliminaries, page 16 The finalize( ) Method By using finalization, can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector. To add a finalizer to a class, simply define the finalize( ) method. The Java run time calls that method whenever it is about to recycle an object of that class. Inside the finalize( ) method, specify those actions that must be performed before an object is destroyed. The garbage collector runs periodically, checking for objects that are no longer referenced by any running state or indirectly through other referenced objects. Right before an asset is freed, the Java run time calls the finalize( ) method on the object.

CSI 3125, Preliminaries, page 17 The finalize( ) Method The finalize( ) method has this general form: protected void finalize( ) { // finalization code here } The keyword protected is a specifier that prevents access to finalize( ) by code defined outside its class. It is important to understand that finalize( ) is only called just prior to garbage collection. It is not called when an object goes out-of-scope, for example. This means that you cannot know when—or even if— finalize( ) will be executed.