Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.

Slides:



Advertisements
Similar presentations
Methods Java 5.1 A quick overview of methods
Advertisements

Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Operator Overloading and Type Conversions
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Overloading There is another aspect to polymorphism: Overloading Overloading is not overriding. In Turkish: Overridding: eskisini (geçersiz kılmak) Overloading:
ECE122 Feb. 22, Any question on Vehicle sample code?
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
1 CSC241: Object Oriented Programming Lecture No 25.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Function Overloading and References
Chapter -6 Polymorphism
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,
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Superclasses and Subclasses in Java Computer Science 3 Gerb Objective: Understand superclass methods and the “this” keyword in Java.
Classes, Interfaces and Packages
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
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 FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Constructors and Destructors
Chapter 9: Value-Returning Functions
Summary prepared by Kirk Scott
Chapter 6: User-Defined Functions I
Templates.
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Constructor & Destructor
Object Oriented Programming
Methods Additional Topics
Polymorphism.
Polymorphism.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
Constructors and Destructors
Method of Classes Chapter 7, page 155 Lecture /4/6.
Java Programming Language
Functions Imran Rashid CTO at ManiWeber Technologies.
Templates CMSC 202, Version 4/02.
Constructors & Destructors
Presentation transcript:

Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1

» Method overloading » Method overloading benefits » Constructor overloading » Using objects as parameter 24/2/2014 Ebtsam Abdelhakam 2

3 ● Method overloading is to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. ● Method overloading is one of the ways that Java supports polymorphism. ● When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. ● Thus, overloaded methods must differ in the type and/or number of their parameters. ● While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. ● When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call. 24/2/2014

4 Ebtsam Abdelhakam 24/2/2014

5 Ebtsam Abdelhakam 24/2/2014

6 Ebtsam Abdelhakam ● The value of overloading is that it allows related methods to be accessed by use of a common name. ● As you can see, test( ) is overloaded four times. ● The first version takes no parameters, the second takes one integer parameter, the third takes two integer parameters, and the fourth takes one double parameter. ● The fact that the fourth version of test( ) also returns a value is of no consequence relative to overloading, since return types do not play a role in overload resolution. 24/2/2014

7 Ebtsam Abdelhakam ● Method overloading is an applying to polymorphism concept. ● The value of overloading is that it allows related methods to be accessed by use of a common name. ● It is left to the compiler to choose the right specific version for a particular circumstance. ● For example, you can use the name sqrt to create methods that return the square of an integer and the square root of a floating- point value. 24/2/2014

Ebtsam Abdelhakam 8

9 ● As you can see, the Box(,, ) constructor requires three parameters. ● This means that all declarations of Box objects must pass three arguments to the Box() constructor. ● For example, the following statement is currently invalid: ● Box ob = new Box(); ● Since Box() requires three arguments, it’s an error to call it without them. ● Note that default constructor Box() can't be called, when you define your constructor. 24/2/2014

10 Ebtsam Abdelhakam ● In addition to overloading normal methods, you can also overload constructor methods. ● Constructor overloading: Same constructor declared with different parameters in the same class. ● Compiler differentiates which constructor is to be called depending upon the number of parameters and their sequence of data types. 24/2/2014

11 Ebtsam Abdelhakam 24/2/2014

12 Ebtsam Abdelhakam 24/2/2014

13 Ebtsam Abdelhakam 24/2/2014

14 Ebtsam Abdelhakam 24/2/2014

15 Ebtsam Abdelhakam ● One of the most common uses of object parameters involves constructors. ● Frequently, you will want to construct a new object so that it is initially the same as some existing object. ● To do this, you must define a constructor that takes an object of its class as a parameter. 24/2/2014

16 Ebtsam Abdelhakam 24/2/2014

17 Ebtsam Abdelhakam 24/2/2014

» Suppose by accessing one constructor, the programmer may require the functionality of other constructors also but by creating one object only. » "this()" is used to access one constructor from another "within the same class". Depending on the parameters supplied, the suitable constructor is accessed. 24/2/2014 Ebtsam Abdelhakam 18

» If included, this() statement must be the first one in the constructor. You cannot write anything before this() in the constructor. » With the above rule, there cannot be two this() statements in the same constructor (because both cannot be the first). » this() must be used with constructors only, that too to call the same class constructor (but not super class constructor). 24/2/2014 Ebtsam Abdelhakam 19