 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
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.
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)
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
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.
Internet Software Development Classes and Inheritance Paul J Krause.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
ECE122 Feb. 22, Any question on Vehicle sample code?
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.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Session 7 Methods Strings Constructors this Inheritance.
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:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
What is an Object? Real world objects are things that have: 1) state 2) behavior Example: your dog: 1) state – name, color, breed, sits?, barks?, wages.
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.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Classes, Interfaces and Packages
Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
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.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
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++
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.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Topic: Classes and Objects
Review What is an object? What is a class?
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
Constructor & Destructor
Constructor Overloading
Classes and Objects.
Defining Classes and Methods
Defining Classes and Methods
Chapter 4 Constructors Section 4.4
Classes and Objects CGS3416 Spring 2019.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Constructors & Destructors
CMSC 202 Constructors Version 9/10.
Presentation transcript:

 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects

 A constructor initializes the instance variables of an object.  It is called immediately after the object is created. ◦ it is syntactically similar to a method: ◦ it has the same name as the name of its class ◦ it is written without return type; the default return type of a class constructor is the same class  When the class has no constructor, the default constructor automatically initializes all its instance variables with zero.

 So far, all boxes have the same dimensions.  We need a constructor able to create boxes with different dimensions:

CodeOut Put

 A constructor helps to initialize an object just after it has been created.  In contrast, the finalize method is invoked just before the object is destroyed: ◦ implemented inside a class as: protected void finalize() { … } ◦ implemented when the usual way of removing objects from memory is insufficient, and some special actions has to be carried out  How is the finalize method invoked?

 Garbage collection is a mechanism to remove objects from memory when they are no longer needed.  Garbage collection is carried out by the garbage collector: ◦ The garbage collector keeps track of how many references an object has. ◦ It removes an object from memory when it has no longer any references. ◦ Thereafter, the memory occupied by the object can be allocated again. ◦ The garbage collector invokes the finalize method.

 Keyword this allows a method to refer to the object that invoked it.  It can be used inside any method to refer to the current object:  The above use of this is redundant but correct.  When is this really needed?

 Variables with the same names: ◦ it is illegal to declare two local variables with the same name inside the same or enclosing scopes ◦ it is legal to declare local variables or parameters with the same name as the instance variables of the class.  As the same-named local variables/parameters will hide the instance variables, using this is necessary to regain access to them:

 It is legal for a class to have two or more methods with the same name.  However, Java has to be able to uniquely associate the invocation of a method with its definition relying on the number and types of arguments.  Therefore the same-named methods must be distinguished: ◦ by the number of arguments, or ◦ by the types of arguments

 No parameters  a: 10  a and b:  double a:  Result of ob.test(123.25):

 Different result types are insufficient.  The following will not compile:

 When an overloaded method is called, Java looks for a match between the arguments used to call the method and the method’s parameters.  When no exact match can be found, Java’s automatic type conversion can aid overload resolution:

 In the languages without overloading, methods must have a unique names:  Java enables logically-related methods to occur under the same name:

 Why overload constructors? Consider this:  All Box objects can be created in one way: passing all three dimensions.

 Three constructors: 3-parameter, 1-parameter, parameter-less.

 So far, all method received arguments of simple types.  They may also receive an object as an argument. Here is a method to check if a parameter object is equal to the invoking object:

ob1 == ob2: true ob1 == ob3: false

 A special case of object-passing is passing an object to the constructor.  This is to initialize one object with another object:

 Two types of variables: ◦ simple types ◦ class types  Two corresponding ways of how the arguments are passed to methods: ◦ by value a method receives a copy of the original value;parameters of simple types ◦ by reference a method receives the memory address of the original value, not the value itself; parameters of class types

 Passing arguments of simple types takes place by value:

 With by-value argument-passing what occurs to the parameter that receives the argument has no effect outside the method:

 Objects are passed to the methods by reference: a parameter obtains the same address as the corresponding argument:

 As the parameter hold the same address as the argument, changes to the object inside the method do affect the object used by the argument:

 So far, all methods returned no values or values of simple types.  Methods may also return objects:

 Each time a method incrByTen is invoked a new object is created and a reference to it is returned:

Questions