Method of Classes Chapter 7, page 155 Lecture 8 2019/4/6.

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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
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 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
COMP More About Classes Yi Hong May 22, 2015.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
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.
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,
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
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.
Introduction to Object-oriented Programming
Topic: Classes and Objects
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Classes (Part 1) Lecture 3
Chapter 7 User-Defined Methods.
The Lifetime of a Variable
C Functions -Continue…-.
Review What is an object? What is a class?
Java Programming: Guided Learning with Early Objects
CompSci 230 S Programming Techniques
Modern JavaScript Develop And Design
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Lecture 4-7 Classes and Objects
Chapter 14: More About Classes.
User-Defined Classes and ADTs
Chapter 5 - Functions Outline 5.1 Introduction
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Constructor Overloading
Chapter 9 Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
Chapter 6 Methods: A Deeper Look
Overloading and Overriding
Inheritance, Polymorphism, and Virtual Functions
Classes & Objects: Examples
CHAPTER 6 GENERAL-PURPOSE METHODS
Chapter 8: User-Defined Classes and ADTs
Constructors and Other Tools
Computer Programming with JAVA
User-Defined Classes and ADTs
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Tonga Institute of Higher Education
Chapter 8 Classes User-Defined Classes and ADTs
COP 3330 Object-oriented Programming in C++
Class.
Eighth step for Learning C++ Programming
CSE 341 Lecture 11 b closures; scoping rules
CS1100 Computational Engineering
Corresponds with Chapter 5
Classes and Objects Systems Programming.
Chapter 5 Classes.
Presentation transcript:

Method of Classes Chapter 7, page 155 Lecture 8 2019/4/6

Review Overloading Methods Using objects as parameters 2019/4/6 Overloading Methods Using objects as parameters Argument passing Returning objects Recursion Access control Understanding static Nested and inner classes String class

Overloading Methods 2019/4/6 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. Class overload { void test() { //no argument } void test(int a) { //one argument void test(int a, int b) { // two arguments

Example of four methods 2019/4/6

Overloading Constructors 2019/4/6 Constructor is to initialize the values

Example of three constructors 2019/4/6

Using Objects as parameters 2019/4/6 We could also pass objects to methods

Example 2019/4/6 ob1 will check with ob2 using equal(test o)

Argument passing 2019/4/6 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.

Call-by-value, there is no change in a and b in the lecture65{}, but change in test{} 2019/4/6

Call by object 2019/4/6

Returning the objects A method can return any type of data. 2019/4/6 A method can return any type of data. The return type can be class types or values

Example of returning an object, tmp 2019/4/6

Recursion 2019/4/6 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 = 1 + 1 n(2) = n(1) + n(0) 3 = 2 + 1 n(3) = n(2) + n(1) 5 = 3 + 2 n(0) =1, n(1) =1

Example of recursion 2019/4/6

Recursion - factorial Fact(n) means n*(n -1)*(n – 2)*(n-3)…3*2*1 2019/4/6 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)

Example of recursion, page 169 2019/4/6 n = 1, 2, 3, 4, 5 fact = 1, 2, 6, 24, 120

Access control 2019/4/6 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

Example – difference between public and private 2019/4/6 cannot directly access through a method

As a is defined as default, we can access it. Explanation 2019/4/6 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.

Understand static 2019/4/6 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()).

Example 2019/4/6 1 sequence of execution 2 4 3

Explanation 2019/4/6 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.

Example – without static 2019/4/6

Example – more 2019/4/6 output 1 4 2 3

A B Nested Classes We can define a class within another class. 2019/4/6 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. A B

More about nested class 2019/4/6 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.

Example of inner class 2019/4/6 2 3 1

Explanation The program will execute static void main()…. 2019/4/6 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.

Example of generating error 2019/4/6 here, y is decared outside the inner class. Thus it is not known here

String class 2019/4/6 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 a DCO student.”); “I am a DCO student” is a String. It can be rewritten as: String name =“ I am a DCO student.” System.out.println(name);

Example 2019/4/6

Summary Overloading Methods – two or more methods with the same class 2019/4/6 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