Object Oriented Programming in Java
More number of developers are familiar and expert in procedural languages like C and Visual Basic. We can develop the software solution by procedural languages, then why it is required to move to object oriented programming (OOPS)? Why OOPS has better industry reception than procedural languages? Finally why Java runs on billions on devices than C and Visual Basic?
Object oriented programming gives us converting the real world system objects into software components. Basically it makes easier to understand the software solutions. OOPS helps us to develop reusable components such as abstract class implementation and inheritance. A software solution can’t stand on its own components, it is required to interact with third party components such as Oracle, SQL server, MySQL, JMS and other enterprise application components. Many enterprise software products builds a software solution for the enterprise. So drafting the standard to interact and specify the contract between the enterprise components is important to ease the integration. OOPS brings this by interfaces.
Interfaces convey the required behavior to the third party enterprise component providers. The third party providers implement the behaviors and Solutions architects (SA) don’t have to worry about the implementation as SA write the solution based on interfaces. Popular example for this is JDBC connectivity. Java published the interfaces such as java.sql.Connection, java.sql.Statement and java.sql.ResultSet to communicate with database. The database products would implement the interfaces to interact with their underlying database and publish the implementation as library (jar). The software solutions want to interact with the databases would import the library (jar) files and access the database. The popular jar “jdbc-oracle.jar” or “odbc7.jar” are implementation of JDBC interface for Oracle. Accessing the oracle database from java is as simple as calling a method. Did you feel that? Yes. It is.!! All these simple integration is possible with the help of OOPS concept interfaces and Abstraction.
In the above paragraph, we mentioned that java.sql.Connection is an interface for JDBC implementation.Connection is interface,what is “java.sql” in it? It is package name. It help us to keep things organized by placing related classes and interfaces with single package name. Here “java.sql” package would have all the classes and interfaces related to java SQL (database) connectivity.
So industry expected a programming strategy which is easy to understand, easy to integrate, easy to reuse and easy to group together. From that expectation, the Object Oriented Programming Paradigm begins. Let us begin with the core components of OOPS.
OOPS:
Objects: Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages)
Class: A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object.
Inheritance: Classes to inherit commonly used state and behavior from other classes. It gives the reusability of the code. Write once in one parent class and inherit the class to other subclasses. It would reuse the same code in parent class.
Interfaces: Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.
Packages: A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.
These 5 are core components in Object oriented programming. 3 more concepts needs to be discussed with OOPS components, Abstraction, Encapsulation and Polymorphism.
Abstraction and Encapsulation are not a core concept of OOPS. It is core concept of programming. OOPS gives ability to write code with abstraction and encapsulation. Writing highly cohesive and low coupling code is important to achieve this.It is programmer’s responsibility to write code which is abstracted and encapsulated properly. (Period). Polymorphism is one of the feature of inheritance and interfaces. It brings polymorphism capability in programming and `make wonders in compile and runtime.
Encapsulation is logically (judiciously) binding all the variables (instance variables) and the methods into a single unit called Class.
Abstraction is hiding implementation of methods and instance variables; exposing only the required methods signatures and accessor method to access the instance variables of the classes.
Polymorphism is the concept where an object behaves differently in different situations. This is common definition for polymorphism. The great feature explained in wrong wording and I do not prefer thisL. Polymorphism means a single object behaves logically and smartly expose the behavior based on it reference object. Stay tuned and will have detailed look into polymorphism with examples in further blogs.