Objects Interacting Example

This program demonstrates interaction between objects.

Problem Statement

Write a Java program where Teacher object interacts with Student object.

Source Code

1 class Student {
2 String name;
3 void learn(){ System.out.println(name+" is learning"); }
4 }
5 class Teacher {
6 String name;
7 void teach(Student s){ System.out.println(name+" teaches "+s.name); }
8 }
9 public class ObjectInteraction {
10 public static void main(String[] args) {
11 System.out.println("Eduinq Object Interaction");
12 Student s=new Student(); s.name="Arun";
13 Teacher t=new Teacher(); t.name="Rahul";
14 t.teach(s); s.learn();
15 }
16 }

Program Output

Eduinq Object Interaction
Rahul teaches Arun
Arun is learning

Explanation

Teacher object calls method involving Student object. - Student object responds with its own method. - This demonstrates collaboration between objects. Conclusion: Shows how objects interact in Java.

Quick Links to Explore