Develop programs based on variation in methods i.e. passing by value, passing by reference, returning values and returning objects from methods.

 

AIM: Develop programs based on variation in methods i.e. passing by value, passing by reference, returning values and returning objects from methods.

CODE:

class Time{

            int h;

            Time(){

                        h=0;

            }

            Time(int h){

                        this.h=h;

            }

            Time Timeobj(Time t){

                        Time t1 = new Time();

                        t1.h = h+t.h;

                        return t1;

            }

            static int sum(int h1,int h2){

                        return h1+h2;

            }

            void display(){

                        System.out.println("Time: "+h);

            }

}

public class p18{

            public static void main(String args[]){

                        Time t1,t2,t3,t4;

                        t1 = new Time(5);

                        t2 = new Time(1);

                        t3 = new Time();

                        t4 = new Time();

                        t3 = t1.Timeobj(t2);

                        t4.h=Time.sum(t1.h,t2.h);

                        t3.display();

                        t4.display();

            }

}

                       

OUTPUT:



Comments

Popular posts from this blog

Write an application that illustrates method overriding in the same package and different packages. Also demonstrate accessibility rules in inside and outside packages

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, and Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object.

Write a program that illustrates interface inheritance. Interface P12 inherits from both P1 and P2. Each interface declares one constant and one method. The class Q implements P12. Instantiate Q and invoke each of its methods. Each method displays one of the constants.