Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 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.

2 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); }

3 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); } }

4 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); }

5 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; }

6 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.

7 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)); }

8 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.

9 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.

10 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) { //...

11 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"); }

12 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.

13 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( )

14 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); } }

15 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( )

16 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

17 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

18 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.

19 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.

20 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.

21 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(); }

22 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);

23 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;

24 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)

25 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))

26 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]);

27 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( ).

28 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

29 CSI 3125, Preliminaries, page 29


Download ppt "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,"

Similar presentations


Ads by Google