Oops Concepts in Java.

First let us look at advantages of oops over procedural languages:

1)OOPs makes development and maintenance easier where as in Procedure-oriented programming language it is not easy to manage if code grows as project size grows.
2)OOPs provides data hiding whereas in Procedure-oriented programming language a global data can be accessed from anywhere.
3)OOPs provides ability to simulate real-world event much more effectively. We can provide the solution of real word problem if we are using the Object-Oriented Programming language.Benefits of Object Oriented Programming:  Modularity, Information-hiding,  Code re-use,  Easy Debugging.



What is a class?
A class is a static piece of code that consists of attributes which don’t change during the execution of a program – like the method definitions within a class.
A class is a template of an object.

What is an object?
An object is an instance of a class.


The basic concepts in oops are :



  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Inheritance:




In simple terms a class acquiring the properties of parent class is known as inheritance. It provides code re-usablity.



Types  of inheritance:



  1. Single inheritance.
  2. Multilevel inheritance.
  3. Multiple inheritance.
  4. Hierarchical inheritance.
Single inheritance:


single inheritance, one class extends one class only. In multilevel inheritance, the ladder of single inheritance increases.

Multilevel inheritance:




Multiple inheritance:
not possible in java because of diamond problem



what is diamond problem?






In the diagram above, we have 2 classes B and C that derive from the same class – which would be class A in the diagram above. We also have class D that derives from both B and C by using multiple inheritance. You can see in the figure above that the classes essentially form the shape of a diamond –          which is why this problem is called the diamond problem.


The problem with having an inheritance hierarchy like the one shown in the diagram above is that when we instantiate an object of class D, any calls to method definitions in class A will be ambiguous – because it’s not sure whether to call the version of the method derived from class B or class C.


But Java have interface:

Java has interfaces which do allow it to mimic multiple inheritance. Although interfaces give us something similar to multiple inheritance, the implementation of those interfaces is singly (as opposed to multiple) inherited. This means that problems like the diamond problem – in which the compiler is confused as to which method to use – will not occur in Java.

Polymorphism:


As the name suggest "poly" means "many" and "Morphism" means "Forms"

i.e An object can take up many forms.

Polymorphism can be of two types.
  1. Compile Time/Static polymorphism.(where methods are binded at compile time).
  2. Run Time/Dynamic polymorphism.(where methods are binded at run time).

Static polymorphism :is acheived through Method Overloading.

Dynamic polymorphism:
in order to acheive dynamic polymorphism the below conditions should be satisfied. 
  • 1. There must be method overriding. 
  • 2. Subclass object must be assigned to a super class object.
example for dynamic polymorphism:


Abstraction:

Abstraction means using the equipment (or code) without knowing the details of working.
Example: 
  • You are using your mobile phone without knowing how a mobile operates internally. Just a click on a button connects to your friend. This is abstraction. That is, the details of mobile mechanism are abstracted. Similarly we use the code of somebody to get the functionality without knowing how the code is written or working. 
  • You are using printf() function to write to the DOS prompt without the awareness of what the code of printf(). One of the ways of achieving abstraction is inheritance. Through inheritance a subclass can use the super class methods as if they belong to it without caring their implementation details.
  • When you apply brakes in car you dont know the internal mechanism but you know what it does this is called as .

Ways to achieve Abstaction

There are two ways to achieve abstraction in java
  1. Abstract class (0 to 100%)
  2. Interface (100%)


Encapsulation:

Binding data with objects (generally through method calls) is known as encapsulation.
data 

In encapsulation, to have control over the manipulation of data (not to feed wrong data, for example, the speed cannot be negative) by other classes, a programmer declares variables as private and methods as public. Other classes can access the private variables through public methods. With encapsulation, every object maintains its own data and this data is entirely private to that object. Other objects cannot access or modify the data.

It includes both Data hiding and data binding. Data hiding means one object cannot access the data of other object. data binding means every object has its own copy of data binded to it..

Data binding leads to data hiding and data hiding leads to encapsulation.












Comments

Popular posts from this blog

FC Barcelona vs Real Madrid -El Clasico Rivalry.

1NF,2NF, 3NF Normalization

Memory Management (Contiguous and Non-Contiguous ).