Wednesday, 1 April 2015

Welcome Here!

This blog is written for the beginners we will start with the basics and trigger toward the concepts we need to understand to learn OOP (Object Oriented Programming)and language we will discuss here is Java.!
So if you want to learn java you need to have a grip on OOP first and later if you want to work on Android Development you may find your way seamless. So lets start reading all concepts are more theoretical here you can try to make it practical on your own environment some examples will be given to show how it works.

First question raised in our mind why do we need to use OOP base programming ? Can't we do all the things with procedural-programming ? Whats the change or fact that it become so obvious to use OOP based languages? Well the answer is OOP has taken the programming revolutionary to the next level. It helps you to consider solving the real world problems, everything in this world is an object and that's exactly you can do in programming now, you can play with objects. Shed some light on the origin of this concept i would like to tell you there was a la"Simula" called the very first OOP language but later "Smalltalk" language was concidered the very first language to bring truly OOP based concepts in programming. Following list revolves in OOP:
  • Object
  • Class
  • Inheritance
  • Aggregation
  • Polymorphism
  • Abstraction
  • Encapsulation
Now i'll explain each one by one. Lets look at object first, its simple objects in real world are that has some weight and occupies some space, well in OOP objects are one that has a value and attribute the only difference is that in real world objects are physical but in OOP we have objects logical.
Classes contains different methods, instance basically they are collection of different objects.
Inheritance There are two school of thoughts from code reuse point of view, one is Inheritence where we have "is-a" relationship and we need to continuously reuse our code. Second thought is having a "has-a" relation which is also known as 'Aggregation' we will read about it later. Inheriteance is a run-time process where classes uses attributes of parent class and enlarge its functionality by acquiring objects of parent class. Inheritance represents "is-a" relationship it has three types:
  • Single level Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance 

There are further two more types which comes under the implementation of interface, we will discuss them later
//Suppose we have Single level Inheritance
public class A{
int a=20;
}

//Another class which is extended form A 
public class B extends A{

public void printit(){
  System.out.println("This is the value of variable a:"+a+"!");
  }

public static void main(String args[]){
B obj = new B();
obj.printit(); 
}
}

Output:
"This is the value of variable a:20"

//NOTE: Here we can see that there was nothing in class B but we make the object of class B word because we inherit class B with parent class A
When we will have multi-level inheritance then there will be more than two classes. e.g

//Suppose we have Multi-level Inheritance
public class A{
int a=20;
public A(){
  System.out.println("This is class A");
 }
}
public class B extend A{
int b=30;
public B(){ 
    System.out.println("This is class B");
  }
}

//Another class which is extended form A 
public class C extends B{
int c=10;
public C(){
   System.out.println("This is class C");
 }
void show(){
  int x=a+b+c;
  System.out.println("Sum of integer from all classes:"+x);
}
}
public static void main(String args[]){
C obj = new C();

obj.show(); 
}
}
Output:
This is class A
This is class B
This is class C
Sum of integer from all classes:60

//NOTE 
Aggregation is used when there is a "has-a" relationship and you want to reuse code you need to make object of the class in another class so that you can access the attributes of the class. Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.
e.g. Employe "has-a" salary

//Suppose abstract method
class Employee{int id;
String name;
Salary salary; //Now this is class type object
salary.calculate(30); //code reuse
}
class Salary{
void calculate(int x){
     int y;
     y=30*x;
     return y;
  }
}
Polymorphism is a Greek word, poly means many and morph mean shapes. We can use it in our programming language in number of different ways and techniques for code sharing and reuse. Whenever any method or class makes the methods of the base class versatile that they can be used in different ways is called polymorphism. Basically methods overloading and method overloading comes in handy for term polymorphism. If a class has a function and the inherited class has same function name but different functionality will make it versatile. Anything which does that we will call it polymorphism whether it is method overriding, overloading, or implementation or extension of a class at the same time e.g.  
//Suppose we have interface
public interface Herbivore{}

//Simple class
public class Animal{}

//Extending simple class & implementing the interface make more than one 'is-a'    //relationship
public class Goat extends Animal implements Herbivore{}

//This example doesn't show the overriden method or overloading of method but still its full filling the general definition of extension of class so we will call it polymorphism
we can perform Method "overriden" and "overloading" for polymorphism as well.
Difference between method overloading and overriding, there may a question may came in your mind that what these term mean ? are they similar? Well the answer is NO they are not similarity they have actually different functionality.
Method overloading always:

  • defined within a class
  • increase readability because faces got add up
  • a compile time polymorphism
  • different number of parameter or different return types

Example: 

class overloadit{

static int add(int a, int b){return a+b;}

static int add(int a, int b, int c){return a+b+c;}
}
 Method overriding always:
  • used to provide specific information already defined in super class
  • can be used if you need to add up functionality
  • occurs in two class where there is "is-a" relationship
  • example of runtime polymorphism
  • have same return types & parameter you can't change them
Example: 
//Overriding
class Animal{
void eat() {System.out.println("doing eat");}
}
class Dog extended Animal{
void eat() {system.out.println("eating bread");}
}
Abstraction are those classes which promise the sub-classes that they will use the method they have initialized. If we declare an abstract method its class also become abstract, abstract methods are declared with just the initial statement with a semi colon we don't need its body. These methods are also called 'pure virtual methods' that they can't have their body another sub class have to define the body whom are not abstract themselfs. Abstract classes may have non-abstract method its like an abstract class be from 0 to 100% abstract
//Suppose abstract method
abstract void mountainbike();
When a method is declared as an abstract in a class that class should also be declared as abstract class.
//Suppose we abstract class with abstract method
public abstract class bike{
   abstract void mountainbike();
}
This was how you can use the pretty basics of abstract classes but the scope of abstract classes are little shorter later it will be compared with interfaces too. For now keep in mind that only one abstract class can be extended at a time, but you can define the access specifiers differently (public, protected, private) with classes and methods as well. 
  • You should consider using abstract classes for following reasons:
    • You want to share code among several closely related classes.
    • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
    • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Interfaces are those classes whom have collection of abstract methods, there is no much difference in working of interface class it works pretty much like abstract class but the scope of interface classes differ at some point. Interfaces must have only abstract methods it can not have non-abstract methods, and by interfaces multiple inheritance is achieve able in java. But interface can't implement the abstract class and they have only static and final variables.They also represents "is-a" relationship. Interface will be extended with Interfaces only.
Encapsulation does the functionality of code visible only and it will hide all the back process of this functionality. We access the private data from public methods. For this process we use access specifiers , it provide you control over the data we can cover the code under those access specifiers that's when we will call it encapsulation

No comments:

Post a Comment