Paramclasses Academy - Blog

Tutorials Details

←
Post 8 of 40
→

What is Java OOP Concepts for Android: Inheritance, Polymorphism, Encapsulation and Abstraction ?

What is Java OOP Concepts for Android: Inheritance, Polymorphism, Encapsulation and Abstraction ?

November 2, 2026

Introduction

Object-Oriented Programming (OOP) is one of the most important concepts in Java. It helps developers write organized, reusable, and maintainable code.

In previous tutorials, we learned about Java classes, objects, and methods. Now, we will understand how Java uses these concepts to build larger applications.

Java has four major OOP principles:

  1. Inheritance

  2. Polymorphism

  3. Encapsulation

  4. Abstraction

These concepts are widely used in Android development. For example, Android Activities use inheritance, method overriding supports polymorphism, private variables help with encapsulation, and abstract classes help define common structures.

Let's understand each concept with simple Java examples.

1. What Is Object-Oriented Programming (OOP)?

Object-Oriented Programming is a programming approach in which code is organized around classes and objects.

Instead of writing the entire application in one place, we divide it into smaller classes. Each class can contain its own variables and methods.

For example, in a student management application, we can create separate classes for:

  • Student

  • Teacher

  • Course

  • Attendance

  • Result

Each class manages its own data and functionality.

OOP makes code easier to understand, reuse, test, and maintain.

2. Inheritance in Java

Inheritance allows one class to acquire the properties and methods of another class.

The class whose properties are inherited is called the Parent Class or Superclass.

The class that inherits those properties is called the Child Class or Subclass.

Syntax:

class Parent {
    // Variables and methods
}

class Child extends Parent {
    // Additional variables and methods
}

The extends keyword is used to inherit a class.

Example: Inheritance

class Animal {

    void eat() {
        System.out.println("Animal is eating");
    }
}

class Dog extends Animal {

    void bark() {
        System.out.println("Dog is barking");
    }
}

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.eat();
        dog.bark();
    }
}

Output:

Animal is eating
Dog is barking

Explanation:

  • Animal is the parent class.

  • Dog is the child class.

  • extends Animal allows Dog to inherit accessible members from Animal.

  • eat() belongs to the parent class.

  • bark() belongs to the child class.

  • The Dog object can call both methods.

Types of Inheritance in Java

Java supports these common inheritance structures through classes:

1. Single Inheritance

One child class inherits from one parent class.

class Animal {
    void eat() {
        System.out.println("Eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking");
    }
}

2. Multilevel Inheritance

A class inherits from another child class, creating a chain.

class Animal {
    void eat() {
        System.out.println("Eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking");
    }
}

class Puppy extends Dog {
    void weep() {
        System.out.println("Weeping");
    }
}

Here, Puppy can access the accessible methods inherited through Dog and Animal.

3. Hierarchical Inheritance

Multiple child classes inherit from the same parent class.

class Animal {
    void eat() {
        System.out.println("Eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking");
    }
}

class Cat extends Animal {
    void meow() {
        System.out.println("Meowing");
    }
}

Both Dog and Cat inherit from Animal.

Important: Java does not support multiple inheritance through classes. A class cannot extend two classes directly. However, Java supports multiple inheritance of type through interfaces.

3. Polymorphism in Java

Polymorphism means "one name, many forms."

It allows the same method name or reference type to behave differently depending on the situation.

Java mainly supports two forms of polymorphism:

  1. Compile-Time Polymorphism

  2. Runtime Polymorphism

A. Compile-Time Polymorphism (Method Overloading)

Method overloading means creating multiple methods with the same name but different parameter lists.

The methods may have different numbers or types of parameters.

Example:

class Calculator {

    int add(int a, int b) {
        return a + b;
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }

    double add(double a, double b) {
        return a + b;
    }
}

public class Main {

    public static void main(String[] args) {

        Calculator calculator = new Calculator();

        System.out.println(calculator.add(10, 20));
        System.out.println(calculator.add(10, 20, 30));
        System.out.println(calculator.add(5.5, 4.5));
    }
}

Output:

30
60
10.0

Explanation:

  • All three methods have the name add.

  • The first accepts two integers.

  • The second accepts three integers.

  • The third accepts two double values.

  • Java selects the appropriate method based on the arguments.

Remember: Changing only the return type is not enough to overload a method. The parameter list must be different.

B. Runtime Polymorphism (Method Overriding)

Method overriding occurs when a child class provides its own implementation of an inherited method.

Example:

class Animal {

    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {

    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {

    @Override
    void sound() {
        System.out.println("Cat meows");
    }
}

public class Main {

    public static void main(String[] args) {

        Animal animal1 = new Dog();
        Animal animal2 = new Cat();

        animal1.sound();
        animal2.sound();
    }
}

Output:

Dog barks
Cat meows

Explanation:

  • Animal is the parent class.

  • Dog and Cat override the sound() method.

  • Both variables have the reference type Animal.

  • The actual objects are Dog and Cat.

  • Java executes the overridden method according to the actual object at runtime.

The @Override annotation helps the compiler check that the method correctly overrides an inherited method.

4. Encapsulation in Java

Encapsulation means combining data and the methods that operate on that data inside a class while controlling access to the data.

In Java, we commonly use:

  • private variables

  • public getter methods

  • public setter methods

This helps protect data from direct, uncontrolled access.

Example: Encapsulation

class Student {

    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {

        if (age > 0) {
            this.age = age;
        }
    }

    public int getAge() {
        return age;
    }
}

public class Main {

    public static void main(String[] args) {

        Student student = new Student();

        student.setName("Rahul");
        student.setAge(21);

        System.out.println(student.getName());
        System.out.println(student.getAge());
    }
}

Output:

Rahul
21

Explanation:

  • private String name prevents direct access to the name variable from outside the class.

  • private int age protects the age variable.

  • setName() updates the name.

  • getName() returns the name.

  • setAge() checks that the age is greater than zero before assigning it.

  • getAge() returns the age.

Notice that we cannot directly write student.age = 21 because the variable is private.

This is useful when we want to validate data before storing it.

5. Abstraction in Java

Abstraction means hiding implementation details and exposing only the essential functionality.

For example, when you use an ATM, you select an option and enter the required information. You do not need to understand the internal banking operations to use it.

Java supports abstraction using:

  1. Abstract Classes

  2. Interfaces

A. Abstract Class

An abstract class is a class declared with the abstract keyword.

It can contain:

  • Abstract methods without a body

  • Normal methods with a body

  • Variables

  • Constructors

An abstract class cannot be instantiated directly.

Example:

abstract class Animal {

    abstract void sound();

    void sleep() {
        System.out.println("Animal is sleeping");
    }
}

class Dog extends Animal {

    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.sound();
        dog.sleep();
    }
}

Output:

Dog barks
Animal is sleeping

Explanation:

  • abstract class Animal defines an abstract class.

  • abstract void sound() declares a method without implementation.

  • Dog extends Animal and provides the implementation of sound().

  • sleep() is a normal method inherited from Animal.

  • We create a Dog object because Animal is abstract.

B. Interface in Java

An interface defines a contract that implementing classes must follow.

A class uses the implements keyword to implement an interface.

Example:

interface Vehicle {

    void start();
}

class Car implements Vehicle {

    @Override
    public void start() {
        System.out.println("Car is starting");
    }
}

public class Main {

    public static void main(String[] args) {

        Car car = new Car();

        car.start();
    }
}

Output:

Car is starting

Explanation:

  • Vehicle is an interface.

  • It declares the start() method.

  • Car implements Vehicle means Car agrees to provide the required implementation.

  • The start() method is implemented inside Car.

  • The method is public because interface methods are implicitly public.

An interface can also contain default, static, and private methods under Java's interface rules. For beginners, focus first on understanding how interfaces define common behavior.

6. Difference Between Abstract Class and Interface

An abstract class is useful when related classes need to share common state or implementation.

An interface is useful when different classes need to follow a common contract, even if they are otherwise unrelated.

Remember these points:

  • A class can extend only one class.

  • A class can implement multiple interfaces.

  • An abstract class can have constructors and instance variables.

  • An interface can define constants and method contracts, along with supported default and static methods.

  • Both abstract classes and interfaces support abstraction.

7. Real Android Development Example of OOP

Android development uses OOP concepts throughout the framework.

One common example is an Activity.

Consider this declaration:

public class MainActivity extends AppCompatActivity {
    
}

Here:

  • MainActivity is your class.

  • AppCompatActivity is the parent class.

  • extends is used for inheritance.

Your Activity inherits useful functionality from AppCompatActivity.

We also override lifecycle methods such as onCreate():

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    }
}

Here:

  • MainActivity inherits from AppCompatActivity.

  • onCreate() overrides an inherited lifecycle method.

  • @Override identifies the overridden method.

  • super.onCreate() calls the parent class implementation.

This is a practical example of inheritance and runtime polymorphism in Android.

8. Common Mistakes Beginners Should Avoid

1. Confusing overloading and overriding

Overloading means the same method name with different parameter lists. Overriding means a child class provides a new implementation of an inherited method.

2. Trying to create an abstract class object

Incorrect:

Animal animal = new Animal();

If Animal is abstract, it cannot be instantiated directly.

3. Accessing private variables directly

Use public methods such as getters and setters to access private data from outside the class.

4. Forgetting to implement interface methods

A normal concrete class must implement the required abstract methods of its interface.

5. Using inheritance unnecessarily

Do not use inheritance only to reuse a few lines of code. Use it when there is a genuine parent-child relationship. Sometimes composition is a better design choice.

9. Practice Programs

Try writing these programs yourself in Java:

  1. Create a Person class and inherit it into a Student class.

  2. Create an Employee class and inherit it into a Manager class.

  3. Demonstrate multilevel inheritance using Animal, Dog, and Puppy classes.

  4. Create a Calculator class with overloaded addition methods.

  5. Demonstrate method overriding using Animal, Dog, and Cat classes.

  6. Create a Student class with private variables and getter-setter methods.

  7. Add age validation inside a setter method.

  8. Create an abstract Shape class with an abstract area method.

  9. Create a Vehicle interface and implement it in Car and Bike classes.

  10. Create an Android Activity that extends AppCompatActivity and override its onCreate() method.

10. Conclusion

Object-Oriented Programming is an essential part of Java and Android development.

The four major OOP principles help developers organize and manage application code:

  • Inheritance allows classes to reuse accessible functionality from parent classes.

  • Polymorphism allows the same method or reference type to work in different forms.

  • Encapsulation protects data by controlling access to class variables.

  • Abstraction hides implementation details and exposes essential functionality.

Understanding these concepts will help you write better Java code and build more structured Android applications.

Practice each example in Android Studio and try changing the class names, methods, and values to understand how the concepts work.

11. Frequently Asked Questions (FAQs)

Q1. What is OOP in Java?

OOP stands for Object-Oriented Programming. It is a programming approach based on classes and objects.

Q2. What are the four pillars of OOP?

The four major principles are Inheritance, Polymorphism, Encapsulation, and Abstraction.

Q3. What is inheritance in Java?

Inheritance allows a child class to acquire accessible properties and methods from a parent class using the extends keyword.

Q4. What is polymorphism in Java?

Polymorphism allows methods or references to represent different forms of behavior. Java supports method overloading and method overriding.

Q5. What is encapsulation in Java?

Encapsulation combines data and related methods inside a class and controls access to data using access modifiers.

Q6. What is abstraction in Java?

Abstraction hides implementation details and exposes essential functionality through abstract classes and interfaces.

Q7. Does Java support multiple inheritance?

Java does not support multiple inheritance through classes, but a class can implement multiple interfaces.

Q8. Why is OOP important in Android development?

OOP helps developers create reusable, structured, and maintainable Android applications. Android Activities, custom classes, interfaces, and data models use OOP concepts.

12. SEO Details

SEO Title: Java OOP Concepts for Android: Inheritance, Polymorphism, Encapsulation and Abstraction

Meta Description: Learn the four pillars of Java OOP: inheritance, polymorphism, encapsulation, and abstraction with practical examples for Android Studio beginners.

Focus Keyword: Java OOP Concepts for Android

Related Keywords:

  • Java OOP concepts

  • Inheritance in Java

  • Polymorphism in Java

  • Encapsulation in Java

  • Abstraction in Java

  • Abstract class in Java

  • Interface in Java

  • Object-oriented programming

  • Android Studio Java tutorial

Suggested URL Slug: java-oop-concepts-android-inheritance-polymorphism

←
Post 8 of 40
→