Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
146 views
in JAVA by (114k points)
What is the difference between abstraction and encapsulation in Java?

Please log in or register to answer this question.

1 Answer

0 votes
by (114k points)

Abstraction and encapsulation are two important concepts in object-oriented programming, and while they are related, they serve different purposes.

Abstraction is the process of focusing on essential features and ignoring unnecessary details. It allows you to create models of complex systems that simplify their structure, making it easier to reason about them. In Java, abstraction is achieved through the use of abstract classes and interfaces.

Encapsulation, on the other hand, is the process of hiding the internal details of an object from the outside world and exposing only what is necessary. It provides a way to control access to the internal state of an object, preventing unwanted modification and ensuring that it remains in a valid state. In Java, encapsulation is achieved through the use of access modifiers such as private, public, and protected.

In other words, abstraction is about modeling a system at a high level, while encapsulation is about controlling access to the internal details of an object. Abstraction simplifies the structure of a system, while encapsulation protects its integrity.

To summarize, abstraction and encapsulation are two different concepts in object-oriented programming, and while they are related, they serve different purposes. Abstraction is about simplifying the structure of a system, while encapsulation is about protecting the integrity of its objects.

Here's an example code snippet in Java that demonstrates both abstraction and encapsulation:

public abstract class Animal {
  private String name;
  
  public Animal(String name) {
    this.name = name;
  }
  
  public String getName() {
    return name;
  }
  
  public abstract void makeSound();
}

public class Dog extends Animal {
  public Dog(String name) {
    super(name);
  }
  
  public void makeSound() {
    System.out.println("Woof!");
  }
}

public class Main {
  public static void main(String[] args) {
    Animal animal = new Dog("Fido");
    System.out.println(animal.getName()); // Output: Fido
    animal.makeSound(); // Output: Woof!
  }
}
 

In this example, we have an abstract class Animal that defines a name field and a getName() method that returns the name of the animal. The Animal class also defines an abstract method makeSound() that must be implemented by any concrete subclasses.

The Dog class is a concrete subclass of Animal that implements the makeSound() method by printing "Woof!" to the console.

In the Main class, we create a new Dog object with the name "Fido" and assign it to an Animal variable. We then call the getName() method on the Animal variable to retrieve the name "Fido", and the makeSound() method to print "Woof!" to the console.

This example demonstrates abstraction because the Animal class simplifies the concept of an animal by focusing on its essential features (name and sound), and ignoring unnecessary details. It also demonstrates encapsulation because the name field is private and can only be accessed through the public getName() method, which prevents unwanted modification and ensures that the object remains in a valid state.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Apr 24, 2023 in JAVA by kvdevika (114k points)
0 votes
2 answers
asked Apr 24, 2023 in JAVA by kvdevika (114k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...