Sunday 23 December 2018

OOPS basics



Abstraction:

remember it from example of Hypervisor, the way Hypervisor Abstract the underlying resource on same SERVER, and provide basic interface for VM creation , VM feels as if it owns the server.



Abstraction in Java is provided with "Interface".

Example:

ICar < -- MarutiCar
ICar <-- HondaCar

The client will be provide with reference to only Car interface, even though actual object ca be of any type.

Client can invoke, only those operation, which are defined in interface.

Hence abtracting the underlying different implementation of different car types.

at run time only it will be called .
Hence abstracting the underlying complexity of actual implementation

Encapsulation


Can be seen as a strategy used in order to provide abstraction.
Hide private variable, protect them from getting visible to subclasses.
and visible to the clients, when they hold the object reference, they shouldn't be able to access your internal private attributes and change it.

Its a way to implement "Single Responsibility" principle from SOLID design principle
i.e. you complete one task by your self without leaking your data hence allowing your responsibilities to be managed/alters by someone else

How Encapsulation helps

Implementation 


 Can you access non static variable in static context ?

A static variable in Java belongs to its class and its value remains the same for all its instances. A static variable is initialized when the class is loaded by the JVM. 






No comments:

Post a Comment