Java | Object Orineted Programming Concepts

05 min read

The primary motivation for the development of various languages is to manage the increasing complexity of programs that are manageable and reliable.

The disadvantage of a structured programming language like C is a process-oriented model ie., it gives importance to what is happening.There is no code reusability. (with inheritance concepts). C++ is basically a procedural language with object-oriented extension. Java is a pure object-oriented language, latest Cii.

Object-Oriented Programming:

Allows us to decompose a problem into a number of entities called objects and then build data and functions around these entities. It is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand. ie., object is considered to be a partitioned area of computer memory that stores data & a set of operations that can access the data. Since the memory partitions are independent, the objects can be used in a variety of different programs without modifications.

Three Object-Oriented Programming Principles:

  • Encapsulation
  • Inheritance
  • Polymorphism

.1. Encapsulation:

The mechanism that binds together data and code that manipulates it and keeps them safe from outside interference & misuse. Insulation of the data from direct access is Data Hiding. In Java, the basis of encapsulation is the class.

Class: Defines the structure & behavior (data & code) that will be shared by a set of objects. Each object of a given class contains the structure and behavior defined by the class. Thus, objects are referred to as instances of a class. A class is a data type & hence cannot be directly manipulated. It is a logical construct. 

Object: Instance of a class data type. It is a physical reality. Instantiation of an object is the process of creating an object of a particular class.

Code and data of a class are its members. Data is called member variable where the Class variable (static variables) and Instance variable (non-static variables). Code that operates on the data is called member method or just method. Purpose of a class is to encapsulate complexity which is done by making each method or variable in a class as private or public. The public interface of a class represents everything that external users of the class need to know or may know. The public interface should be carefully designed not to expose too much of the inner workings of the class.

Data Abstraction: Act of representing essential features without including the background details or explanations. It is the process of abstracting common features from object and procedures and creating a single interface to complete multiple tasks. Eg: a programmer may note that a function that prints a document exists in many classes may abstract that function, creating a separate class that handles any kind of printing.

2. Inheritance:

Process by which object of one class acquires the properties of objects of another class. It provides the idea of reusability. We can add additional features to an existing class without modifying it. Each subclass (derived class) defines only those features that are unique to it. Without this, each object would need to define all of its characteristics explicitly.

3. Polymorphism:

Creating a generic interface to be used for a group of related activities. It is the ability to take more than one form. Eg: the operator '+' can be used to add 2 numbers. '+' can also be used to concatenate 2 strings.

Binding: Linking of a procedure call to the code to be executed in response to the call.

Dynamic Binding: The code associated with a given procedure call is not known until the time of the call at runtime. It is associated with polymorphism & inheritance.

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error