What Are Class, Object, Instance, Reference, Abstraction, Encapsulation, Polymorphism And Inheritance In Java?

2

2 Answers

Naureen Khan Profile
Naureen Khan answered
CLASS

A class is a user-defined data type. Sometimes in object-oriented programming basic data types are not enough to accomplish our needs. In such cases we can create our own data types like classes and structures. Within the class we can have data members and member functions of the class.

OBJECT and INSTANCE

To make use of the newly created class in your code you have to create an object of the class. An object created of a class is said to be an INSTANCE OF THE CLASS. If you create two objects they are two instances of the same class.

REFERENCE

There are two ways of passing arguments to functions 1) by value 2) by reference.

When an argument is passed by value the called function will create a new variable of this data type and copy the value of the argument into this.

For large arrays this is not ideal, as a lot of memory and time is used for copying purposes. In such cases we pass the address in memory where that variable is located. Through the memory address we can directly access to the variable.

ABSTRACTION

Sometimes we need to create a class that only acts as a parent class for other classes. This class is known as an abstract class. Such a class cannot instantiate an object; instead other classes inherit from it and implement its functions.

ENCAPSULATION

Encapsulation or data hiding (as it is sometimes called) simply means that the data members within your class are hidden, and inaccessible from outside the class. They can only be accessed through public member functions.

This is done to hide the implementation of the class from other users of the class, and to prevent accidental access of the class variables.

INHERITANCE

Inheritance allows us to extend the functionality of an existing class, and add our own custom features. For example, let's say your friend wrote a class for drawing circles, which you really liked. But you want to extend the same class so that it can draw circles of different colors. Rather than writing the circles class from scratch you can as him for the class, and inherit a new class from it and implement your custom logic in that.

POLYMORPHISM

POLY means many, MORPHISM means forms. What this means in OOP is that you can have two functions of the same name but with different arguments (number of arguments or data type of arguments) and the correct one will get called.

For example you can have:

Function GetMySize(int a);

And Function GetMySize (float b);

Answer Question

Anonymous