‘Object oriented programming’ is a type of programming which involves object oriented principles like encapsulation, polymorphism and inheritance. These principles are implemented by means of classes, methods, variables and other constructs in Java. We saw a sneak peek into object oriented programming in the Java 101 post.
What is inheritance?
When classes and interfaces are subclassed, the sub-class or the sub-interface inherits the properties of the class above it. This is inheritance. Recall, that a class defines methods and variables and is the basic building block of programs in Java. Inheritance promotes code reuse. For example,
public class Car extends Vehicle{
}
The above code shows that ‘Car’ class extends from ‘Vehicle’ class and inherits the properties of ‘Vehicle’.
What is Encapsulation:
When the variables of a class are made ‘private’ and can be exclusively accessed only by its ‘public’ methods’, the class is said to exhibit the property of ‘Encapsulation’. Encapsulation ensures that the data and the methods are bound together and they can be accessed by other classes only by the classes public methods.
What is polymorphism?
Polymorphism is the ability of the object to take on different forms. For example, if we consider a method defined as ‘Shape’ in the super class, each sub-class namely Triangle, Square, Circle will have their own implementation of ‘Shape’. Thus the object ‘Shape’ takes on different forms in different sub-classes. This is the concept of ‘Polymorphism’.
This post is for alphabet ‘O’ for the Blogchatter challenge… the previous post is here...