Author Profile Altaf Shaikh
Altaf Shaikh

BE Engg | Python Dev | Content Writer | Founder of TeachMeBro | Blockchain Dev | Django

Basic Concepts of OOPs

October 15th 2020   29

Basic Concepts of OOPs
Basic Concepts of OOPs

By combining data and action, we will get many advantages over structural programming viz,

  • Object - Any entity that has state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.
  • Class - Collection of objects is called class. It is a logical entity.
  • Abstraction - Hiding internal details and showing functionality is known as abstraction. For example phone call, we don't know the internal processing.
  • Encapsulation - Binding (or wrapping) code and data together into a single unit are known as encapsulation.
  • Inheritance - When one object acquires all the properties and behaviors of a parent object, it is known as inheritance.
  • Polymorphism - Polymorphism is the ability of an object to take on many forms. 

Apart from these concepts, there are some other terms which are used in Object-Oriented design:

  • Coupling - It refers to the knowledge or information or dependency of another class.
  • Cohesion - It refers to the level of a component which performs a single well-defined task. 
  • Association - It represents the relationship between the objects. 
  •   Aggregation - It is a way to achieve Association.               
  • Composition - It is also  a way to achieve Association.   

OOP System

Object:

An object can be defined as an instance of a class, and there can be multiple instances of a class in a program. An Object contains both the data or variables (which knows something) and the function or method (which does something). For example - chair, bike, marker, pen, table, car, etc. An object consists of:

  • State: It is represented by attributes of an object. It also reflects the properties of an object.
  • Behavior: It is represented by methods of an object. It also reflects the response of an object with other objects.
  • Identity: It gives a unique name to an object and enables one object to interact with other objects.

 

OOPs Objects

Class:

A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:

  • Modifiers: A class can be public or has default access (In Java, methods and data members of a class/interface can have one of the following four access specifiers viz, private, public, default, protected. However, we cannot declare class/interface with private or protected access specifiers. Otherwise, program fails in compilation. Remember, nested interfaces and classes can have all access specifiers).
  • Class name: The name should begin with a initial letter (capitalized by convention).
  • Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  • Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  • Body: The class body surrounded by braces, { }.

OOPs class example    

 

Class Creation:

java class creation example

Object creation:

  • Object is stored in Heap memory
  • New keyword is used to allocate memory inside Heap memory
  • Constructor is used to define the size of an object
  • Reference variable is stored in stack memory
  • Dot(.) operator is used to access the variables and methods of the class

java object creation example