Java Learning

 Java 

General Purpose:

Java is an Object oriented application programming language developed by Sun Microsystems. Java is a very powerful general-purpose programming language. It is a stupendous programming language which is not confined to machine applications only. Java is more than just a tool for building transportable multimedia applications. Due to its versatility, it is a platform independent language, be it a hardware platform or any operating system.
Java programs run as quickly and efficiently as C++ programs due to the implementation of the JVM (Java Virtual Machine). It adds to C++ in the areas of automatic memory management. Moreover it also extends the language support for multithreaded applications.
T he Java applet API is a framework that allows Java-enabled Web browsers to manage and display embedded Java applications within. Java applications can be executed by any software that implements the Java run-time system. Any kind of applications can be written in Java programming language such as any small or large applications, or any standalone application.

Java as OOP :

OOP stands for Object Oriented Programming. This is a technique used to develop programs revolving around the real world entities. In OOPs programming model, programs are developed around data rather than actions and logics. In OOPs, every real life object has properties and behavior. which is achieved through the class and object creation. They contains properties (variables of some type) and behavior (methods).
OOPs provides a better flexibility and compatibility for developing large applications.

There are four main pillars of an Object Oriented Programming Language :

  • Inheritance:
    Inheritance provide the facility to drive one class by another using simple syntax. You can say that it is a process of creating new class and use the behavior of the existing class by extending them for reuse the existing code and adding the additional features as you need. It also use to manage and make well structured software.
  • Encapsulation:
    Encapsulation is the ability to bundle the property and method of the object and also operate them. It is the mechanism of combining the information and providing the abstraction as well.
  • Polymorphism:
    As the name suggest one name multiple form, Polymorphism is the way that provide the different functionality by the functions  having the same name based on the signatures of the methods. There are two type of polymorphism first is run-time polymorphism and second is compile-time polymorphism.
  • Dynamic binding:
    It is the way that provide the maximum functionality to a program for a specific type at runtime. There are two type of binding first is dynamic binding and second is static binding.
As the languages like C, C++ fulfills the above four characteristics yet they  are not fully object oriented languages because they are structured as well as object oriented languages. But in case of java,  it is a fully Object Oriented language because object is at the outer most level of data structure in java. No stand alone methods, constants, and variables are there in java. Everything in java is object even the primitive data types can also be converted into object by using the wrapper class.
Class: A class defines the properties and behavior (variables and methods) that is shared by all its objects. It is a blue print for the creation of  objects. The primitive data type and keyword void is work as a class object.
Object:  Object is the basic entity of object oriented programming language. Class itself does nothing but the real functionality is achieved through their objects. Object is an instance of the class. It takes the properties (variables) and uses the behavior (methods) defined in the class.
The Encapsulation, Inheritance and Polymorphism are main pillars of OOPs. These have been described below :
Encapsulation:
Encapsulation is the process of binding together the methods and data variables as a  single entity. It keeps both the  data and functionality code safe  from the outside world. It hides the data within the class and makes it available only through  the methods. Java provides different accessibility scopes (public, protected, private ,default) to hide the data from outside.

create a class "Check" which has a variable "amount" to store the current amount. Now to manipulate  this variable we create a methods and to set the value of amount we create setAmount() method and to get the value of amount we create getAmount() method .

Here is the code for "Mainclass" class :

class Check{
  private int amount=0;

  public int getAmount(){
   return amount;
  }
  public void setAmount(int amt){
   amount=amt;
  }
}

public class Mainclass{
  public static void main(String[] args){
   int amt=0;
   Check obj= new Check();
   obj.setAmount(200);
   amt=obj.getAmount();
   System.out.println("Your current amount is :"+amt);

   }
}


Here the data variable "amount" and methods setAmount() and  getAmount() are enclosed together with in a single entity called the "Check" class. These two methods are used to manipulate  this variable i.e. set and get the current value of amount.

Here is the output of the example:

 C:\roseindia>javac Mainclass.java

C:\roseindia>java Mainclass
Your current amount is :200


Inheritance:
Inheritance allows a class (subclass) to acquire the properties and behavior of another class (superclass). In java, a class can inherit only one class (superclass) at a time but a class can have any number of subclasses. It helps to reuse, customize and enhance the existing code. So it helps to write a code accurately and reduce the development time. Java uses extends keyword to extend a class.

Here is the code of the example :

class A{
  public void fun1(int x){
   System.out.println("Int in A is :" + x);
  }
}
class extends A{
  public void fun2(int x,int y){
   fun1(6);  // prints "int in A" 

   System.out.println("Int in B is :" + x " and "+y);  
  }
}
public class 
inherit{
  public static void main(String[] args){
   B obj= new B();
   obj.fun2(2); 
   }
}


In the above example,  class B extends class A and so acquires properties and behavior of class A. So we can call method of A in class B.

Here is the output of the example:
C:\roseindia>javac inherit.java

C:\roseindia>java inherit
Int in A is :6
Int in B is :2 and 5


Polymorphism :
Polymorphism allows one interface to be used for a set of actions i.e. one name may refer to different functionality. Polymorphism allows a object to accept different requests of a client (it then properly interprets the request like choosing appropriate method) and responds  according to the current state of the runtime system, all without bothering the user.
There are two types of polymorphism :
  1. Compile-time polymorphism
  2. Runtime Polymorphism
In compiletime Polymorphism, method to be invoked is determined at the compile time. Compile time polymorphism is supported through the method overloading concept in java.
Method overloading means having multiple methods with same name but with different signature (number, type and order of parameters).

Here is the code of the example :

class A{
  public void fun1(int x){
  System.out.println("The value of class A is : " + x);
  }
  public void fun1(int x,int y){
  System.out.println("The value of class B is : " + x + " and " + y);
  }
}
public class polyone{
  public static void main(String[] args){
  A obj=new A();
// Here compiler decides that fun1(int) is to
 be called and "int" will be printed.

  obj.fun1(2); 
// Here compiler decides that fun1(int,int)is
to be called and "int and int" will be printed.

  obj.fun1(2,3); 
  }
}


Here is the output of the example:
C:\roseindia>javac polyone.java

C:\roseindia>java polyone
The value of class A is : 2
The value of class B is : 2 and 3



 

No comments:

Post a Comment