Object Oriented Programming - 1
Object Oriented Programming - 1
A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to
a method. However, constructors have no explicit return type.
Typically, you will use a constructor to give initial values to the instance variables defined by the class, or to perform
any other start-up procedures required to create a fully formed object.
All classes have constructors, whether you define one or not, because Java automatically provides a default
constructor. However, once you define your own constructor, the default constructor is no longer used.
Constructor name must be the same as its class name.
A Constructor must have no explicit return type.
A Java constructor cannot be abstract, static, final, and synchronized.
Encapsulation:
The mechanism of hiding of data implementation by restricting access to public methods. Instance variables are
kept private and accessor methods are made public to achieve this. ( Setters & Getters)
Polymorphism:
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability
of a message to be displayed in more than one form.
Real life example of polymorphism: A person at the same time can have different characteristic. Like a man at
the same time is a father, a husband, an employee. So the same person posses different behavior in different
situations. This is called polymorphism. ( Overloading & Overriding )
Inheritance
the mechanism by which one class is allowed to inherit the features(fields and methods) of another class.
abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to
the user. In other words, the user will have the information on what the object does instead of how it does it.
Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase
the speed of car or applying brakes will stop the car but he does not know about how on pressing the accelerator
the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of
accelerator, brakes etc. in the car. This is what abstraction is.
Abstraction & interface