Tuesday, June 30, 2015

Some interesting Java Facts

Creation of an Object:

Following steps are completed while creating a new object of any class :-
1. Bind Constructor parameters.
2. If explicit this(),call recursively and skip to step 5.
3. Call recursively the implicit and explicit super(...),except for their Object class
because it has no parent class.
4. Execute the explicit instance variable initializers. // it refers to initialisation of instance variables at the time of their declaration.
5.  Execute the body of the current constructor.

Exceptions

1.finally clause is always executed regardless of the fact that exception is caused or not.
2.only way that finally will not be executed is virtual machine shutdown or execution of "System.exit(0)" method.
3.If return statement is embedded inside the try block then finally clause executes before return.
4. Exception must be thrown in same line where its object is created because it contains line no. information.

Accessing Members of "super base" Class

Suppose class B extends class A and class C further extends class B.
Now Suppose we want to access the members of class A directly through C class.
This can be either accomplished by using super twice once in C and once in B.
Or other way is to use super in Class C and if members with same name are not
present in B then this super will directly refer to member in super base class A.
example of a such a program.

class A
{
 static
 {
  System.out.println("static initializer of A");
 }
 {
  System.out.println("initializer of A");
 }
 A()
 {
  System.out.println("Constructor class A");
 }
 A(int a)
 {
  System.out.println("Constructor class A parameter");
 }
 void hello()
 {
  System.out.println("Hello in c");
 }
 void quality()
 {
  System.out.println("Quality Method Class A");
 }
}
class B extends A
{
 static
 {
  System.out.println("static initializer of B");
 }
 {
  System.out.println("initializer of B");
 }
 B()
 {
  System.out.println("Constructor class B");
 }
 B(int b)
 {
  System.out.println("Constructor class B parameter");
 }
 void show()
 {
  System.out.println("I m in class B");
 }
}
class C extends B
{
 static
 {
  System.out.println("static initializer of C");
 }
 {
  System.out.println("initializer of C");
 }
 C()
 {
  System.out.println("Constructor class C");
 }
 C(int c)
 {
  //super.show();
  System.out.println("Constructor class C parameter");
 }
 void show()
 {
  System.out.println("I m in class C");
 }
 void quality()
 {
  super.quality();
  System.out.println("Quality Method Class C");
 }
}
public class inh
{
 public static void main(String ...args)
 {
  A a=new C(3);
  a.quality();
 }
}

Polymorphic Arguments in Polymorphism

Suppose a method in a class uses reference of some base class as its argument to perform certain calculations. Now it can also intake the object of child class as its argument because child is also a type of base class object. Bu important point is that this method can only access the members of parent class and not child specific features.

Referring members of a base class in Polymorphism

If we create reference of base class then we can access child class methods which are overridden but child class specific methods are not accessible as accomplished by following program:
class base
{
 base()
 {
  System.out.println("base class");
 }
 void show()
 {
  System.out.println("Show method base class");
 }
}
class child extends base
{
 child()
 {
  System.out.println("child class");
 }
 void show()
 {
  System.out.println("Show method child class");
 }
 void specific()
 {
  System.out.println("Specific Method of Child class not present in base");
 }
 public static void main(String args[])
 {
  base b=new child();
  b.specific();//illegal will not compile
  b.show();//child class method executes
 }
}

Encapsulation

Encapsulation is one of the four fundamental OOP concepts. 
Grouping functions and corresponding data into a single capsule is called Encapsulation
Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

Encapsulation =Datahiding + Abstraction

Example:

Let us look at an example that depicts encapsulation:
/* File name : EncapTest.java */
public class EncapTest{

   private String name;
   private String idNum;
   private int age;

   public int getAge(){
      return age;
   }

   public String getName(){
      return name;
   }

   public String getIdNum(){
      return idNum;
   }

   public void setAge( int newAge){
      age = newAge;
   }

   public void setName(String newName){
      name = newName;
   }

   public void setIdNum( String newId){
      idNum = newId;
   }
}
The public methods are the access points to this class's fields from the outside java world. Normally these methods are referred as getters and setters. Therefore any class that wants to access the variables should access them through these getters and setters.
The variables of the EncapTest class can be access as below::
/* File name : RunEncap.java */
public class RunEncap{

   public static void main(String args[]){
      EncapTest encap = new EncapTest();
      encap.setName("James");
      encap.setAge(20);
      encap.setIdNum("12343ms");

      System.out.print("Name : " + encap.getName()+ 
                             " Age : "+ encap.getAge());
    }
}
This would produce following result:
Name : James Age : 20

Benefits of Encapsulation:

  • The fields of a class can be made read-only or write-only.
  • A class can have total control over what is stored in its fields.
  • The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.

My Profile

My photo
can be reached at 09916017317