Posts

Showing posts from September, 2025

Encapsulation in Java

Image
Encapsulation in java:- Encapsulation is defined as the wrapping up of data under a single unit.  It is the mechanism that binds together code and the data it manipulates. Other way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield. Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared. As in encapsulation, the data in a class is hidden from other classes using the data hiding concept which is achieved by making the members or methods of class as private and the class is exposed to the end user or the world without providing any details behind implementation using the abstraction concept, so it is also known as combination of data-hiding and abstraction.. Encapsulation can be achieved by: Declaring all the variables in the class as private and writing public methods in...

Abstraction in Java

 Abstraction in Java   Data Abstraction is the property by virtue of which only the essential details are displayed to the user.The trivial or the non-essentials units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components. Ways to achieve Abstraction There are two ways to achieve abstraction in java Abstract class  Interface Abstract class in Java:- A class which is declared with the abstract keyword is known as an abstract class in java. It can have abstract and non-abstract methods (method with the body). A class which is declared as abstract is known as an abstract class . It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated. Points to Remember An abstract class must be declared with an abstract keyword. It can have abstract and non-abstract methods. It cannot be instantiated. It can have constructor and static methods also. It can have final method...