- Data Hiding & Encapsulation
- Dynamic Binding
- Dynamic binding (dispatch) means that a block of code executed with reference to a procedure (method) call is determined at run time.
- Message Passing
- Encapsulation is the mechanism of hiding of data implementation by restricting access to public methods.
- Data-hiding - public getter, setter methods, variables of class remain private
- Abstract means a concept or an Idea which is not associated with any particular instance. Using abstract class/interface we express the intent of the class rather than the actual implementation. In a way, one class should not know the inner details of another in order to use it, just knowing the interfaces should be good enough.
- Abstract class and methods. Employee abstract class's
computePay()methods is implemented differently by a class called
- Inheritances expresses "is a" relationship between two objects. Using proper inheritance, In derived classes we can reuse the code of existing super classes.
- Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.
extends- Dog extends Animal
It means one name many forms. It is further of two types - static and dynamic. Static polymorphism is achieved using method overloading (resolved at compile time) and dynamic polymorphism using method overriding (resolved at runtime).
The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
- A Deer IS-A Animal
- A Deer IS-A Vegetarian
- A Deer IS-A Deer
- A Deer IS-A Object
- public: Anyone can access, usually for methods only
- private: can be accessed by same class, usually for attributes
- protected: accessed by same class or its child classes & same package. Recommended for att/methods common in a family. Accessible by parent & sub-classes only
- None (Default): Only accessible to classes in same Java package. Package private visibility.
During developer testing, there is often a need to inject stubs into the code to isolate the SUT from other collaborating objects so that it can be tested independently. This is dependency injection (i.e.: replacing the collaborating objects with test-friendly objects such as stubs or mock objects).
Coupling is a measure of the degree of dependence between components. Low coupling = component is less dependent on other components. Strive for low coupling.
Cohesion is a measure of how strongly-related and focused the various responsibilities of a component are. Strive for high cohesion.
Inheritance is when a 'class' derives from an existing 'class'. So if you have a Person class, then you have a Student class that extends Person, Student inherits all the things that Person has. There are some details around the access modifiers you put on the fields/methods in Person, but that's the basic idea. For example, if you have a private field on Person, Student won't see it because its private, and private fields are not visible to subclasses.
Polymorphism deals with how the program decides which methods it should use, depending on what type of thing it has. If you have a Person, which has a read method, and you have a Student which extends Person, which has its own implementation of read, which method gets called is determined for you by the runtime, depending if you have a Person or a Student. It gets a bit tricky, but if you do something like
the read method on Student gets called. That's the polymorphism in action. You can do that assignment because a Student is a Person, but the runtime is smart enough to know that the actual type of p is Student.
Polymorphism is useful because we can design other classes for the most general version of this object. The methods called upon this object will be the implementation in the specific classes which inherits from the general class.
Abstraction, as the name suggests, allows us to abstract the complex details of implementation away from the functionality. For example, to send an email to someone, we only need to know how to use the GUI to type the content and hit send. What happens after that - the protocols being called, the mail server actions and handling are all hidden from the user.
Similarly, in programming, abstraction keeps the implementation details away and exposes only the functionality.
abstract keyword is an abstract class. A method can be defined
abstract. Declaring a method to be
abstract means the class has to be definded
Note: Abstract classes cannot be instantiated. Instead, they have to be
extended by another class which implements these abstract methods.
An interface is a collection of abstract methods. A class implements an interface and hence, it will inherit all the abstract methods from the interface.
Differences between a class and interface:
- You cannot instantiate an interface.
- An interface does not contain any constructors.
- All of the methods in an interface are abstract.
- An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
- An interface is not extended by a class; it is implemented by a class.
- An interface can extend multiple interfaces.
- A class can implement more than one interface at a time.
- A class can extend only one class, but implement many interfaces.
- An interface can extend another interface, in a similar way as a class can extend another class.