Java OOPs Concept, Constructors, PolyMorphism, Constructor Overloading, Static Keyword

Object Oriented Features

--

OOPs is a method of implementation where a co-operative collection of objects may be created, accessed and deployed.

Features:

  1. Classes and Objects
  2. Data Abstraction
  3. Data Encapsulation
  4. Polymorphism
  5. Inheritance
  6. Persistence
  7. Extendability
  8. Message Passing

Class

A class is a description of similar kind of objects or class is a blue print for several objects. A class is a logical entity that gives description of related objects.

Object

Object is a real world entity or An object is an instance of a class. An object is a physical entity which occupies certain space in memory.

Data Abstraction

It is a process of exhibiting the essential details by hiding the unwanted details on demand. It is a process of creating a new datatype called ADT [ Abstract Data Type].

Data Encapsulation

The process of wrapping up data and its methods into a single unit is nothing but encapsulation.

Poly Morphism

The ability to provide multiple definitions with same method signature.

Inheritance

The ability to extent the features of one class to another class is nothing but inheritance. The main Objective is code reusability.

Persistence

The process of making the object state permanent is nothing but persistence. The object by nature is temporary.

Extendibility

The ability to enhance the features or properties of the system without restructuring the existing system.

Message Passing

The process of invoking the method over an object is nothing but message passing.

An example program for demonstrating class and objects.

Output:

Constructors

A constructor is a special method that initializes an object of a class. Initialization is a process of assigning values to the data methods (or) instant variables of a class. Unless an object is initialized it cannot be kept in use.

A constructor initializes such objects when ever they are created. It is the first function to be executed when ever the object is created. It is executed only once and is invoked/implicitly called by the compiler.

The constructor must have the same name as its class name. Generally the constructor should be declared as public as it is meant for object initialization it doesn’t return any value. Even void should not be mentioned.

An example program for demonstrating Constructors

Output:

Parameterized Constructors

A constructor that accepts data as an argument is a parameterized constructor.

Output:

Static keyword

In a class either data or method can be declared as static.

  1. In case of data as static the variable becomes sharable among all the object of the class has only one instance of the static variable is created in the method.
  2. In case of method as static, it doesn’t use the object of a class. When a class doesn’t hold any data members then it is better to declare the method as static. A static method can be increased with the help of class name.
  3. A class method can have access to static data only.

Output

Polymorphism

It is the ability to provide multiple definations to the same method signature.

Here poly means many and morphism means forms.

Advantages

Polymorphism is used to build complex software system where the methods that form similar activity can have the same name.

The compiler is able to discriminate one method with another based on arguments. To be more specific the number of arguments, type of arguments and order of arguments.

Syntax of a method:

A method signature doesn’t include returntype. Here some of the valid and invalid methods are given below.

Now suppose we extend Employee class as follows −

Now, you study the following program carefully and try to determine its output −

Output

Here, we instantiate two Salary objects. One using a Salary reference s, and the other using an Employee reference e.

While invoking s.mailCheck(), the compiler sees mailCheck() in the Salary class at compile time, and the JVM invokes mailCheck() in the Salary class at run time.

mailCheck() on e is quite different because e is an Employee reference. When the compiler sees e.mailCheck(), the compiler sees the mailCheck() method in the Employee class.

Here, at compile time, the compiler used mailCheck() in Employee to validate this statement. At run time, however, the JVM invokes mailCheck() in the Salary class.

This behavior is referred to as virtual method invocation, and these methods are referred to as virtual methods. An overridden method is invoked at run time, no matter what data type the reference is that was used in the source code at compile time.

Constructor Overloading

As a constructor is also a mthod, it can also be overloaded.

If a class consists of multiple constructors it provides flexibility in creating objects for class.

Using this() in constructor overloading

this() reference can be used during constructor overloading to call default constructor implicitly from parameterized constructor. Please note, this() should be the first statement inside a constructor.

--

--