Your SlideShare is downloading. ×
Chapter 6Classes And Method                 http://www.java2all.com
Method Overloading                 http://www.java2all.com
•     A class can contain any number of methods.  Methods can be with parameter and without  parameter.•     The parameter...
•       Overloading methods demonstrate the concept    of polymorphism.•      When an overloaded method is invoked, java  ...
•      When java encounters a call to an overloaded    method, it simply executes the version of the    method whose param...
public class MethodOver{       int n1;       int n2;       MethodOver()       {            n1 = 10;            n2 = 20;   ...
public class MethodOver{       int n1;       int n2;       MethodOver()       {            n1 = 10;            n2 = 20;   ...
Constructors overloading                      http://www.java2all.com
Constructors      Along with method overloading, we canalso overload constructors.      Constructors having the same name ...
class Point{     int x;     int y;     Point(int a, int b)     {          x = a;          y = b;     }}class Circle{     i...
Circle(int x1, int y1, int r)    {         originX = x1;         originY = y1;         radius = r;    }    Circle(Point p,...
Circle(int x1, int y1, int r)    {         originX = x1;         originY = y1;         radius = r;    }    Circle(Point p,...
Above program is quite complicated here i amgiving you perfect flow of program.      First of all note one thing thatnewCl...
Next due to syntax Circle c2 = newCircle(10,20,5); constructor which has 3 arguments willbe called for object c2 so we get...
More About Method                http://www.java2all.com
1. Passing Objects as a Parameter to Method.      We have seen that methods can take parameters as input and process them....
class PassObj{     int n1;     int n2;      PassObj() // constructor     {          n1 = 0;          n2 = 0;     }     Pas...
2. Method overloading with object as a parameter class MetObjOv {      int n1;      int n2;     // constructor     MetObjO...
void multiply(MetObjOv p1, MetObjOv p2)    {         n1 = p1.n1 * p2.n1;         n2 = p1.n2 * p2.n2;         System.out.pr...
3. Return an Object.  A method can return any type of  data, including class type (object)           that you create.     ...
class RetObj{     int n1;     int n2;    // constructor    RetObj()    {         n1 = 0;         n2 = 0;    }    RetObj(in...
public static void main(String args[])    {        RetObj obj1 = new RetObj(5,6);        RetObj obj2 = new RetObj(6,5);   ...
Call By Value            http://www.java2all.com
Now we all know that how to define and call themethods.There are two types of calling method and those are1. call by value...
This method copies the value of an argumentinto the formal parameter of the subroutine.     Therefore, changes made to the...
public class CallBy_Value{   public static void main(String[] args)   {     Value v = new Value(10,20);     System.out.pri...
You can see that after calling method we changevalue of a and b but it will not affect the original value ofclass` members...
Call By Reference                    http://www.java2all.com
Here we pass reference as parameter infunction calling.      We all know that reference means object so wepass object as p...
When we pass an object to a method, thesituation changes, because objects are passed bycall-by-reference.      When we cre...
public class CallBy_Reference{   public static void main(String[] args)   {              Reference r = new Reference(10,20...
You can see that after calling method valueof original a and b is changed because of call byreference.     Here we pass "r...
Recursion            http://www.java2all.com
Recursion• Recursion is the process of defining something  in terms of itself.• When function call it self is called recur...
import java.util.Scanner;class Factorial{    int fact(int n) // this is a recursive function   {      int result;      if(...
•       Here the method fact is recursive because it    calls itself.•      The whole process something like this•      re...
Nested Class               http://www.java2all.com
It is possible to define a class within anotherclass; such classes are known as nested classes.     The scope of a nested ...
There are two types of nested classes:1.   Static2.   Non – StaticStatic nested class :-       A static nested class is on...
Non – Static nested class :-         Non – Static nested class is known as innerclass.      It has access to all of its va...
class Inner1{     class Contents     {          private int i = 16;          public int value()          {              re...
public static void main(String args[])    {          Inner1 p = new Inner1();          p.ship("Congo"); //call ship method...
Let us see one more example but here theprogram will not compileclass Outer{  int outer_x = 100;  void test()  {     Inner...
class InnerClassDemo{    public static void main(String args[])  {     Outer outer = new Outer();     outer.test();  }}   ...
Command Line  Argument               http://www.java2all.com
Sometimes you will want to pass informationinto a program when you run it. This isaccomplished by passing command-line arg...
Example :-class CommandLine{  public static void main(String args[])  {     for(int i=0; i < args.length; i++)       Syste...
Upcoming SlideShare
Loading in...5
×

Class method

1,907

Published on

ppt of class and methods in java,recursion in java,nested class,java,command line argument,method overloading,call by value,call by reference,constructor overloading core java ppt

1 Comment
2 Likes
Statistics
Notes
  • this ppt how to download
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
1,907
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • Transcript of "Class method"

    1. 1. Chapter 6Classes And Method http://www.java2all.com
    2. 2. Method Overloading http://www.java2all.com
    3. 3. • A class can contain any number of methods. Methods can be with parameter and without parameter.• The parameter in a method are called type signature.• It is possible in java to define two or more methods within the same class that share the same name, but with different parameter declarations (type signatures).• When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. http://www.java2all.com
    4. 4. • Overloading methods demonstrate the concept of 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 call.• Thus, overloaded methods must differ in the type and/or number of their parameters.• Overloaded methods may have different return types. http://www.java2all.com
    5. 5. • 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. http://www.java2all.com
    6. 6. public class MethodOver{ int n1; int n2; MethodOver() { n1 = 10; n2 = 20; } void square() { System.out.println("The Square is " + n1 * n2); } void square(int p1) { n1 = p1; System.out.println("The Square is " + n1 * n2); } void square(int p1, int p2) { n1 = p1; n2 = p2; System.out.println("The Square is " + n1 * n2); } public static void main(String args[]) { MethodOver obj1 = new MethodOver(); obj1.square(); //call non parameterise method obj1.square(4); //call method which has 1 argument obj1.square(7,8); //call method which has 2 argument } http://www.java2all.com
    7. 7. public class MethodOver{ int n1; int n2; MethodOver() { n1 = 10; n2 = 20; } void square() { System.out.println("The Square is " + n1 * n2); } Output : void square(int p1) { n1 = p1; System.out.println("The Square is " + n1 * n2); } The Square is 200 void square(int p1, int p2) { The Square is 80 n1 = p1; The Square is 56 n2 = p2; System.out.println("The Square is " + n1 * n2); } You can see that here we have 3 square public static void main(String args[]) { methods with different argument. MethodOver obj1 = new MethodOver(); Its called method overloading. obj1.square(); //call non parameterise method obj1.square(4); //call method which has 1 argument obj1.square(7,8); //call method which has 2 argument } http://www.java2all.com
    8. 8. Constructors overloading http://www.java2all.com
    9. 9. Constructors Along with method overloading, we canalso overload constructors. Constructors having the same name withdifferent parameter list is called constructoroverloading. http://www.java2all.com
    10. 10. class Point{ int x; int y; Point(int a, int b) { x = a; y = b; }}class Circle{ int originX; int originY; int radius; //Default Constructor Circle() { originX = 5; originY = 5; radius = 3; }// Constructor initializing the coordinates of origin and the radius. http://www.java2all.com
    11. 11. Circle(int x1, int y1, int r) { originX = x1; originY = y1; radius = r; } Circle(Point p, int r) { originX = p.x; originY = p.y; radius = r; } void display() { System.out.println("--Center at " + originX + " and " + originY); System.out.println("Radius = " + radius); } public static void main(String args[]) { Circle c1 = new Circle(); Circle c2 = new Circle(10,20,5); Circle c3 = new Circle(new Point(15,25),10); c1.display(); c2.display(); c3.display(); }} http://www.java2all.com
    12. 12. Circle(int x1, int y1, int r) { originX = x1; originY = y1; radius = r; } Circle(Point p, int r) { originX = p.x; originY = p.y; radius = r; } void display() { Output : --Center at 5 and 5 System.out.println("--Center at " + originX + " and " + originY); System.out.println("Radius = " + radius); } Radius = 3 public static void main(String args[]) { --Center at 10 and 20 Circle c1 = new Circle(); = 5 Radius --Center at 15 and 25 Circle c2 = new Circle(10,20,5); Circle c3 = new Circle(new Point(15,25),10); c1.display(); Radius = 10 c2.display(); c3.display(); }} http://www.java2all.com
    13. 13. Above program is quite complicated here i amgiving you perfect flow of program. First of all note one thing thatnewClassName() this is a short syntax of creating objectof any class. And we all know that when we create object theconstructor of that class will be called automatically. So in our program first of all due to syntax Circlec1 = new Circle(); non parameterize constructor will becalled for object c1 so we get output like Center at 5 and5 Radius = 3 in c1.display(). http://www.java2all.com
    14. 14. Next due to syntax Circle c2 = newCircle(10,20,5); constructor which has 3 arguments willbe called for object c2 so we get output like Center at 10and 20 Radius = 5 in c2.display(). Now when we define object c3 our syntax islike Circle c3 = new Circle(new Point(15,25),10); so firstof all it will create object for Point class so constructor ofpoint class will be called and it will set parameter x and y. Then constructor of circle class which has Pointclass object as an argument along with one int argumentwill be called and set all parameter as per program and weget output like Center at 15 and 25 Radius = 10 inc3.display(). http://www.java2all.com
    15. 15. More About Method http://www.java2all.com
    16. 16. 1. Passing Objects as a Parameter to Method. We have seen that methods can take parameters as input and process them. It is also common to pass objects as a parameter to methods. http://www.java2all.com
    17. 17. class PassObj{ int n1; int n2; PassObj() // constructor { n1 = 0; n2 = 0; } PassObj(int p1, int p2) Output : { n1 = p1; n2 = p2; Multiplication is } void multiply(PassObj p1) 30 { int temp; temp = p1.n1 * p1.n2; System.out.println("Multiplication is " + temp); } public static void main(String args[]) { PassObj obj1 = new PassObj(5,6); PassObj obj2 = new PassObj(); obj2.multiply(obj1); }} http://www.java2all.com
    18. 18. 2. Method overloading with object as a parameter class MetObjOv { int n1; int n2; // constructor MetObjOv() { n1 = 0; n2 = 0; } MetObjOv(int x, int y) { n1 = x; n2 = y; } void multiply(MetObjOv p1) { n1 = p1.n1; n2 = p1.n2; System.out.println("There is nothing to multiply "); System.out.println("n1 = "+n1+"tn2 = " +n2); } http://www.java2all.com
    19. 19. void multiply(MetObjOv p1, MetObjOv p2) { n1 = p1.n1 * p2.n1; n2 = p1.n2 * p2.n2; System.out.println("Multiplication of two objects "); System.out.println("n1 = " + n1 + "tn2 = " + n2 ); } public static void main(String args[]) { MetObjOv obj1 = new MetObjOv(5,6); MetObjOv obj2 = new MetObjOv(6,5); MetObjOv obj3 = new MetObjOv(); obj3.multiply(obj1); obj3.multiply(obj1, obj2); }} Output : There is nothing to multiply n1 = 5 n2 = 6 Multiplication of two objects n1 = 30 n2 = 30 http://www.java2all.com
    20. 20. 3. Return an Object. A method can return any type of data, including class type (object) that you create. http://www.java2all.com
    21. 21. class RetObj{ int n1; int n2; // constructor RetObj() { n1 = 0; n2 = 0; } RetObj(int x, int y) { n1 = x; n2 = y; } RetObj multiply(RetObj p1, RetObj p2) { n1 = p1.n1 * p2.n1; n2 = p1.n2 * p2.n2; return (this); } void display() { System.out.println("An Example of returning an Object "); System.out.println("n1 = "+n1+"tn2 = " +n2); } http://www.java2all.com
    22. 22. public static void main(String args[]) { RetObj obj1 = new RetObj(5,6); RetObj obj2 = new RetObj(6,5); RetObj obj3 = new RetObj(); obj3 = obj3.multiply(obj1, obj2); obj3.display(); }} Output : An Example of returning an Object n1 = 30 n2 = 30 RetObj multiply(RetObj p1, RetObj p2) This is the syntax in ourprogram which has return type object. obj3 = obj3.multiply(obj1, obj2); this is the syntax which calls methodmultiply and return object, it will store in obj3. http://www.java2all.com
    23. 23. Call By Value http://www.java2all.com
    24. 24. Now we all know that how to define and call themethods.There are two types of calling method and those are1. call by value2. call by reference Here we illustrate call by value and in next topicwe will look at call by reference. In call by value when we call any method wepass value as method parameter so changing inlocal variables of the method doesn‘t affect theoriginal variables of class. http://www.java2all.com
    25. 25. This method copies the value of an argumentinto the formal parameter of the subroutine. Therefore, changes made to the parameter of thesubroutine have no effect on the argument. In java, when we pass a primitive type to amethod, it is passed by value. Thus, what occurs to the parameter that receivesthe argument has no effect outside the method. http://www.java2all.com
    26. 26. public class CallBy_Value{ public static void main(String[] args) { Value v = new Value(10,20); System.out.println("a and b before call............"); System.out.println("a = "+v.a); System.out.println("b = "+v.b); v.call(v.a,v.b); // CALL BY VALUE System.out.println("a and b after call............"); System.out.println("a = "+v.a); System.out.println("b = "+v.b); }}class Value Output :{ int a,b; Value(int i,int j) a and b before call............ { a=i; a = 10 b = j; } b = 20 void call(int a, int b) { a and b after call............ a = a * 2; a = 10 b = b * 2; } b = 20} http://www.java2all.com
    27. 27. You can see that after calling method we changevalue of a and b but it will not affect the original value ofclass` members because of call by value. We pass value v.a and v.b as parameter and it willchange local method`s a and b variables. http://www.java2all.com
    28. 28. Call By Reference http://www.java2all.com
    29. 29. Here we pass reference as parameter infunction calling. We all know that reference means object so wepass object as parameter. A reference to an argument (not value ofargument) is passed to the parameter. Inside the subroutine, this reference is used toaccess the actual argument specified in the call. This means that changes made to theparameters will affect the argument used to call thesubroutine. http://www.java2all.com
    30. 30. When we pass an object to a method, thesituation changes, because objects are passed bycall-by-reference. When we create a variable of a class type, weare only creating a reference to an object. Thus,When you pass this reference to a method, theparameter that receives it will refer to the sameobject as that referred to by the argument. This effectively means that objects are passedto method do affect the object used as anargument. http://www.java2all.com
    31. 31. public class CallBy_Reference{ public static void main(String[] args) { Reference r = new Reference(10,20); System.out.println("a and b before call............"); System.out.println("a = "+r.a); System.out.println("b = "+r.b); r.call(r); // CALL BY REFERENCE System.out.println("a and b after call............."); System.out.println("a = "+r.a); System.out.println("b = "+r.b); }}class Reference Output :{ int a,b; Reference(int i,int j) { a and b before call............ a=i; b = j; a = 10 } b = 20 void call(Reference r) { a and b after call............. r.a = a * 2; r.b = b * 2; a = 20} } b = 40 http://www.java2all.com
    32. 32. You can see that after calling method valueof original a and b is changed because of call byreference. Here we pass "r" reference (Object) asparameter in method calling. So changes insidemethod will affect original variable of class. http://www.java2all.com
    33. 33. Recursion http://www.java2all.com
    34. 34. Recursion• Recursion is the process of defining something in terms of itself.• When function call it self is called recursion.• A method that calls itself is said to be recursive. http://www.java2all.com
    35. 35. import java.util.Scanner;class Factorial{ int fact(int n) // this is a recursive function { int result; if(n==1) return 1; result = fact(n-1) * n; //From here function call it self (fact(n-1)) return result; }}public class Recursion Output :{ public static void main(String args[]) { Enter int no = 7 int x; Factorial of7 is 5040 Scanner s = new Scanner(System.in); System.out.print("Enter int no = "); x = s.nextInt(); Factorial f = new Factorial(); System.out.println("Factorial of" + x + " is " + f.fact(x)); }} http://www.java2all.com
    36. 36. • Here the method fact is recursive because it calls itself.• The whole process something like this• result = fact(7-1) * 7 and so on until it returns 1.• So one thing is sure that we have to take care that in every recursive process there must be a terminate condition to come out from recursion. http://www.java2all.com
    37. 37. Nested Class http://www.java2all.com
    38. 38. It is possible to define a class within anotherclass; such classes are known as nested classes. The scope of a nested class is bounded by thescope of its enclosing class. That means, if class B is defined within classA, then B is known to A, but not outside A. If A is nesting class B, then A has access to allthe members of B, including private members. Butthe B does not have access to the members of nestedclass. http://www.java2all.com
    39. 39. There are two types of nested classes:1. Static2. Non – StaticStatic nested class :- A static nested class is one which has thestatic modifier, as it is static it must access themember of its enclosing class through an object.That means it cannot refer to member of itsenclosing class directly. http://www.java2all.com
    40. 40. Non – Static nested class :- Non – Static nested class is known as innerclass. It has access to all of its variables andmethods of its outer class and can refer to themdirectly. An inner class is fully within the scope of itsenclosing class. http://www.java2all.com
    41. 41. class Inner1{ class Contents { private int i = 16; public int value() { return i; } } class Destination { private String label; Destination(String whereTo) { label = whereTo; } } public void ship(String dest) { Contents c = new Contents(); // create object of inner class Contents Destination d = new Destination(dest); // create object of inner class Destination System.out.println("Shipped " + c.value() + " item(s) to " + dest); } http://www.java2all.com
    42. 42. public static void main(String args[]) { Inner1 p = new Inner1(); p.ship("Congo"); //call ship method of outer class "inner1" }}Output :Shipped 16 item(s) to Congo http://www.java2all.com
    43. 43. Let us see one more example but here theprogram will not compileclass Outer{ int outer_x = 100; void test() { Inner inner = new Inner(); inner.display(); } class Inner // this is an inner class { int y = 10; // y is local to Inner void display() { System.out.println("display: outer_x = " + outer_x); } } void showy() { System.out.println(y); // error, y not known here! }} http://www.java2all.com
    44. 44. class InnerClassDemo{ public static void main(String args[]) { Outer outer = new Outer(); outer.test(); }} Here, y is declared as an instance variable of Inner. Thus it is not known outside Of that class and it cannot be used by showy( ). http://www.java2all.com
    45. 45. Command Line Argument http://www.java2all.com
    46. 46. Sometimes you will want to pass informationinto a program when you run it. This isaccomplished by passing command-line argumentsto main( ). A command-line argument is the informationthat directly follows the program’s name on thecommand line when it is executed. To access the command-line arguments inside aJava program is quite easy—they are stored asstrings in the String array passed to main( ). http://www.java2all.com
    47. 47. Example :-class CommandLine{ public static void main(String args[]) { for(int i=0; i < args.length; i++) System.out.println("args[" + i + "]: " +args[i]); }}Try executing this program, as shown here:java Command Line this is a test 100 -1When you do, you will see the following output:args[0]: thisargs[1]: isargs[2]: aargs[3]: testargs[4]: 100args[5]: -1 http://www.java2all.com

    ×