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,

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Java Software Solutions
Lecture 2: Object Oriented Programming I
CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Road Map Introduction to object oriented programming. Classes
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Enhancing classes Visibility modifiers and encapsulation revisited
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Java Unit 9: Arrays Declaring and Processing Arrays.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
A Closer Look at Methods and Classes. Overloading Methods In Java it is possible to define two or more methods within the same class that share the same.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
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.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
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.
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:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
CSI 3125, Preliminaries, page 1 Compiling the Program.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CSI 3125, Preliminaries, page 1 Generic Class & Generic methods.
Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
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.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Classes - Intermediate
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
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.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
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.
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 7 User-Defined Methods.
Chapter 11 Inheritance and Polymorphism
Constructor Overloading
Chapter 9 Inheritance and Polymorphism
Interface.
An Introduction to Java – Part I, language basics
Group Status Project Status.
Method of Classes Chapter 7, page 155 Lecture /4/6.
Classes, Objects and Methods
Corresponds with Chapter 5
Presentation transcript:

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, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java implements 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. 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.

CSI 3125, Preliminaries, page 2 Overloading Methods class popo { public static void main(String[] args) { check obj =new check(); obj.show(); obj.show(10); obj.show(1,2); } class check { int a,b; void show() { System.out.println("function with no argument"); } void show(int x) { a=x; System.out.println("functi on with one argument " +a); } void show(int x,int y) { a=x;b=y; System.out.println("functi on with 2 argu "+a+" "+b); }

CSI 3125, Preliminaries, page 3 Overloading Constructors More than one constructors with different no of arguments class popo { public static void main(String[] args) { check obj1 =new check(); check obj2 =new check(10); } class check { int a; check(){ System.out.println("no argument");} check(int x) { a=x; System.out.println("with one argument "+a); } }

CSI 3125, Preliminaries, page 4 Using Objects as Parameters Can pass object as method argument class popo { public static void main(String[] args) { check obj1 =new check(10,2); obj1.show(obj1); } class check { int a,b; check(int x,int y) { a=x;b=y;} void show(check p) { p.a=p.a+a; p.b=p.b+b; System.out.println(p.a+" "+p.b); }

CSI 3125, Preliminaries, page 5 Returning Objects Can return class object Addition of 2 complex num class popo { public static void main(String[] args) { comp obj1 =new comp(10,2); obj1.show(); comp obj2=new comp(2,4); obj2.show(); comp obj3=new comp(); obj3=obj2.add(obj1,obj2); obj3.show(); } class comp { int r,img; comp(){} comp(int x,int y) { r=x;img=y;} void show() { System.out.println(r+" + i "+img); } comp add(comp A,comp B) { comp t=new comp(); t.r=A.r+B.r; t.img=A.img+B.img; return t; }

CSI 3125, Preliminaries, page 6 Recursion Java supports recursion. Recursion is the process of defining something in terms of itself. Recursion that allows a method to call itself. A method that calls itself is said to be recursive. factorial of a number.

CSI 3125, Preliminaries, page 7 Recursion factorial of a number. class popo { public static void main(String[] args) { fact obj1 =new fact(); System.out.println("factorial of 5= "+ obj1.show(5)); } class fact { int n; int show(int x) { n=x; if(n==1) return 1; else return (n* show(n-1)); }

CSI 3125, Preliminaries, page 8 Access Control what parts of a program can access the members of a class. A member can be accessed is determined by the access specifier that modifies its declaration. Java supplies a rich set of access specifiers. Java’s access specifiers are public, private, and protected. Java also defines a default access level. protected applies only when inheritance is involved.

CSI 3125, Preliminaries, page 9 Access Control When a member of a class is modified by the public specifier, then that member can be accessed by any other code. When a member of a class is specified as private, then that member can only be accessed by other members of its class. main( ) has always been preceded by the public specifier. It is called by code that is outside the program—that is, by the Java run-time system.

CSI 3125, Preliminaries, page 10 Access Control When no access specifier is used, then by default the member of a class is public within its own package, but cannot be accessed outside of its package. Usually, want to restrict access to the data members of a class—allowing access only through methods. An access specifier precedes the member’s type specification. That is, it must begin a member’s declaration statement. private double j; private int myMethod(int a, char b) { //...

CSI 3125, Preliminaries, page 11 Access Control class popo { public static void main(String[] args) { check obj =new check(); obj.n=10;//error obj.show();//errror } class check { private int n; private void show() { System.out.println("Cannot access out side class"); }

CSI 3125, Preliminaries, page 12 Static Normally a class member must be accessed only in conjunction with an object of its class. It is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. Can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static because it must be called before any objects exist.

CSI 3125, Preliminaries, page 13 Static Methods Methods declared as static have several restrictions: ■ They can only call other static methods. ■ They must only access static data. ■ They cannot refer to this or super (inheritance) in any way. to call a static method from outside its class, do so using the following general form: classname.method( )

CSI 3125, Preliminaries, page 14 Static Methods static initialization block class check{ static int a = 3; static void fun() { System.out.println("a = " + a); } static { System.out.println("Static block initialized."); a = a * 4; } class popo_static { public static void main(String args[]) { check.fun(); System.out.println("a="+check.a); } }

CSI 3125, Preliminaries, page 15 Static Methods As soon as the check class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, a is initialized to a * 4 or 12. Then main( ) is called, which calls fun( )

CSI 3125, Preliminaries, page 16 Final A variable can be declared as final. Doing so prevents its contents from being modified. This means that must initialize a final variable when it is declared. class check{ final int a = 3; void fun() { System.out.println("a = " + a); } class popo_final{ public static void main(String args[]) { check obj=new check(); obj.fun(); } Cannot modify the final value

CSI 3125, Preliminaries, page 17 Array Array are implemented as objects. length instance variable. All arrays have this variable, and it will always hold the size of the array. class Length { public static void main(String args[]) { int a1[] = new int[10]; int a2[] = {3, 5, 7}; System.out.println("length of a1 is " + a1.length); System.out.println("length of a2 is " + a2.length); }} length of a1 is 10 length of s2 is 3

CSI 3125, Preliminaries, page 18 Nested and Inner Classes Class within another class; such classes are known as nested classes. The scope of a nested class is bounded by the scope of its enclosing class. if class B is defined within class A, then B is known to A, but not outside of A. A nested class has access to the members, including private members, of the class in which it is nested. However, the enclosing class does not have access to the members of the nested class.

CSI 3125, Preliminaries, page 19 Nested and Inner Classes 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. That is, it cannot refer to members of its enclosing class directly. Because of this restriction, static nested classes are rearly used.

CSI 3125, Preliminaries, page 20 Nested and Inner Classes An inner class is a non-static nested class. It has access to all of the variables and methods of its outer class and may refer to them directly in the same way that other non-static members of the outer class Thus, an inner class is fully within the scope of its enclosing class.

CSI 3125, Preliminaries, page 21 Nested and Inner Classes class Outer { int outerx; void test() { Inner inner = new Inner(); inner.display(); } // this is an inner class class Inner { void display(){ outerx=10; System.out.println("outerx = " + outerx); } class InnerClass { public static void main(String args[]) { Outer outer = new Outer(); outer.test(); }

CSI 3125, Preliminaries, page 22 String Class Every string is actually an object of type String. Even string constants are actually String objects. Strings can be constructed a variety of ways String myString = "this is a test"; Once you have created a String object, you can use it anywhere System.out.println(myString);

CSI 3125, Preliminaries, page 23 String Class Java defines one operator for String objects: +. It is used to concatenate two strings String myString = "I" + " like " + "Java."; results in myString containing “I like Java.” String strOb1 = "First String"; String strOb2 = "Second String"; String strOb3 = strOb1 + " and " + strOb2;

CSI 3125, Preliminaries, page 24 String Class The String class contains several methods test two strings for equality by using equals( ). obtain the length of a string by length( ) method. obtain the character at a specified index within a string by charAt( ). The general forms of these three methods are boolean equals(String object) int length( ) char charAt(int index)

CSI 3125, Preliminaries, page 25 String Class Eg String strOb1 = "First String"; System.out.println("Length of strOb1: " + strOb1.length()); System.out.println("Char at index 3 in strOb1: " + strOb1.charAt(3)); if(strOb1.equals(strOb1))

CSI 3125, Preliminaries, page 26 String Array String str[] = { "one", "two", "three" }; for(int i=0; i<str.length; i++) System.out.println("str[" + i + "]: " + str[i]);

CSI 3125, Preliminaries, page 27 Command-Line Arguments To pass information into a program when it run. Accomplished by passing command-line arguments to main( ). The information that directly follows with the program’s name on the command line when it is executed Information are stored as strings in the String array passed to main( ).

CSI 3125, Preliminaries, page 28 Command-Line Arguments class commandline { public static void main(String args[]) { for(int i=0; i<args.length; i++) System.out.println("args[" + i + "]: " +args[i]); } After compile java commandline this is a test 1 2 args[0]: this args[1]: is args[2]: a args[3]: test args[4]: 1 args[5]: 2

CSI 3125, Preliminaries, page 29