Q:

Java program to implement cascaded method call

belongs to collection: Java Class and Object Programs

0

Java program to implement cascaded method call

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will create a Sample class with 3 methods. Each method will return the object of the same class. To return the object of the class, we used this object. By returning an object from each method, we can implement a cascaded method call.

Program/Source Code:

The source code to implement the cascaded method call is given below. The given program is compiled and executed successfully.

// Java program to implement cascaded 
// method call

class Sample {
  Sample Method1() {
    System.out.println("Method1 called");
    return this;
  }
  Sample Method2() {
    System.out.println("Method2 called");
    return this;
  }
  Sample Method3() {
    System.out.println("Method3 called");
    return this;
  }
}

class Main {
  public static void main(String args[]) {
    Sample X = new Sample();
    X.Method1().Method2().Method3();
  }
}

Output:

Method1 called
Method2 called
Method3 called

Explanation:

In the above program, we created a Sample class and public class Main. The Sample class contains three methods Method1()Method2()Method3(). All methods return the object of the same class using this object to implement the cascaded method class.

The Main class contains a static method main(). The main() is an entry point for the program. Here, we created an object of the Sample class and called all methods in a cascaded manner.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Class and Object Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to create a method returning an objec... >>
<< Java program to create a singleton class with meth...