Java is purely object oriented programming language which means everything we write in Java is an Object and Class. We have various OOPS concepts but to understand and follow Java we just have to learn 4 of these OOPS concepts.
To start with the specialty of OOPS languages is they combine both Data and Action together instead of the structured and un-structured programming languages. Main OOPS concepts which related to Java and which actually are followed completely are
The process of hiding important data and showing only the essential data to other classes and implementing classes is called Abstraction. We observe Abstraction usually in dealing with the outside view of the object (interfaces). We generally implement Abstraction in Java using interfaces, abstract classes, abstract methods about which I have written in the following posts
Basics of Interfaces : http://learningjavaonline.blogspot.in/2013/02/basics-of-interfaces-in-java.html
Abstract Classes & Asbtract Methods : http://learningjavaonline.blogspot.in/2013/02/abstract-classes-abstract-methods-with.html
To start with the specialty of OOPS languages is they combine both Data and Action together instead of the structured and un-structured programming languages. Main OOPS concepts which related to Java and which actually are followed completely are
- Encapsulation
- Abstraction
- Polymorphism
- Inheritance
Before I get into deeper programming of these concepts, let us discuss one line definitions to understand what actually each of them means.
Encapsulation
A protection provided to the data and code from being accessed randomly by other data in the program and outside the class. Encapsulation is the technique by which methods and variables may be hidden using access specifiers like protected, private etc., and other details are shown using public access specifier. 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.
Example Program : http://pastie.org/6079461
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.
Important Notes on Encapsulation
- The fields of a class can be made read-only or write-only.
- A class can have total control over what is stored in its fields.
- The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.
Abstraction
The process of hiding important data and showing only the essential data to other classes and implementing classes is called Abstraction. We observe Abstraction usually in dealing with the outside view of the object (interfaces). We generally implement Abstraction in Java using interfaces, abstract classes, abstract methods about which I have written in the following posts
Basics of Interfaces : http://learningjavaonline.blogspot.in/2013/02/basics-of-interfaces-in-java.html
Abstract Classes & Asbtract Methods : http://learningjavaonline.blogspot.in/2013/02/abstract-classes-abstract-methods-with.html
Polymorphism
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared the type of a reference variable cannot be changed. The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object.
Inheritance
Simple mechanism used to structure the program into a proper understandable manner is called inheritance. Though objects are distinguished from each other by some additional features but there are objects that share certain things common. In object oriented programming classes can inherit some common behavior and state from others. Inheritance in OOP allows to define a general class and later to organize some other classes simply adding some details with the old class definition. This saves work as the special class inherits all the properties of the old general class and as a programmer you only require the new features. This helps in a better data analysis, accurate coding and reduces development time.
When we talk about inheritance the most commonly used keyword would be extends and implements. These words would determine whether one object IS-A type of another. By using these keywords we can make one object acquire the properties of another object. In depth about inheritance I will write another post soon.
0 comments:
Post a Comment