Object Oriented Principles
Java is object-oriented programming language. They key principles of object-oriented programming are
Abstraction
Encapsulation
Inheritance
Polymorphism
Abstraction
Abstraction is hiding or managing complexity. For example, when we think of a car, we think of well-defined object with states and behaviour. While driving a car we ignore many internal details of the car like engine, gear system, brake system etc. We manage the complexity of the car through abstractions. Abstractions allows us to shield with inevitable changes in the sub systems.
Encapsulation
Encapsulation is binding of data and the methods that operate on the data. For example, you can make a method that changes a variable as private. This keeps the data safe from outside misuse of the data.
Encapsulation acts like a protective shield that prevents code and data from outside interference. Access will be allowed through well-defined interface. In Java class we have variables and methods.
Inheritance
Inheritance is that one object called as sub class or child class acquires or inherits state and behavior from another object called as super class or parent class. The child class and the parent class share a “IS-A” relationship. For example, cat is an animal. In Java, we use extends keyword to use inheritance.
public Cat extends Pet {
…
}
Polymorphism
Polymorphism means having many forms and shapes. Java supports static as well as dynamic Polymorphism. It has the ability to process multiple objects of various classes through a single interface. Any object in Java is polymorphic in nature. We will discuss more about static and dynamic polymorphism in detail in another post.