Sunday 19 February 2017

Java OOP’s Concepts




Java OOP’s Concepts:

Java is purely an object oriented programming language. This is very different from procedural programming language. With object oriented programming, a problem will be broken down into a number of units. These units are called objects.
If you have never used an object-oriented programming language before, you will need to learn a few basic concepts before you can begin writing any code.


Object:

Objects, which represent the entities in the real-world problem that the program is trying to solve, form the building blocks of any Java program. A class is a template (or a blueprint) from which objects are created. So, in your source code, you do not write an object, you write a class, and the code to create objects from the class. When a program is running, the objects are created and do the job for which you created them. Object can be physical or logical (tangible and intangible). The example of intangible object is banking system.


Characteristics of objects are:

State: what the objects have, the data (value) represents the state of an object.
For Example: Students have a first name, last name, age, etc
Behavior: what the objects do, Operation performed on data (value) represents the behavior of an Object.
For Example: Student attend a course "Java for beginners"
Identity: What makes them unique, every object with the same properties and behavior has their own individual identity. This unique identity is implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.
Object is an instance of a class: Class is a template or blueprint from which objects are created. So object is the instance (result) of a class.
Example: A recipe of a cake is a template so it’s a class and cakes that are made from that recipe are the objects.


Class:

A class is a template (or a blueprint) from which objects are created. So, in your source code, you do not write an object, you write a class, and the code to create objects from the class. When a program is running, the objects are created and do the job for which you created them.


Inheritance:

Inheritance is a mechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes. A class derived from another class is called a subclass, whereas the class from which a subclass is derived is called a superclass.
Use of inheritance in Java:

  • • For Method Overriding (so runtime polymorphism can be achieved).
  • • For Code Reusability.


Polymorphism:

The term polymorphism comes from:
Poly = many
morphism = the ability to change
Polymorphism is the ability of the programmer to write methods of the same name that does different things for different types of objects, depending on the needs of those objects.
In java, we use method overloading and method overriding to achieve polymorphism.


Abstraction:

Abstraction in Java is used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object (interface).
In java, we use abstract class and interface to achieve abstraction.


Encapsulation:

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.



 Next Topic Shahbaz

0 comments:

Post a Comment

Powered by Blogger.

Stats