VOOZH about

URL: https://dev.to/kathirvel-s/accessing-methods-from-another-class-in-java-from-tambaram-to-velachery-understanding-method-515c

⇱ Accessing Methods from Another Class in Java – From Tambaram to Velachery: Understanding Method Access and Inheritance in Java - DEV Community


Today my trainer taught a Java concept that genuinely stayed with me.

Sometimes in Java, you write a method in one class and then wonder:

"How can I use that method inside another class?"

At first, it feels confusing.

Later, when you understand objects, imports, inheritance, and the extends keyword, everything starts making sense.

In this article, I'll explain the exact flow my trainer taught todayβ€”from accessing methods using objects, to why inheritance exists, to how static methods work.

Let's start from the beginning.


Step 1: A Simple Home Class

Suppose we have a package called tambaram.

package tambaram;

public class Home {

 public static void main(String[] args){
 Home person = new Home();
 person.study();
 }

 public void study(){
 System.out.println("learning java");
 }
}

Output

learning java

Here what happens?

  1. Java starts execution from main().
  2. We create an object called person.
  3. Using that object, we call the method study().
  4. The method executes and prints the output.

Think of it like this:

  • Home is the blueprint.
  • person is the actual object created from that blueprint.
  • Methods belong to the object.

So Java understands:

person.study();

as

"Hey object person, execute your study() method."


Step 2: Another Class in Another Package

Now let's create a completely different class.

package velachery;

public class Theatre {

 public static void main(String [] args ){
 Theatre person = new Theatre();
 person.watch();
 }

 public void watch(){
 System.out.println("watch blast movie");
 }
}

Output

watch blast movie

This class has its own object and its own method.

Notice something interesting:

study() belongs to Home.

watch() belongs to Theatre.

They are completely separate classes.


A Small Story Before We Continue...

Let's imagine a person named Arun.

Arun lives in Tambaram.

Every morning, he sits in his room, opens his laptop, and spends time learning Java. He practices programs, writes code, fixes errors, and slowly improves his programming skills.

So when Arun is at home, we can think of him like this:

person.study();

because his current activity is studying.

Now imagine it is the weekend.

πŸ‘

After spending hours learning Java, Arun feels like he deserves a small break.

He checks the latest movie releases and hears that a great movie is playing in a theatre at Velachery.

So he travels from Tambaram to Velachery to watch the movie.

At that moment, his activity changes from:

person.study();

to

person.watch();

Now think carefully.

Is Arun a different person?

No.

It is still the same Arun.

The only difference is the place and the behavior he wants to perform.

When he is in Tambaram, he studies.

When he is in Velachery, he watches movies.

As humans, we can naturally perform both actions because we move between places and use different facilities.

But Java classes don't work that way.

A method belongs to the class in which it is defined.

The study() method belongs to Home.

The watch() method belongs to Theatre.

So when our Home person wants to watch a movie, Java cannot automatically understand where the watch() method exists.

Java needs a proper connection.

It needs to know:

"Where is this method defined?"

"Which class owns this behavior?"

"How can I access it?"

That is exactly why we import the Theatre class and create an object of it.

By doing that, we are telling Java:

"Our Tambaram person also wants to use the facilities available in Velachery."

And that leads us to the next step, where the Home class accesses the Theatre class and calls:

samePerson.watch();

This may look like a simple line of code, but it introduces one of the most important ideas in Java:

A class can access behaviors from another class when we create the proper relationship between them.

Let's see how that works.


The Big Question

Now suppose the person in Home wants to watch a movie.

How can Home access the method inside Theatre?

This is where Java gives us a solution.


Step 3: Importing Another Class

import velachery.Theatre;

public class Home {

 public static void main(String[] args){

 Home person = new Home();

 Theatre samePerson = new Theatre();

 samePerson.watch();

 person.study();
 }

 public void study(){
 System.out.println("learning java");
 }
}

Output

watch blast movie
learning java

How Does This Work?

Let's break it down carefully.

First

import velachery.Theatre;

This tells Java:

"I want to use the Theatre class that exists inside the velachery package."

Without importing, Java doesn't know where Theatre exists.


Second

Theatre samePerson = new Theatre();

Here we create an object of Theatre.

Think of it as:

Home Person ---> Studies

Theatre Person ---> Watches Movie

Now we have access to Theatre's methods.


Third

samePerson.watch();

Java looks inside the Theatre object.

It finds:

public void watch()

and executes it.


Visual Understanding

Think about a real person.

Person:
 Study at Home
 Watch Movie at Theatre

Similarly:

Home person = new Home();

Theatre samePerson = new Theatre();

One object accesses Home behaviors.

Another object accesses Theatre behaviors.

Both objects represent different class instances.


But There Is a Problem...

This works.

But imagine a project with:

Home
Theatre
School
Office
Library
College
Bank
Hospital

For every class:

ClassName obj = new ClassName();

must be created.

Example:

School school = new School();
Office office = new Office();
Library library = new Library();

Again and again.

Again and again.

Lots of object creation.

Lots of repetitive code.

This becomes boilerplate code.

Java gives us a better approach.

That approach is called:

Inheritance


Understanding Why Inheritance Exists

Consider this class.

public class Mobile{

 public static void main(String[] args){

 Mobile mobile = new Mobile();

 mobile.calling();
 mobile.message();

 System.out.println("Hellooo ");
 }

 public void calling(){
 System.out.println("calling feature");
 }

 public void message(){
 System.out.println("message feature");
 }
}

Output

calling feature
message feature
Hellooo

Now think carefully.

Every mobile phone has:

  • Calling
  • Messaging

πŸ‘

Whether it is:

  • Samsung
  • Vivo
  • Oppo
  • Nokia
  • Motorola

all phones share these common features.


Creating Samsung Class

public class Samsung{

 public static void main(String[] args){

 Samsung samsung = new Samsung();

 samsung.calling();
 samsung.message();
 }
}

Java gives an error.

Why?

Because Samsung class doesn't contain:

calling()
message()

methods.

Java looks inside Samsung and says:

"I can't find these methods."

Hence compilation error.


Enter the extends Keyword

Now let's modify the class.

public class Samsung extends Mobile{

 public static void main(String[] args){

 Samsung samsung = new Samsung();

 samsung.calling();
 samsung.message();
 }
}

Now it works perfectly.


What Exactly Does extends Mean?

The Oracle Java documentation defines inheritance as a mechanism where one class acquires the properties and behaviors of another class.

In simple words:

One class can reuse the methods and variables of another class.

The keyword used is:

extends

Parent and Child Relationship

Mobile
 ↑
Samsung

Mobile is called:

  • Parent Class
  • Super Class
  • Base Class

Samsung is called:

  • Child Class
  • Sub Class
  • Derived Class

What Happens Internally?

When Java sees:

class Samsung extends Mobile

it understands:

Samsung should inherit everything available inside Mobile.

So Samsung automatically gets:

calling()

message()

without rewriting them.

That is the real power of inheritance.


Real World Example

Imagine a room with four AC units.

AC 1
AC 2
AC 3
AC 4

Do we need four remotes?

No.

πŸ‘

One remote can control all ACs because they share common functionality.

Inheritance works similarly.

The parent class contains common behavior.

Child classes reuse it.

Instead of rewriting code everywhere, one common source is shared.

This is exactly the idea behind inheritance.


Why Developers Love Inheritance

Without inheritance:

calling()
message()

must be written in:

Samsung
Vivo
Oppo
Nokia
Realme
Motorola

again and again.

With inheritance:

Mobile

contains the common methods.

Every mobile brand simply extends it.

Benefits:

Code Reusability

Write once.

Use everywhere.

Less Boilerplate

Less repeated code.

Easier Maintenance

Fix one method.

All child classes get the update.

Better Design

Common behavior stays in one place.


Accessing Static Methods

Until now we discussed instance methods.

Example:

public void calling()

requires an object.

But static methods belong to the class itself.

Example:

public class Mobile{

 public static void companyName(){
 System.out.println("Mobile World");
 }
}

Call it like:

Mobile.companyName();

Notice:

new Mobile()

is not needed.

Why?

Because static methods belong to the class, not the object.


Static Method Analogy

Imagine again our room with four ACs.

AC1
AC2
AC3
AC4

Each AC is an object.

But there is only one remote.

That remote is shared.

Similarly:

static

means one common thing shared by all objects.

Every object can use it.

But Java stores only one copy.


Classes Without main() Method

Many beginners think every class must contain:

public static void main(String[] args)

Actually, no.

Consider:

public class Mobile{

 public void calling(){
 System.out.println("calling");
 }

 public void message(){
 System.out.println("message");
 }
}

This compiles perfectly.

No error.


Why Doesn't It Run?

Because Java starts execution from:

main()

If there is no main method:

Java has nothing to execute.

So:

Compiles?

βœ… Yes

Runs?

❌ No


Then Why Create Such Classes?

Because many classes exist only to provide:

  • Methods
  • Variables
  • Common functionality

Example:

Mobile

doesn't need to run directly.

Its purpose is:

Provide common features

for:

Samsung
Vivo
Oppo
Realme

and other child classes.

This is extremely common in real-world projects.

Many classes are never executed directly.

They simply provide reusable functionality.


The Journey We Learned Today

We started with:

person.study();

Then moved to:

samePerson.watch();

using imported classes.

Then understood the limitation of creating objects everywhere.

Finally we discovered:

extends

which allows one class to inherit methods from another class.

This is one of the most important pillars of Object-Oriented Programming (OOP).

Once inheritance clicks, many advanced concepts become easier:

  • Method Overriding
  • Polymorphism
  • Abstract Classes
  • Interfaces
  • Framework Development
  • Spring Boot Development

Everything starts from understanding inheritance properly.

And honestly, this was one of those concepts that instantly felt practical when connected to real-world examples like mobile phones and AC remotes.

If you're learning Java, spend time experimenting with:

extends

because you'll see it everywhere in professional Java applications.

A single keyword can save hundreds or even thousands of lines of repeated code.

That is the power of inheritance.


References

  1. Oracle Java Tutorials – Inheritance
  2. Oracle Java Language Specification
  3. Oracle Java Documentation – Classes and Objects
  4. Oracle Java Documentation – Static Methods and Fields
  5. Oracle Java Tutorials – Object-Oriented Programming Concepts

Official Oracle Java Documentation:

Oracle Java Documentation

Oracle Java OOP Concepts

Oracle Classes and Objects Guide

Happy Coding!