JAVA OOPS CONCEPTS

The Java Object Oriented Programming was been developed to overcome the drawbacks of conventional programming approaches. In this module we will learn about general OOP concepts like Class, Object, Data Abstraction, Data Encapsulation, Modularity, Inheritance, Polymorphism.

Table of Contents

[hide]
  • Why Java OOP's
  • Programming Paradigms
  • Basic Concepts of OOP

Why Java OOP's

Object Oriented Programming System (OOP's) is a programming approach that offers a new and powerful way of managing complexity. With the rapid change in technologies, industries are evolving and are finding a new approach to software design and development.

Traditional languages struggled to handle the increased Complexity of Software. Large programs that were written were more prone to errors due to this complexity. The structured Programming approach which was once the most popular approach also failed to tackle the drawbacks like bug-free code, easy to maintain, and reusable codes.

Object Oriented Programming approach is more powerful in terms of handling the errors, easily maintaining the program, reuse the existing code. Hence we will learn about such concepts which will help us to overcome the drawbacks of conventional programming approach.

The most adapted and popular object-oriented languages are Java, Python, C++, C# etc.

Programming Paradigms

Paradigms is way of doing some task or an appraoch to solve some problem. Lets say a method is used to solve some complex problem using some appraoch which can be referred to as Paradigms. In this module we will learn about three Paradigms.

  • 1. Procedural Programming
  • 2. Modular Programming
  • 3. Object Oriented Programming

Procedural Programming

Procedural Programming is a programming model with a list of instructions where each statement tells coomputer to do something. It is derived from structured programming, based on the concepts of calling the function with series of computational steps involved.

The Paradigms is to choose the best algorithm and decide which Procedure you want. Procedural programming supports this Paradigms by providing parameters to functions (sub-programs) and returning values from it.

Procedural programming is good when used with general-purpose programming. In Procedural programming, the code becomes harder to write and sometimes the code is not reusable to be used in other applications.

Procedural Programming languages :COBOL, Pascal, FORTRAN and C etc.

Modular Programming

Modular Programming is way to break down large programs into smaller units i.e functions (sub-programs). It can also be defined as grouping of components that carry a specific tasks into a larger entity called module.

Since many modules access the same data, it becomes critical that how we store data. To change the data, we will need to modify all the functions that access it.

Modular programming do not model the real world entity well, this can be stated as its one of the drawbacks that is overcome by Object oriented paradigms.

Object Oriented Programming

Object Oriented Programming revolves around objects and classes. A class is a group of objects sharing common properties, while object is an entity with certain characteristics and behaviors

In Object oriented programming more emphasis is layed on data rather than Procedures. It can solve almost all the real life problems in todays world.

Lets take an example to stimulate the crowd at airport. Using Procedural programming paradigm, the more focus will be on working happening in the airport i.e moving, halting or turning. But Object oriented programming will emphasis more on the objects and their interface i.e baggage carts, Shopping trollies, flights, people. If we write a program regarding this we can define a simple class of Airport with related objects.

Examples of Object Oriented Programming paradigm : Simula : First OOP language, Java : James Gosling, Python : Guido van Rossum etc.

Basic Concepts of OOP

Object Oriented Program System (OOP's) is a programming approach that offers a new and powerful complexity management approach. It simplifies the development of software by maintaining the code, providing bug-free solutions and using reusable code.

Some of the most popular concepts are :-

  • 1. Object
  • 2. Class
  • 3. Inheritance
  • 4. Polymorphism
  • 5. Abstraction
  • 6. Encapsulation
oopsconcepts

Object

An object in Java is an instance of a class and consists of properties and its behaviors. A class may contain many objects, object represent real-life entities such as dog, bed, fan, computer, chair, purse etc.

Java Objects resides insides heap memory area and contains some address. Object consists of some characteristics mainly State, Behavior and Identity.

Java objects comprises of states or variables which are combined together within an object, we can refer them as fields or member variables.

NOTE : We will learn about objects in detail in the next module Java Classes and Objects.

Class

The Class can be defined as a user-defined model that creates an object. A group of objects with common properties may also be referred to as a class.

A Class in Java contains fields, functions/methods, constructors etc. Class do not occupy any space in the memory.

Public class name in Java should be same as File name. It is a way to tell JVM that this is an entry point. Suppose if we construct a program in which there is more than one class file, then interpreting which class contains the program entry point will be ambiguous for Interpreter and JIT compiler.

NOTE : We will learn about objects in detail in the next module Java Classes and Objects.

Inheritance

Inheritance is the capability of one class to inherit the capabilities or properties from other class. It helps in code usabilty, which is one of the most important feature of Object Oriented Programming.

Lets take an Example of humans, we inherit from class humans certain properties like drinking, speaking, walking, bathing etc. But these properties are not common to humans, class human inherits these properties from a class called Mammal which again inherits from another class Animal.

Lets take another example of a class Child which inherits certain properties from a class called Father, which again inherit from another class GrandFather.

NOTE : We will learn about Inheritance in detail in upcoming tutorials Inheritance.

Polymorphism

Polymorphism is one of the main key features of Object-Oriented programming. Languages that support classes but not Polymorphism are known as Object-Based Languages, Ada is an example of such a language.

Polymorphism is the ability of an object of a class to behave differently. Here "Poly" means many and "morphs" means forms. Example could be mode of communcation, it could be face to face, over phone, over chat or video etc. Hence it can be done in many forms.

Java Polymorphism can be divided into two parts i.e Compile time Polymorphism and Runtime Polymorphism.

NOTE : We will learn about Polymorphism in detail in upcoming tutorials Polymorphism

Abstraction

Abstraction is the mechanism which simplifies real words concepts into its essential meaning i.e Showing essential functionality without including the background details or explanation.

Example : While you are driving a car, you know only the essential features such as gear handling, use of clutch, accelerator, you dont know about the internal details like motor working, wiring, these details are hidden. This is known as Abstraction where the internal details are hidden and only essential functionality is exposed.

NOTE : We will learn about Abstraction in detail in upcoming tutorials Abstraction.

Encapsulation

Encapsulation is the packaging up of data and its functions that operate on that data into a single unit. Encapsulation is a way to implement data Abstraction.

If you want to read a data item in an object, you need to call the member function in the object. You can not directly access the data, hence its also called data hiding.

Encapsulation can be accomplished by defining all variables in a class as private, and by writing public methods in the class to set and obtain the values of variables.

NOTE : We will learn about Encapsulation in detail in upcoming tutorials Encapsulation.