Concepts of OOP..


Sunday, March 1, 2015

classes, objects, methods..

Java Class

 

A class is like a template (or blueprint) telling how objects of that class would look. For instance, the class called planet will contains the different planets as its objects.


Classes can be declared in the following way:

class MyClass {
    // field, constructor, and 
    // method declarations
} 
 
Class declaration can also include Modifiers such as public,  
private etc. Class body (within the curly brackets) contains 
the objects and all the coding. A class can have fields, 
constructors and methods and more..
 

Objects 

 

Objects are instances of classes. When you create an object, that object will be of a certain class. Objects interact by invoking methods. Below is the syntax to create object:


new keyword:

The new keyword is used to allocate memory at runtime.


Above shows how the objects for the planet class can be created.




Methods

 

 A method is a set of code which is referred to by name and can be called simply by utilizing the method’s name. Following is the Syntax of method:








Methods are groups of operations that carry out a certain function together.Methods are typically used when you need to group operations together, that you need to be able execute from several different places.

Advantage of Method

  • Code Reusability
  • Code Optimization



 CONSTRUCTOR is a type of method which is used to initialize objects. Constructor will be invoked at the time of object creation. It constructs the values (provides data for the object) that is why it is known as constructor.

It looks like a method but the difference is Methods have return type but constructors don’t have any return type. And constructors must have the same name as the class name which does not require for methods.



  • Name of the constructor must be same as the name of class
  • They do not specify any return type not even void.
  • Constructor initialize an object immediately after creation.


Wednesday, December 10, 2014

Types of polymorphism

Polymorphism types

 

  Polymorphism is the ability of an object to take on many forms. It can happen when a method or a constructor is behaving differently in a class. There are 3 categories of polymorphism:
 

1. Method overloading

2. Method overriding

3. Constructor overloading


An operation can exhibit different behavior in different instances.This behavior depends on the types of data used in the operation.


Method Overloading happens when two or more methods of same name is defined in a class, provided that their argument list or parameters are different.

In Method Overriding, the Child class has the same method as of base class. In such cases child class overrides the parent class method without even touching the source code of the base class

Like method overloading,constructors can also be overloaded.Constructor overloading is way of having more than one constructor which does 2 different tasks.


METHOD: A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method’s name.

 CONSTRUCTOR: Constructor is a block of code, which runs when you use new keyword in order to instantiate an object. It looks like a method but the difference is Methods have return type but constructors don’t have any return type. And constructors must have the same name as the class name which does not require for methods.


Examples:

 

Method overloading:

class Overload
{
    void demo (int a)
    {
       System.out.println ("a: " + a);
    }
    void demo (int a, int b)
    {
       System.out.println ("a and b: " + a + "," + b);
    }
    double demo(double a) {
       System.out.println("double a: " + a);
       return a*a;
    }
}
class MethodOverloading
{
    public static void main (String args [])
    {
        Overload Obj = new Overload();
        double result;
        Obj .demo(10);
        Obj .demo(10, 20);
        result = Obj .demo(5.5);
        System.out.println("O/P : " + result);
    }
}
Here the method demo() is overloaded 3 times: first having 1 int parameter, second one has 2 int parameters and third one is having double arg. The method which is invoked/called depends on the type and number of parameters used for the input.



Method overriding:


public class BaseClass
{
    public void methodToOverride() //Base class method
    {
         System.out.println ("I'm the method of BaseClass");
    }
}
public class DerivedClass extends BaseClass
{
    public void methodToOverride() //Derived Class method
    {
         System.out.println ("I'm the method of DerivedClass");
    }
}

public class TestMethod
{
     public static void main (String args []) {
        // BaseClass reference and object
        BaseClass obj1 = new BaseClass(); 
        // BaseClass reference but DerivedClass object
        BaseClass obj2 = new DerivedClass(); 
        // Calls the method from BaseClass class
        obj1.methodToOverride(); 
        //Calls the method from DerivedClass class
        obj2.methodToOverride(); 
     }
}


Constructor overloading:


package beginnersbook.com;
public class StudentData
{
   private int stuID;
   private String stuName;
   private int stuAge;
   StudentData()
   {
       //Default constructor
       stuID = 100;
       stuName = "New Student";
       stuAge = 18;
   }
   StudentData(int num1, String str, int num2)
   {
       //Parameterized constructor
       stuID = num1;
       stuName = str;
       stuAge = num2;
   }
   //Getter and setter methods
   public int getStuID() {
       return stuID;
   }
   public void setStuID(int stuID) {
       this.stuID = stuID;
   }
   public String getStuName() {
       return stuName;
   }
   public void setStuName(String stuName) {
       this.stuName = stuName;
   }
   public int getStuAge() {
       return stuAge;
   }
   public void setStuAge(int stuAge) {
       this.stuAge = stuAge;
   }
}

class TestOverloading
{
   public static void main(String args[])
   {
       //This object creation would call the default constructor
       StudentData myobj = new StudentData();
       System.out.println("Student Name is: "+myobj.getStuName());
       System.out.println("Student Age is: "+myobj.getStuAge());
       System.out.println("Student ID is: "+myobj.getStuID());

       /*This object creation would call the parameterized
        * constructor StudentData(int, String, int)*/
       StudentData myobj2 = new StudentData(555, "Chaitanya", 25);
       System.out.println("Student Name is: "+myobj2.getStuName());
       System.out.println("Student Age is: "+myobj2.getStuAge());
       System.out.println("Student ID is: "+myobj2.getStuID()); 
  }
}
 
As you can see above, while creating the instance myobj
default constructor (StudentData()) gets called. But during 
the creating of myobj2, the arg-constructor 
(StudentDate(int, String, int))was called. Since both 
the constructors are having different initialization code, 
the variables value are different in each case.
 


 (These examples were taken from http://beginnersbook.com)

Sunday, November 16, 2014

Inheritance, Encapsulation, Polymorphism & Abstraction














In inheritance a new class is created from an existing class.

The word inheritance means inheriting a feature, property or anything from one to another.




In programming, some codes for one class can be inherited from another class so that coding time can be reduced.

The arrow head indicates from where it is inherited.
So A is inherited from B in the diagram above.


Below shows a real life example of Inheritance:



Here Herbivore, Carnivore and omnivore are inherited from the Animal.
Rabbit is inherited from Herbivore and so on...


Here shows another way of inheritance (multiple inheritance)

Here Omnivore is Inherited from both Herbivore and Carnivore!!







Encapsulation basically means hiding something within another.




 Encapsulation is used for hiding the code and data in a single unit to protect the data from the outside world. In programming, Encapsulation is wrapping and hiding properties and methods.






Encapsulate means to hide irrelevant data from the user .

Abstraction refers to showing only the necessary details to the intended user. 









Simply, polymorphism means a single action that is done in many different ways. For example in the picture below different animals are saying (perhaps the same thing) in different ways - which is their language.


And in the following picture there are girls and boys. On the 'girl' class and 'boy' class they have dressed differently. On the same class, same gender but their way of dressing is different. This is polymorphism.






    
Abstract means unrealistic or maybe a summary or anything which is difficult to interpret.
Below are three Abstract pictures. By looking at it we don't really understand what it is.




                       
In software development abstraction can be used for example if you have an new idea of a new part to include in the software but have no idea how to write the exact code for it. Then for this abstract idea you can make a separate abstract class.
In this abstract class only the relevant data about an object will be shown in order to reduce complexity and to increase efficiency. And everything else will be hidden.



Below are some links which will provide more explanations and examples for OOP concepts..