Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, March 10, 2025

The Object class, abstract classes and methods

 

The Object Class in Java

The Object class is the root class of the Java class hierarchy. Every class in Java implicitly extends the Object class if no other superclass is specified. It provides several useful methods that are available to all Java classes.


📌 1. Key Methods of Object Class

Method

Description

equals()

Checks if two objects are equal.

hashCode()

Returns a hash code value for the object.

toString()

Returns a string representation of the object.

getClass()

Returns the runtime class of the object.

clone()

Creates a new object that is a copy of the existing one.

finalize()

Called by the garbage collector before an object is destroyed.

wait(), notify(), notifyAll()

Methods for thread synchronization.


Example of Object Class Methods

public class Example {

    String name;

    int age;

 

    public Example(String name, int age) {

        this.name = name;

        this.age = age;

    }

 

    @Override

    public boolean equals(Object obj) { // Overriding equals() method

        if (this == obj) return true;

        if (obj == null || getClass() != obj.getClass()) return false;

        Example other = (Example) obj;

        return age == other.age && name.equals(other.name);

    }

 

    @Override

    public String toString() { // Overriding toString() method

        return "Name: " + name + ", Age: " + age;

    }

 

    public static void main(String[] args) {

        Example obj1 = new Example("Krishna", 33);

        Example obj2 = new Example("Krishna", 33);

 

        System.out.println(obj1.toString());  // Displaying object details

        System.out.println("Are both objects equal? " + obj1.equals(obj2));  // Comparing objects

    }

}


Output

Name: Krishna, Age: 33

Are both objects equal? true


🔍 Explanation

  • equals() is overridden to provide a meaningful comparison between two objects based on their data.
  • toString() provides a readable string representation of the object.

Abstract Classes and Methods in Java

Abstract classes and methods are used to achieve abstraction, which hides the implementation details from the user and only exposes the necessary functionalities.


📌 2. Abstract Classes

An abstract class is a class that is declared with the abstract keyword. It cannot be instantiated, and it may or may not include abstract methods.


Syntax

abstract class Animal {  // Abstract class

    abstract void sound();  // Abstract method

 

    public void eat() {  // Non-abstract method

        System.out.println("Eating...");

    }

}


📌 3. Abstract Methods

An abstract method is a method that is declared without an implementation. The implementation must be provided by the subclass.


📌 4. Example of Abstract Class and Method

abstract class Shape {

    String color;

 

    public Shape(String color) {

        this.color = color;

    }

 

    // Abstract method (must be implemented by subclasses)

    abstract double area();

 

    // Non-abstract method

    public String getColor() {

        return color;

    }

}

 

class Circle extends Shape {

    double radius;

 

    public Circle(String color, double radius) {

        super(color);

        this.radius = radius;

    }

 

    @Override

    double area() {  // Implementing abstract method

        return Math.PI * radius * radius;

    }

}

 

class Rectangle extends Shape {

    double length, width;

 

    public Rectangle(String color, double length, double width) {

        super(color);

        this.length = length;

        this.width = width;

    }

 

    @Override

    double area() {  // Implementing abstract method

        return length * width;

    }

}

 

public class Main {

    public static void main(String[] args) {

        Shape circle = new Circle("Red", 5);

        Shape rectangle = new Rectangle("Blue", 4, 6);

 

        System.out.println("Circle Color: " + circle.getColor() + ", Area: " + circle.area());

        System.out.println("Rectangle Color: " + rectangle.getColor() + ", Area: " + rectangle.area());

    }

}


Output

Circle Color: Red, Area: 78.53981633974483

Rectangle Color: Blue, Area: 24.0


🔍 Explanation

  1. Abstract Class (Shape):
    • Has both abstract (area()) and non-abstract (getColor()) methods.
  2. Concrete Classes (Circle, Rectangle):
    • Implement the abstract method area() in their own way.
  3. Polymorphism:
    • The objects circle and rectangle are treated as objects of the Shape class, demonstrating runtime polymorphism.

📌 Important Points

  1. An abstract class can have constructors and static methods.
  2. An abstract method must be implemented by the subclass.
  3. If a class contains even one abstract method, it must be declared abstract.
  4. Abstract classes can have fields and methods with any access modifiers (public, protected, private).
  5. A subclass inheriting an abstract class must provide implementations for all its abstract methods or be declared abstract itself.

 

No comments:

Post a Comment