 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Java: How to Program Methods Summary Yingcai Xiao.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
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.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
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?
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 15,16 Java’s Methods.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 6 Methods Chapter 6 - Methods.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
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.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
“Form Ever Follows Function” Louis Henri Sullivan
Java Primer 1: Types, Classes and Operators
Methods Chapter 6.
Methods.
Chapter 6 Methods: A Deeper Look
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
Conversions of the type of the value of an expression
Chapter 6 Methods: A Deeper Look
6 Methods: A Deeper Look.
Methods.
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Corresponds with Chapter 5
SPL – PS3 C++ Classes.
Presentation transcript:

 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor Overloading

The static modifier associates a variable or method with its class rather than with an object of the class. Non – Static FieldsStatic Fields Also called as instance variablesAlso called as class variables Each object of the class will have its own copy of each of the instance variables that appear in the class definition. A class will have only one copy of each of its static fields, and these will be shared among all the objects of the class. Each object will have its own values for each instance variables. Each class variable exists even if no objects of the class have been created. If the value of a static field is changed, the new value is available equally in all the objects of the class. A static field or method is defined using the keyword static preceding the type name.

In the following code, the class “T2” has two variables x and y. The y is declared with the keyword “static”. In the main class “T1”, we try to manipulate the variables x and y in “T2”, showing the differences of instance variable and class variable.

Note that the static methods are called using class Names.

Argument Promotion:  Java will promote a method call argument to match its corresponding method parameter according to the promotion rules.  Values in an expression are promoted to the “highest” type in the expression (a temporary copy of the value is made).  Converting values to lower types results in a compilation error, unless the programmer explicitly forces the conversion to occur.  Place the desired data type in parentheses before the value Example: ( int ) 4.5

Basic scope rules:  Scope of a parameter declaration is the body of the method in which appears  Scope of a local-variable declaration is from the point of declaration to the end of that block  Scope of a local-variable declaration in the initialization section of a for header is the rest of the for header and the body of the for statement  Scope of a method or field of a class is the entire body of the class

Shadows field x Display value of local variable x

Shadows field x Display value of local variable x Display value of field x

In JAVA, it is possible to create methods that have the same name, but different parameter lists and different definitions. This is called method Overloading. When we call a method in an object, java matches up the method name first and then number and type of parameters to decide which one of the definitions to execute. This process is called polymorphism. Differences in return type are irrelevant in method overloading  Overloaded methods can have different return types  Methods with different return types but the same signature cause a compilation error

Correctly calls the “ square of int ” method Correctly calls the “ square of double ” method Declaring the “ square of int ” method Declaring the “ square of double ” method

Like methods, constructors can also be overloaded. Since the constructors in a class all have the same name as the class, />their signatures are differentiated by their parameter lists. Example: A cube class containing 2 constructors. Default and Parameterized

It is possible to use this() construct, to implement local chaining of constructors in a class. The this() call in a constructor invokes the an other constructor with the corresponding parameter list within the same class. Calling the default constructor to create a Cube object results in the second and third parameterized constructors being called as well. Java requires that any this() call must occur as the first statement in a constructor. Example: A cube class containing 3 constructors which demonstrates the this() method in Constructors