Java OOPC - Abstraction

 

Abstraction





 

Abstraction is that modify class or method using the ‘abstract’ key word.

We can keep abstract method if only the class is abstract.

If class is abstract there is no any difference, it is just a class. As a normal class we can work with it,

means that we can use non abstract methods and variables  or anything inside the abstract class.

 

Meaningless we can define using the abstraction. Meaningless is that There is not any code inside the method or we can say there is not body  inside the method.




In the  example, an abstract class `Animal` has been created, featuring an abstract method `makeSound()` and a non-abstract method `eat()`.

 

A subclass `Dog` has been derived from the superclass `Animal`. In this subclass, the abstract method `makeSound()` is implemented, providing a specific sound for a dog.

 

An object `d1` of the `Dog` class is then instantiated. Through this object, we invoke the methods `makeSound()` and `eat()`, demonstrating the usage of both the abstract and non-abstract methods from the inherited class.

 

Conclusion about Abstraction:


- The `abstract` keyword is utilized to define both abstract classes and methods.

- Abstract methods lack a specific implementation, meaning they don't contain a method body.

- If a class contains abstract methods, it should also be declared as abstract.

- Instances of an abstract class cannot be created; rather, it serves as a blueprint for its subclasses.

- To leverage the features of an abstract class, we create subclasses that inherit from it and instantiate objects of those subclasses.

- In the subclass, it is mandatory to override all abstract methods of the abstract class. However, if the subclass is declared as abstract, overriding becomes optional.

- Accessing static attributes and methods of an abstract class can be achieved using a reference to the abstract class.

Comments

Popular posts from this blog

Linked List Data Structures

Java OOPC -Encapsulation

Java OOPC - Interface