Pages

get our extension

Search program

Monday 12 August 2013

What is Object Oriented Programming(OOP)

è Object-oriented programming (OOP) is a programming language model organized around "objects" rather than "actions" and data rather than logic.


è Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data.

Difference between Procedure Oriented Programming (POP) and Object Oriented Programming(OOP)



        Procedure Oriented Programming(POP)
Object Oriented  Programming(OOP)
In POP, program is divided into small parts called functions.
In OOP, program is divided into parts called objects.
POP follows Top Down approach.
OOP follows Bottom Up approach.
POP does not have any access specifier.
OOP has access specifiers named Public, Private, Protected, etc.
POP does not have any proper way for hiding data so it is less secure.
OOP provides Data Hiding so provides more security.
In POP, Overloading is not possible.
In OOP, overloading is possible in the form of Function Overloading and Operator Overloading.
In POP, Most function uses Global data for sharing that can be accessed freely from function to function in the system.
In OOP, data can not move easily from function to function,it can be kept public or private so we can control the access of data.
Example of POP are : C, VB, FORTRAN, Pascal.
Example of OOP are : C++, JAVA, VB.NET, C#.NET.

Example Program of Method overloading and Method overriding

Example of method overloading:-

//dynamic polymorphism

class Sum
{
void add(int a, int b)
{
            System.out.println("Sum of two="+(a+b));
}

void add(int a, int b,int c)
{
            System.out.println("Sum of three="+(a+b+c));
}
}
class Polymorphism
{
      public static void main(String args[])
      {
            Sum s=new Sum();
            
            s.add(10,15);
         s.add(10,20,30); 
   }
 }

Output:-

Sum of two=25
Sum of three=60


è That means, JVM decides which method is called depending on the difference in the method signature.

Example of method overriding:-

//dynamic polymorphism

class A
{
void cal(double x)
{
            System.out.println("square value="+(x*x));
}
}
class B extends A
{
void cal(double x)
{
            System.out.println("square root="+Math.sqrt(x));
}
}
class Polymorphism
{
      public static void main(String args[])
      {
            A a=new A();
           
            a.cal(15);             
  }                   
} 
  
Output:-
square value=225.0                                                     
è When we create object of class B(B a=new B()), then out will be
square root=3.872983346207417

è That means JVM decides which method is called depending on the data type (class) of the object used to call the method.


Difference between method overloading and method overriding

 Method Overloading
  Method Overriding
Writing two or more method with the same name but with different signature is called method overloading
Writing two or more method with the same name and same signature is called method overriding
method overloading is done in the same class
Method overriding is done in super and sub classes.
In method overloading, method return type can be same or different.
In method overriding, method return type should also be same.
JVM decides which method is called depending on the difference in the method signature.
JVM decides which method is called depending on the data type (class) of the object used to call the method.
Method overloading is done when the programmer wants to extend the already available feature.
Method overriding is done when the programmer wants to provide a different implementation (body) for the same feature.
Method overloading is code refinement. Same method is refined to perform a different task.
Method overriding is code replacement. The sub class method overrides (replaces) the super class method.

What is final?

final is a keyword which is used in two ways:-

è It is used to declare constants

     final double PI=3.1415; //PI is constant

è  It is use to prevent inheritance

final class School; //subclass to School can not be created.

n  If we declare class as final that means we cannot create sub class of that class

Ex: - final class School

        Class Student extends School //invalid 

Polymorphism with example

è The ability to exist in different form is called Polymorphism
è In java , a variable, an object or a method can exist in different forms, thus performing various tasks depending on the context.
è If the same method performs different tasks, then that method is said to exhibit polymorphism.

There are two types of Polymorphism:

1.    Static polymorphism ----

 The polymorphism exhibited at compile time is called static polymorphism.

2.    Dynamic polymorphism----

à  The polymorphism exhibited at run time is called dynamic polymorphism.
è This means when a method is called, the method call is bound to the method body at the time of running the program, dynamically
è In this case, java compiler does not know which method is called at the time of compilation, only JVM knows at runtime which method is to be executed.
è Hence this is also called runtime polymorphism or dynamic binding.

Example of polymorphism

//dynamic polymorphism
class Sum
{
void add(int a, int b)
{
            System.out.println("Sum of two="+(a+b));
}
void add(int a, int b,int c)
{
            System.out.println("Sum of three="+(a+b+c));
}
}
class Polymorphism
{
      public static void main(String args[])
      {
            Sum s=new Sum();
            s.add(10,15);
            s.add(10,20,30);
      }

}

JAVA VIRTUAL MACHINE(JVM)

JVM  is responsible for taking the .class file and converting each byte code instruction into the machine language instruction that can be executed by the microprocessor.

In JVM, there is a module(or program) called class loader sub system, which perform the following functions:

è First it loads .class file into memory.
è Then it verifies whether all byte code instructions are proper or not. If it finds any instruction suspicious, the execution is rejected immediately.

è If the byte code instructions are proper, then it allocates necessary memory to execute the program.

   What is Byte Code ?

                     Java bytecode, a middle-language between Java (user language) and the machine language uses    by the JVM. This Java bytecode is the smallest unit that deploys the Java code.