How to call a class in another class in Java?

Calling a Class in Another Class in Java

Introduction

In Java, classes are the fundamental building blocks of object-oriented programming (OOP). They provide a way to encapsulate data and behavior, making it easier to write reusable code. One of the most useful features of classes is the ability to call another class from within it. This is known as inheritance, and it’s a powerful tool for building complex systems.

Why Call a Class in Another Class?

There are several reasons why you might want to call a class in another class:

  • To inherit behavior from another class
  • To create a new class that builds upon an existing one
  • To create a class that provides a specific implementation of a class
  • To create a class that provides a specific interface to another class

How to Call a Class in Another Class in Java

To call a class in another class in Java, you need to use the extends keyword to inherit from the class you want to call. Here’s a step-by-step guide:

Step 1: Define the Class You Want to Call

First, you need to define the class you want to call. This class should have the same name as the class you want to call, but with a different suffix (e.g., MyClass instead of MyClass.java).

Step 2: Define the Class You Want to Call in Another Class

Next, you need to define the class you want to call in another class. This class should extend the class you want to call.

Step 3: Call the Class in Another Class

Finally, you can call the class in another class using the extends keyword.

Example Code

Here’s an example of how you can call a class in another class in Java:

// Define the class you want to call
public class MyClass {
public void myMethod() {
System.out.println("Hello, World!");
}
}

// Define the class you want to call in another class
public class AnotherClass {
public static void main(String[] args) {
// Call the class in another class
MyClass myClass = new MyClass();
myClass.myMethod();
}
}

Inheritance

One of the most powerful features of classes is inheritance. Inheritance allows you to create a new class that builds upon an existing one. Here’s an example:

// Define the parent class
public class Animal {
public void sound() {
System.out.println("The animal makes a sound.");
}
}

// Define the child class
public class Dog extends Animal {
public void sound() {
System.out.println("The dog barks.");
}
}

// Call the child class
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound();
}
}

Polymorphism

Polymorphism is the ability of an object to take on multiple forms. In Java, polymorphism is achieved through method overriding and method overloading. Here’s an example:

// Define the parent class
public class Shape {
public void draw() {
System.out.println("Drawing a shape.");
}
}

// Define the child class
public class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
}

// Define the child class
public class Rectangle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a rectangle.");
}
}

// Call the child class
public class Main {
public static void main(String[] args) {
Shape myShape = new Circle();
myShape.draw();
}
}

Encapsulation

Encapsulation is the ability to hide the implementation details of an object from the outside world. In Java, encapsulation is achieved through access modifiers (public, private, protected) and getter and setter methods. Here’s an example:

// Define the class
public class BankAccount {
private double balance;

public BankAccount(double balance) {
this.balance = balance;
}

public double getBalance() {
return balance;
}

public void deposit(double amount) {
balance += amount;
}

public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient funds.");
}
}
}

// Call the class
public class Main {
public static void main(String[] args) {
BankAccount myAccount = new BankAccount(1000);
myAccount.deposit(500);
myAccount.withdraw(200);
}
}

Conclusion

Calling a class in another class in Java is a powerful tool for building complex systems. By using inheritance, polymorphism, encapsulation, and access modifiers, you can create complex classes that provide a specific implementation of a class. With practice and experience, you’ll become proficient in using these features to build robust and maintainable code.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top