Connect Two Classes in Java:
We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it. |
(1) In this blog, we can learn how to connect two classes in Java.
(2) We named the class Main.java and Bike.java.
(3) In Bike.java class we define the objects and methods. We call these objects and methods in Main.java class as shown below.
Bike.java class:

public class Bike {
String maker = "Yamaha";
String name = "R15";
int year = 2015;
String color = "red";
double price = 100000.00;
void drive() {
System.out.println("You drive the bike");
}
void brake() {
System.out.println("You have to apply the brake");
}
}
Main.java Class:

public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Bike myBike = new Bike();
System.out.println(myBike.maker);
System.out.println(myBike.name);
System.out.println(myBike.year);
System.out.println(myBike.color);
System.out.println(myBike.price);
System.out.println();
myBike.drive();
myBike.brake();
}
}
Comments (No)