
Class: A class defines the properties and behavior (variables and methods) that is shared by all its objects. It is a blue print for the creation of objects.
Object: Object is the basic entity of object oriented programming language. Class itself does nothing but the real functionality is achieved through their objects. Object is an instance of the class. It takes the properties (variables) and uses the behavior (methods) defined in the class.
Encapsulation, Inheritance and Polymorphism are main pillars of OOPs. These have been described below :
Encapsulation: Encapsulation is the process of binding together the methods and data variables as a single entity. This keeps both the data and functionality code safe from the outside world. It hides the data within the class and makes it available only through the methods. Java provides different accessibility scopes (public, protected, private ,default) to hide the data from outside. For example, we can create a class "Check" which has a variable "amount" to store the current amount. Now to manipulate this variable we can create methods. For example to set the value of amount create setAmount() method and to get the value of amount create getAmount() method . Here is the code for "Check" class :
class Check{ public class Mainclass{ |
Here the data variable "amount" and methods setAmount() and getAmount() are enclosed together with in a single entity called the "Check" class. These two methods are used to manipulate this variable i.e. set and get the current value of amount.
Inheritance: Inheritance allows a class (subclass) to acquire the properties and behavior of another class (superclass). In java, a class can inherit only one class(superclass) at a time but a class can have any number of subclasses. It helps to reuse, customize and enhance the existing code. So it helps to write a code accurately and reduce the development time. Java uses extends keyword to extend a class.
class A{ public class C{ |
In the above example, class B extends class A and so acquires properties and behavior of class A. So we can call method of A in class B.
Polymorphism : Polymorphism allows one interface to be used for a set of actions i.e. one name may refer to different functionality. Polymorphism allows a object to accept different requests of a client (it then properly interprets the request like choosing appropriate method) and responds according to the current state of the runtime system, all without bothering the user.
There are two types of polymorphism :
- Compile-time polymorphism
- Runtime Polymorphism
Method overloading means having multiple methods with same name but with different signature (number, type and order of parameters).
class A{ // Here compiler decides that fun1(int,int) obj.fun1(2,3); |
In rumtime polymorphism, the method to be invoked is determined at the run time. The example of run time polymorphism is method overriding. When a subclass contains a method with the same name and signature as in the super class then it is called as method overriding.
class A{ |
In the above program, obj has been declared as A type. In line 1, object of class A is assigned. Now in the next line, fun1(int) of class A will be called. In line 3, obj has been assigned the object of class C so fun1(int) of class C will be invoked in line 4. Now we can understand that same name of the method invokes different functions, defined in different classes, according to the current type of variable "obj". This binding of method code to the method call is decided at run time.
No comments:
Post a Comment