Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Inheritance Inheritance Reserved word protected Reserved word super
Modularity. Methods & Class as program unit A method is comprised of statement sequences and is often viewed as the smallest program unit to be considered.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Objects and Classes Objects and Classes objects defining classes method declaration object references and aliases instance variables encapsulation and.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Inheritance using Java
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
CSC 212 Object-Oriented Programming and Java Part 1.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Programming in Java CSCI-2220 Object Oriented Programming.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Interfaces and Inner Classes
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSII Final Review. How many bits are in a byte? 8.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Final and Abstract Classes
University of Central Florida COP 3330 Object Oriented Programming
Chapter 4: Writing Classes
Overloading and Constructors
Chapter 5 – Writing Classes
null, true, and false are also reserved.
Interface.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Introduction to Java Programming
Interfaces.
Introduction to Object-Oriented Concepts in Java
Chap 2. Identifiers, Keywords, and Types
Final and Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Java – Methods Lecture Notes 5

Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address, courses, grades, this is a state. On the other hand, the student has behaviors/methods, such that adding/changing course.

Objects Objects are defined by classes. A class can contain any number of data and methods - they are called members of a class. Methods allow the programmer to modularize a program. All variables declared in method definitions are local variables–they are known only in the method in which they are defined Most methods have a list of parameters that provide the means for communicating information between methods via method calls. A method’s parameters are also local variables.

Methods The formal method definition: access_modifiers method_modifiers return_type method_name( parameter_list ) { method body } A method has two major parts: method declaration (the first line) and method body. The method declaration defines all of the method's attributes, such as access level, return type, name, and arguments. The method body is where all the action takes place.

Classes and Objects All what is said below is true for fields and classes as well. access_modifiers You control which other classes have access to a method using one of four access levels: public, private, protected and package. An example package my_package; public class Point { int x, y; private int moves = 0; public void move(int dx, int dy) { x += dx; y += dy; moves++; }

Class Member Access A public class member is accessible throughout the package where it is declared and from any other package. A private class member is accessible only within the class body in which the member is declared. Subclasses cannot see private names declared in their parent classes. protected class members can be visible to subclasses or other classes within the same package. Eg: public class Shape extends Object{ protected x; } public class Circle extends Shape{ // have the access to x; } If none of keywords public, private, protected are specified, the method is not accessible in any other package.

Methods method name Method names should be verbs or verb phrases with the first letter lowercase and the first letter of any subsequent words capitalized, for example toLowerCase. field name Names of fields that are not final should be nouns or noun phrases with a lowercase first letter and the first letters of subsequent words capitalized.

Methods method_modifiers static method This is as a class method. A class method is always invoked without reference to a particular object. A method that is NOT declared static is called an instance method, and sometimes called a non-static method. abstract method This method has no implementation, but it does have a signature: name and number and type of parameters. final method This method cannot be overridden by subclasses. native method is implemented in platform-dependent code, typically written in another programming language. synchronized method We discuss this when we learn threads.

An Example public class Coin { // our public data final public int head = 0; final public int tail = 1; // our private data private int face; //result of flipping // methods public void flip() // Math.random() generate a float number between 0 and 1 { face = (int)(Math.random()*2); //casting is necesary } public int getFace() //since face is private we have to define a method { //which returns the result of flipping return face; }

An Example public class CountFlips { public static void main(String[] args) { final int NUM = 10000; // number of flips int counter=0; //instantiate(create) an object Coin mycoin = new Coin(); for(int i=1; i <= NUM; i++) { mycoin.flip(); if (mycoin.getFace() == mycoin.tail) //count tails counter++; } double prob = (double) counter/NUM * 100; System.out.println( "The probability of having a tail is " + prob); }