Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes.

Slides:



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

8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Author: Takdir, S.ST. © Sekolah Tinggi Ilmu Statistik.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
COMP More About Classes Yi Hong May 22, 2015.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Variable Scope Storage Class Recursion
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
C++ Lecture 2 Friday 11 July Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
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:
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Topics Instance variables, set and get methods Encapsulation
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.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Methods and Classes. Method Overloading Two or more methods within the same class that share the same name but different parameters class OverloadDemo.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.
Chapter 5: Enhancing Classes
Classes (Part 1) Lecture 3
Chapter 7 User-Defined Methods.
Examples of Classes & Objects
C Functions -Continue…-.
Java Programming: Guided Learning with Early Objects
CompSci 230 S Programming Techniques
Chapter 5 - Functions Outline 5.1 Introduction
Constructor Overloading
Overloading and Overriding
Classes & Objects: Examples
CHAPTER 6 GENERAL-PURPOSE METHODS
Method of Classes Chapter 7, page 155 Lecture /4/6.
Eighth step for Learning C++ Programming
Corresponds with Chapter 5
Classes and Objects Systems Programming.
Chapter 5 Classes.
Presentation transcript:

Lecture From Chapter 6 & /8/10 1 Method of Classes

Review Overloading Methods Using objects as parameters Argument passing Returning objects Recursion Access control Understanding static Nested and inner classes String class 2015/8/10 2

Overloading Methods Overloading means more than two methods with the same class that share the same name. This method is called method overloading. This is one of the ways that Java implements polymorphism. 2015/8/10 3 Class overload { void test() { //no argument } void test(int a) { //one argument } void test(int a, int b) { // two arguments }

Example of four methods 2015/8/10 4

Overloading Constructors Constructor is to initialize the values 2015/8/10 5

Example of three constructors 2015/8/10 6

Using Objects as parameters We could also pass objects to methods 2015/8/10 7

Example 2015/8/10 8

Argument passing In java, there are two ways of passing arguments, call-by-value and call-by-reference Call-by-value will pass the value to the method or subroutine. It will not modify the original data. Call-by-reference will pass the object to the method or subroutine. It might modify the original data depending on the method. 2015/8/10 9

Call-by-value, there is no change in a and b in the lecture65{}, but change in test{} 2015/8/10 10

Call by object 2015/8/10 11

Returning the objects A method can return any type of data. The return type can be class types or values 2015/8/10 12

Example of returning an object, tmp 2015/8/10 13

Recursion Recursion is the process of defining something in terms of itself. It is a method to call itself. For example, 1, 1, 2, 3, 5, 8, 13 etc. Here, 2 = n(2) = n(1) + n(0) 3 = n(3) = n(2) + n(1) 5 = n(0) =1, n(1) =1 2015/8/10 14

Example of recursion 2015/8/10 15

Recursion - factorial Fact(n) means n*(n -1)*(n – 2)*(n-3)…3*2*1 For example, n = 3, fact(3) = 3*2*1 = 6 n = 4, fact(4) = 4*3*2*1 = 24 The relationship is fact(1) = 1 fact(n) = n*fact(n-1) 2015/8/10 16

Example of recursion, page /8/10 17

Access control We can specify which part of program can be accessed. This will prevent misuse. There are three access specifiers that are used in Java. They are public, private and protected. public: that member can be accessed by any other code private: member can only be accessed by other members of its class 2015/8/10 18

Example – difference between public and private 2015/8/10 19

Explanation As a is defined as default, we can access it. b is also defined as public, we can also access it. c is defined as private and we cannot access it, we have to access it through a method. 2015/8/10 20

Understand static Sometimes, we want to define a class member that will be used independently of any object of that class. We used the definition of static (initial value). When a member is declared static, it can be accessed before any objects of its class are created. main() is declared as static. We can also declare a static block which gets executed first (before main()). 2015/8/10 21

Example 2015/8/10 22

Explanation While loading the program, this program will set i = 1 and j = 2, it executes the static block to compute the value of i (4 = 3 + 1) and j (6 = 2 + 4). It then executes main() and call String show() to display the value. 2015/8/10 23

Example – without static 2015/8/10 24

Example – more 2015/8/10 25

Nested Classes We can define a class within another class. This class is called nested class. The scope of a nested class is bounded by the enclosing class. If class B is defined within class A, then B is known to A, but not outside A. 2015/8/10 26 A B

More about nested class There are two types of nested classes: static and non-static. A static nested class is one which has the static modifier applied. Because it is static, it must access the members of its enclosing class through an object. The most important type of nested class is the inner class. 2015/8/10 27

Example of inner class 2015/8/10 28

Explanation The program will execute static void main()…. It then create a new object called outer(). Outer() then defines the value outer_val and call inner class. The inner class is to display the value of outer_val. 2015/8/10 29

Example of generating error 2015/8/10 30

String class String is the most commonly used class in Java’s class library. Every string that we created is an object of type String. System.out.println(“I am MSc student.”); “I am a MSc student” is a String. It can be rewritten as: String name =“ I am MSc student.” System.out.println(name); 2015/8/10 31

Example 2015/8/10 32

Summary Overloading Methods – two or more methods with the same class Using objects as parameters – pass object Argument passing – value and object Returning objects – return any data type Recursion – calling itself Access control – public, private and protected Understanding static- initial value Nested and inner classes – a class within a class String class – each string is an object 2015/8/10 33