Perfect assignment of object oriented programing

237 views
141 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
237
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Perfect assignment of object oriented programing

  1. 1. Assignment of Object oriented Programing: Submitted To: Sir Khalid Mahmood Submitted By: Asif Raza Roll No: F12-M23
  2. 2. Method Overloading (function overloading) in Java 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. Note that all Method of a class must have different parameter list i.e the signature of each constructor must differ. The signature of a constructor is a combination of its name and the sequence of its parameter types. // Demonstrate method overloading. class method_overloading {int x,y,r; void add() { System.out.println (" Empty ... "); } void add (int a) { System.out.println (" You Have type Integer value... With out Decimal"); } void add (int a, int b) { x=a; y=b; r=a+b; System.out.println(" Sum Result is" +r); } void add (double a) { System.out.println(" You have type decimal no"); } } class app_method_overloading { public static void main(String agr[]) { method_overloading obj=new method_overloading(); obj.add(); obj.add(2); obj.add(2.2); obj.add(2,3); } }
  3. 3. Overloading Constructors A class can have several constructors. This feature of java is called constructor overloading. Note that all constructor of a class must have different parameter list i.e the signature of each constructor must differ. The signature of a constructor is a combination of its name and the sequence of its parameter types. Overloaded constructors are very common in java because they provide several ways to create an object of a particular class. It also allows you to create and initialize an object by using different types of data. class constractor { int x, y,r; Constractor () { x=0; y=0; r=x+y; } Constractor (int a, int b) { x=a; y=b; r=x+y; } void display () { System.out.println (" Sum Result is: “ +r); } } class app_constractor_overloading { public static void main(String agr[]) { constractor obj=new constractor(); obj.display(); constractor bj=new constractor(1,3); bj.display(); }}
  4. 4. Object as Parameter passing in Java class Box { double width; double height; // constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized box } // constructor used when all dimensions specified Box(double w, double h) { width = w; height = h; } // construct clone of an object Box(Box ob) { // pass object to constructor width = ob.width; height = ob.height; System.out.println("Volume of mybox1 is " + width); System.out.println("Volume of mybox1 is " + height); } void volume() { double volum =width * height ; System.out.println("Volume is " +volum ); } } class my_object_passing { public static void main(String args[]) { // create boxes using the various constructors Box mybox1 = new Box(10, 20); Box mybox2 = new Box(10,10); Box myclone = new Box(mybox1); Box myclone1 = new Box(mybox2); myclone.volume(); myclone1.volume(); } }
  5. 5. Recursion A method that calls itself is said to be recursive. // demo recursion .. class Factorial { int fact(int n) { int result; if ( n ==1) return 1; result = fact (n-1) * n; return result; } } class Recursion { public static void main (String args[]) { Factorial f =new Factorial(); System.out.println(“Factorial of 3 is “ + f.fact(3)); System.out.println(“Factorial of 4 is “ + f.fact(4)); System.out.println(“Factorial of 5 is “ + f.fact(5)); } } The output from this program is shown here: Factorial of 3 is 6 Factorial of 4 is 24 Factorial of 5 is 120

×