A complete tutorial guide for basic OOP concepts in Java. A complete interview guide to basic Java (OOP) concepts with the key points and differences to remember.
- Includes 4 pillars of OOP i.e. Abstraction, Encapsulation, Inheritance, Polymorphism.
- Overloading vs Overriding.
- Static (Class, Methods, Variables and Blocks).
- Access modifiers, packages, wildcards.
- Keywords like Final and Super.
OOP Concepts in Java – Basics
|1. 4 Pillars of OOP||2. Method Overloading||3. Method Overriding|
Q) What is OOP?
A) OOP, formally known as Object Oriented Programming, can be defined as a programming technique based on objects, instead of just functions and procedures. OOP binds data and instructions for processing the data together into an object that can be used within a program or in other programs.
Q) Advantages of Object Oriented Programming?
A) Advantages of OOP can be from various aspects
- Code Reuse: OOP enables the reuse of code from one program into another program through objects.
- Data Integrity: Through encapsulation, one of the 4 pillars of OOP, it becomes easier to achieve data integrity and consistency by validating data and restricting user from directly accessing the data/data members.
- Simple-Complex Design: OOP allows a designer to model large complex systems into simple and manageable designs.
- Software Maintenance: An Object Oriented Program is much easier to maintain as compared to a non-Object Oriented piece of software.
Q) Define a Class?
A) A class represents description of objects that share same attributes and actions. It serves as a template for creating, or instantiating, specific objects within a program.
Q) What is an Object?
A) An object is an entity that binds together the variables (state) and the methods (behaviors). It can be defined as bundle of related functions/methods and variables.
For Example, a car encapsulates variable (State) Red in Color and methods (Behaviors) like ‘Start’, ‘Stop’ etc., so does an object.
Characteristics of an Object:
An object has
- an Identity
- a State
- a Behavior
Q) What are 4 pillars of OOP? Explain each one of them.
Encapsulation can be defined as a process of binding data members (variables) and member functions (methods) in order to achieve and maintain data integrity and consistency. It is also known as a process of “data/information hiding” where the user is restricted from directly accessing and modifying the data members of a class.
In abstraction, we provide only essential/necessary features (methods) of an entity to the outside world (the user) while hiding other irrelevant details, in order to reduce operational complexity at the user-end.
In order to achieve abstraction, in object oriented programming languages, access modifiers like private and public are used. Therefore, to implement abstraction, set the essential features (methods) of the class at public and make the irrelevant ones private. The public ones will be available to the outside world, while the private ones will stay inaccessible.