Since Java came into existence there is a buzz relates to whether Java is an OOP or not. Still there are many criticizes java for its OOP concepts.
Java supports all the concepts and properties of OOP, this is shown as below:
Basic Concepts of OOP
Abstraction:
In Java we can have as much number of classes as we want and put them all in a package. In this way we can have a package having all set of functionality each of which is a standalone class.
Encapsulation:
Java supports encapsulation by introducing the concept of interfaces.
Interfaces are an agreement between the class designer and the programmer.
Interfaces just provide the functionality that needs to be present in the class and not the actual implementation.
The best Java programming practice says that we should program to an interface.
An interface can be implemented by any number of classes and at the same time two classes implementing the same interface need not be worried about how the other provides the implementation of a particular method; all it knows is that the other class does provide a certain method.
For example:
public interface myinterface{
// declaration of methods and variables.
}
public class myclass implements myinterface{
//implementation of class goes here
}
Polymorphism:
We can call the child class methods having the same name using the base class object. The child classes may have the same signatures of a method but the base class object decides which one to call. This way polymorphism is achieved in Java.
The syntax of this is:
baseobj = new childobj ();
baseobj.childfunc ();
Properties of OOP
Class:
In Java all the attributes and methods are declared inside a class only. Nothing can be outside the boundaries of class in Java. For example:
public class Myclass{
// all the declaration of variables, functions etc go here.
}
Object:
In Java a before we can use the functionality of the class; its object needs to be instantiated. Using the following syntax:
Myobject obj = new Myobject (); or
Myobject obj = new Myobject (list of parameters);
Method:
A class may have several functionalities which are implemented as methods. Methods are the only means by which we can make use of the functionalities provided by the class. For example:
public class Myclass{
// declaration of variables.
public returntype func1(the list of parameters separated by comma){
//the code goes here.
}
private void func2(){
//the code goes here.
}
//similarly a class can have any number of functions.
}
Inheritance:
In java we can inherit a class and make use of the functionality it provides. Java supports only multilevel inheritance and not multiple inheritance. For example:
public class myclass extends baseclass {
//implementation goes here
}
Message Passing:
We can invoke a different class’ method on that class’ object. This takes care of message passing between one active object and other. The syntax for this is:
Obj.methodname (); or
Obj.methodname (list of comma separated parameters);
/* Here the object obj is already instantiated*/
Thus we can see that as Java supports all the concepts and properties of the Object Oriented Programming paradigm it is an Object Oriented Programming Language.
No comments:
Post a Comment