Abstraction in java. In English
Abstraction
Another way to describe data abstraction is the act of focusing just on an object's necessary features while ignoring its extraneous aspects. An object's characteristics and actions set it apart from other objects of the same type and aid in classifying and organizing the objects. Java's abstract classes and interfaces allow for abstraction. Using interfaces, we can achieve 100% abstraction.
Java Abstract classes: -
- A class declared using the abstract keyword is known as an abstract class.
- All abstract methods may or may not be included in an abstract class. Some of them might be practical techniques.
- No object of an abstract class is possible. In other words, the new operator cannot be used to instantiate an abstract class directly.
- The default constructor is always present in an abstract class, however parameterized constructors are also possible.
- A method that is declared but not implemented is called an abstract method.
- Overriding is required or the subclass becomes abstract since a method-defined abstract needs to be redefined in the subclass at all times.
- An abstract keyword must be used when declaring any class that has one or more abstract methods.
- Choose the interfaces or classes that will be included in the abstraction.
- Make an abstract class or interface that defines these classes' shared attributes and behaviors.
- Within the abstract class or interface, define abstract methods devoid of implementation details.
- Implement the interface or create concrete classes that extend the abstract class.
- In the concrete classes, override the abstract methods to supply their particular implementations.
- To put the program logic into practice, use the concrete classes.
abstract class Animal {
private String name;
public Animal(String name) { this.name = name; }
public abstract void makeSound();
public String getName() { return name; }
}
class Pig extends Animal {
public Pig(String name) { super(name); }
public void makeSound() {
System.out.println(getName() + " oinks"); }
}
class Cat extends Animal {
public Cat(String name) { super(name); }
public void makeSound() {
System.out.println(getName() + " meows");}}
public class AbstractionExample {
public static void main(String[] args) {
Animal myDog = new Pig("Bulky");
Animal myCat = new Cat("Fluffy");
myPig.makeSound();
myCat.makeSound(); }}
- It makes things seem less complicated.
- Reduces duplication of code and improves reusability.
- Helps to improve an application's or program's security by giving the user access to only the information that is necessary.
- It makes the application easier to maintain.
- It enhances the application's modularity.
- The fact that we can make any kind of change to our internal system without having an impact on end users makes the enhancement quite simple.
- Increases the maintainability and reusability of programs.
- keeps implementation specifics hidden and only displays pertinent data.
- Understanding how a system operates might become more challenging when abstraction is used.
- More complexity may result from it, particularly if improperly used.
- This could reduce the implementation's flexibility.
- If abstraction is not used properly, it can add needless complexity to code, increasing the time and effort required for development.
- Debugging and comprehending code can be more difficult when abstraction is present, especially for people who are not familiar with the implementation details and abstraction layers.
- Because of the extra code layers and indirection that come with excessive abstraction, performance may suffer.
Comments
Post a Comment