2

Deep Dive into Object Oriented Programming Concept

 2 years ago
source link: https://blog.knoldus.com/deep-dive-into-object-oriented-programming-concept/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Reading Time: 4 minutes Oops Concept

OOP Concept

Object-Oriented Programming (OOP) concept is a programming methodology that uses objects and classes and is also used to implement real world scenarios. Most important concepts of OOP are as follows:-

1.Class :-

A class is a user defined blueprint or prototype from which objects are created. It also represents the set of properties or methods that are common to all objects of one type.

A class declaration consists of following things :

  1. Modifiers: It can be public as well as default access.
  2. Class name: Class name are the Initial letter that is used while declaring any class.
  3. Superclass: A class can only extend one parent class which is a known as subclass.
  4. Interfaces: A class can implement one or more than one interfaces.
  5. Body: Body is surrounded by curly braces that is represented by { }.
Oops ConceptStructure of defining a class

2. Object:- 

Object is a real world entity. These are also self–contained which consists of methods and properties which makes data useful. Objects can be both physical and also logical data. It also contains addresses and takes up some space in memory. Some of the examples of objects also are dog, chair, tree etc. 

Here is an example of creating an object of type Employee.

Employee e = new Employee();

3. Encapsulation:-

Encapsulation means wrapping up or binding or data to make it more secure. It is also achieved by declaring the variables as private and providing public setter and getter methods to modify and view the variable values. In addition , the fields of a class are made read-only or write-only. This method also improves the re-usability. Encapsulated code is also easy to test for unit testing. 

4. Abstraction:-

Firstly Abstraction is the process of categorizing data. It also means to show only the necessary information to the client of the object.

There are two ways to achieve abstraction in java

  1. Abstract class
  2. Interface : It is also a 100% abstract class.
Oops ConceptCode Implementation of an Abstract Class.

5. Polymorphism Oops Concept:-

Firstly Polymorphism is a process that performs a single action in different ways. It occurs when we have many classes related to each other by inheritance. By using method overloading and method overriding, we can also perform polymorphism which is also important OOP concepts.

Polymorphism in java can be classified into two types:

  1. Static / Compile-Time Polymorphism:- Compile-Time polymorphism in java is also known as Static Polymorphism .It is which is achieved through Method Overloading.
  2. Dynamic / Runtime Polymorphism:- Runtime polymorphism in java is also known as Dynamic Binding which is used to call to an overridden method that is resolved dynamically at runtime rather than at compile-time. 
Oops ConceptCode Implementation of Polymorphism Concept

6. Inheritance Oops Concept :-

Inheritance is a method in which one object acquires/inherits another object’s properties, In this we can create new classes built on existing classes, i.e., when you inherit from an existing class, we can reuse methods and fields of the parent class. It also represents the parent-child relationship.To inherit a class, we use the extend keyword. 

There are five types of inheritance :-

  • Single level  

In this one class i.e., derived class inherits properties from its parental class.  This enables code reusability and also adds new features to the code. For Example: class b inherits properties from class a.

  • Multilevel

In this one class is derived from another class which is also derived from another class i.e., this class has more than one parental class.

  • Multiple

In this case, there is more than one superclass, and there can be one or more subclasses. 

Multiple inheritances are available in object-oriented programming with C++, but it is not available in Java. 

  • Hierarchical level 

In this concept one parental class has two or more derived classes or we can also say that two or more child classes has one parental class.

  • Hybrid inheritance

Hybrid Inheritance is the combination of multiple and multilevel inheritance.

Hybrid Inheritance in C++ Oops Concept

References :-


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK