Sunday 17 February 2013

OOPs

The four main concepts are involved in OOP:
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
Abstraction is hiding of information from others. In case of Java, its hiding implementation of a method/ object. For example if an interface is available to outer world, so end user won’t know about implementing class details. This is just a kind of abstraction. Abstraction could be anywhere in java, wherever we hide some information, we call it abstraction.
Encapsulation is combining data and method along with implementation hiding within the class. Implementation hiding is done with the help of access modifiers (public, protected, package, private).
Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. Although inheritance may sometimes imply (especially in Java, where the keyword for inheritance is extends) that you are going to add new methods to the interface, that’s not necessarily true. The second and more important way to differentiate your new class is to change the behavior of an existing base-class method. This is referred to as overriding that method.
Polymorphism is something like one name many forms. Polymorphism is also known as dynamic binding or late binding or run-time binding.

---------------------------------------------------------------------------------------------------------------------------
Inheritance?

Definition 1:
Java Inheritance defines an is-a relationship between a superclass and its subclasses. This means that an object of a subclass can be used wherever an object of the superclass can be used. Class Inheritance in java mechanism is used to build new classes from existing classes.
Definition 2:
Inheritance means to acquire property of others. It also means that we can use code of other functions and methods. Means Code reusability. In this object are defines by classes that are super class and which classes uses property of that class is known as sub class. It is very useful in Object Oriented Languages.
-------------------------------------------------------------------------------------------------------------------------
What is Association?
Association is a relationship between two classes. In this relationship the object of one instance perform an action on behalf of the other class. The typical behaviour can be invoking the method of other class and using the member of the other class.

   
public class MyMainClass
{
 public void init()
  {
    new OtherClass.init();
  }
}
-------------------------------------------------------------------------------------------------------------------------
What is Aggregation?

Aggregation has a relationship between two classes. In this relationship the object of one class is a member of the other class. Aggregation always insists for a direction.
   
public class MyMainClass
{
  OtherClass otherClassObj = new OtherClass();
}
-------------------------------------------------------------------------------------------------------------------------
What is Composition?
Composition is a special type of aggregation relationship with a difference that its the compulsion for the OtherClass object to exist for the existence of MyMainClass.

import java.util.ArrayList;

public class Aggregation
{
 public static void main(String[] args)
  {
    MusicPlayer player = new MusicPlayer();
    Car car = new Car();
    car.addPlayer(player);
  }
}

No comments:

Post a Comment