-
Notifications
You must be signed in to change notification settings - Fork 7
OOPS
Abhishek Khare edited this page Jan 10, 2017
·
7 revisions
- The beauty of encapsulation is the power of changing things without affecting its users.
- In Java, you achieve encapsulation by hiding details using the accessibility modifiers (public, protected, private, plus no modifier which implies package private). With these levels of accessibility you control the level of encapsulation, the less restrictive the level, the more expensive change is when it happens and the more coupled the class is with other dependent classes.
- The idea is to provide a public interface through which you gain access to this data. You can later change the internal representation of the data without compromising the public interface of the class. On the contrary, by exposing the data itself, you compromise encapsulation, and therefore, the capacity of changing the way you manipulate the data without affecting its users.
- There are several reasons why you might want to encapsulate access to your fields. For example:
- You can limit the values that can be stored in a field
- You can take actions when the field is modified (trigger event, validate, etc).
- You can provide thread safety by synchronizing the method.
- You can switch to a new data representation (i.e. calculated fields, different data type)
- Encapsulated Code is more flexible and easy to change with new requirements.
- Encapsulation in Java makes unit testing easy.
- Encapsulation in Java allows you to control who can access what.
- Encapsulation also helps to write immutable class in Java which are a good choice in multi-threading environment.
- Encapsulation reduce coupling of modules and increase cohesion inside a module because all piece of one thing are encapsulated in one place.
- Encapsulation allows you to change one part of code without affecting other part of code.
- Example in Java is Arrays.asList().
- Through the process of abstraction, a programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency.
- In the process of abstraction, the programmer tries to ensure that the entity is named in a manner that will make sense and that it will have all the relevant aspects included and none of the extraneous ones. A real-world analogy of abstraction might work like this: You (the object) are arranging to meet a blind date and are deciding what to tell them so that they can recognize you in the restaurant. You decide to include the information about where you will be located, your height, hair color, and the color of your jacket. This is all data that will help the procedure (your date finding you) work smoothly. You should include all that information. On the other hand, there are a lot of bits of information about you that aren't relevant to this situation: your social security number, your admiration for obscure films, and what you took to "show and tell" in fifth grade are all irrelevant to this particular situation because they won't help your date find you. However, since entities may have any number of abstractions, you may get to use them in another procedure in the future
- In OOP, we often organize classes in hierarchy to avoid duplication and reduce redundancy. The classes in the lower hierarchy inherit all the variables (static attributes) and methods (dynamic behaviors) from the higher hierarchies. A class in the lower hierarchy is called a subclass (or derived, child, extended class). A class in the upper hierarchy is called a superclass (or base, parent class). By pulling out all the common variables and methods into the superclasses, and leave the specialized variables and methods in the subclasses, redundancy can be greatly reduced or eliminated as these common variables and methods do not need to be repeated in all the subclasses.
- A subclass inherits all the variables and methods from its superclasses, including its immediate parent as well as all the ancestors. It is important to note that a subclass is not a "subset" of a superclass. In contrast, subclass is a "superset" of a superclass. It is because a subclass inherits all the variables and methods of the superclass; in addition, it extends the superclass by providing more variables and methods.
- Polymorphism is one of the principle of object oriented programming. "Poly" means many and "morph" means forms hence the name polymorphism. Polymorphism also referred to as one name many forms or having one name with multiple functionality.
- In simple words you can use same method name with different signature or same signature but in different class.So depending on a data type it processes objects differently and an ability to redefine methods for a derived classes.
- There are basically two types of polymorphism
- Static Polymorphism - Static polymorphism is also called as Compile Time polymorphism. In Static polymorphism methods are overloaded with same name but having different signatures.So it is called as method overloading.
- Dynamic Polymorphism - Dynamic polymorphism is also called as Run Time polymorphism. In this type of polymorphism methods have the same name, same signature but different in the implementation. In Dynamic polymorphism methods are overridden so it also called as method overriding. During run time, Method overriding can be achieved by using inheritance principle. So this type of polymorphism can also be called as late binding. In late binding compiler doesn't know what kind of methods it has to call and which can be achieved only during the run time. So it is called as run time polymorphism.
-
Association - It represents a relationship between two or more objects where all objects have their own lifecycle and there is no owner. An association relationship can be represented as (also known as cardinality) one-to-one, one-to-many and many-to-many.
- The relationship between a doctor and a patient. A doctor can be associated with multiple patients and at the same time, one patient can visit multiple doctors for treatment and/or consultation.
- Parking and Car.
- Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers.
-
Aggregation - It is a specialized form of Association where all object have their own lifecycle but there is ownership. This represents “whole-part or a-part-of” relationship.
- an employee may belong to multiple departments in the organization. However, if the department is deleted, the employee object wouldn't be destroyed.
- Wallet and Money classes. Wallet has Money but money doesn’t need to have Wallet necessarily so its a one directional relationship. In this relationship both the entries can survive if other one ends.
- Car and Driver.
-
Composition - It is a specialized form of Aggregation. It is a strong type of Aggregation. In this relationship child objects does not have their lifecycle without Parent object. If a parent object is deleted, all its child objects will also be deleted. This represents “death” relationship.
- a house is composed of one or more rooms. If the house is destroyed, all the rooms that are part of the house are also destroyed as they cannot exist by themselves.
- Human and Heart. A human needs heart to live and a heart needs a Human body to survive. In other words when the classes (entities) are dependent on each other and their life span are same (if one dies then another one too) then its a composition.