Recent Posts

Sunday, 24 July 2016

OOPS Concepts Tutorial Part 3


10. Static Control Flow

Output
Process of static control flow
1. Identification of static members from top to bottom
i = 0 (RIWO) Read Indirect Write Only
j = 0 (RIWO)
(1-6) Steps.

2. Execution of static variable assignments and static block from top to bottom
i = 0 (R & W)
j = 0 (R & W)
(7-12) Steps.

3. Execution of Main Method.
(12-15) Steps.
If the variable is in RIWO state then we r not allowed to perform read operation directly, violation leads to C.E Saying “Illegal forward reference”.

static Blocks
     If we want to perform some activity at the time of class loading, Then we should define that activity at static blocks because these(static blocks) will execute at the time of class loading only.
     We have to load native libraries at the time of class loading. Hence we have to define this activity inside the “static block”.
E.g
class Native {
   static {
      System.loadLibrary("native Library path");
   }
}
     After loading database driver compulsory we should register with the driver manager. But it is not required to perform explicitly this registration because in every driver class there should be one static block to perform this registration.
     Hence at the time of loading the database driver automatically registration will be performed.
E.g
class DatabaseDriver {
   static {
      //Register Driver with Driver Manager
   }
}
     With out using main method we can able to print some statement to the console.
E.g
class StaticDemo {
   static {
      System.out.println("Hello....I can print ");
      System.exit(0);
   }
}
11. Instance Control Flow
Output
Process of instance control flow
     Instance control flow will execute at the time of object creation the following is the sequence of statements which will execute at the time of object creation.
1. Identification of Instance members
i = 0 (RIWO) Read Indirect Write Only
j = 0 (RIWO)
(1-8) Steps.

2. Execution of Instance variable assignments and Instance block from top to bottom
i = 0 (R & W)
j = 0 (R & W)
(9-14) Steps.

3. Execution of Main Method.
(15) Steps.
Note
     Static control flow is only one time activity and that will execute at the time of class loading. But instance control flow is not one time activity. And it will execute for every object creation
separately.

     If the variable is in RIWO state then we r not allowed to perform read operation directly, violation leads to C.E Saying “Illegal forward reference”

12. Constructor
* After Creation of Object compulsory we should perform initialization then only that object in a position to provide service to others.

* At the time of Object Creation some peace of code will execute automatically to perform initialization that peace of code is nothing but “Constructor”.

* Hence the main Objective of constructor is to perform initialization.

* If we don’t have constructor we will face burden. For example see the following code.
E.g
class Student {
   String name;
   int rollno;
   public static void main(String[] args) {
      Student s1 = new Student();
      Student s2 = new Student();
      Student s3 = new Student();
      Student s4 = new Student();
      s1.name = "Ashok";
   s1.rollno = 101;
      
   s2.name = "Vinod";
   s2.rollno = 102;
      
   s3.name = "Dillesh";
   s3.rollno = 103;
      
   s4.name = "Thiru";
   s4.rollno = 104;
      System.out.println(s1.name+"-----"+s1.rollno);
      System.out.println(s2.name+"-----"+s2.rollno);
      System.out.println(s3.name+"-----"+s3.rollno);
      System.out.println(s4.name+"-----"+s4.rollno);
    }
}
To over come this type of burden constructor was introduced.
E.g
class Student {
   String name;
   int rollno;
   Student(String name, int rollno) {
      this.name = name;
      this.rollno = rollno;
   }
   public static void main(String[] args) {
      Student s1 = new Student("raju",101);
      Student s2 = new Student("mani",102);
      System.out.println(s1.name+"-----"+s1.rollno);
      System.out.println(s2.name+"-----"+s2.rollno);
   }
}
Rules for Constructor
     While writing constructors we should follow the following rules.
1. The name of the constructor and name of the class must be same.

2. The only allowed modifiers for the constructors are public, private, protected, <default>. If we are using any other modifier we will get C.E(Compiler Error).

3. return type is not allowed for the constructors even void also. If we r declaring return type then the compiler treats it as a method and hence there is no C.E and R.E(RuntimeError).
class Test {
   void Test() {
      System.out.println("Hai .....");
   }
   public static void main(String arg[]) {
      Test t = new Test();
   }
}
     It is legal(But Stupid) to have a method whose name is exactly same as class name. Here it was treated as method.

Default Constructor
     If we are not writing any constructor then the compiler always generate default constructor. If we r writing at least one constructor then the compiler won’t generate any constructor. Hence every class contains either programmer written constructor or compiler generated default constructor but not both simultaneously.

Prototype of default constructor
1. It is always no-arg constructor.
2. It contains only one – line super();
This is a call to superclass – constructor it is no-argument call only.
3. The modifier of the default constructor is same as class modifier(either public or default).
     The first line inside any constructor must be a call to super class constructor(super()) or a call to overloaded constructor of the same class(this()). If we are not taking any thing then the compiler will always place super().

super() & this() in constructor
     We should use as first statement in constructor. We can use either super or this but not both simultaneously. Outside constructs we can’t use i.e we can invoke a constructor directly from another constructor only.

Overloaded Constructor
     A class can contain more than one constructors with different arguments. This type of constructors are called “overloaded constructor”.
E.g
class Test {
   Test() {
      this(10);
      System.out.println("No-arg constructor");
   }
   Test(int i) {
      this(10.5);
      System.out.println("int-arg");
   }
   Test(double d) {
      System.out.println("double-arg")
   }
   public static void main(String arg[]) {
      Test t1 = new Test();
      Test t2 = new Test(10);
      Test t3 = new Test(20.5);
      Test t4 = new Test('a');
      Test t5 = new Test(10l);
   }
}
     Inheritance concept is not applicable for constructor and hence overriding is also not applicable. If the parent class constructor throws some checked exception. Compulsory the child class constructor should throw the same checked exception or it’s parent other wise compile time error. If the parent class constructor throws unchecked exception then child class constructor not required to throw that exception.

13. Coupling
     It is also one of the feature of OOPs. The degree of dependency between the components is called coupling.
     These components are tightly coupled with each other. With out effecting any component we can’t modify any component. This type of programming is not recomanded. When ever we are developing the components compulsory we should maintain very less dependency
between the components i.e we have to maintain loosely coupling.

The main advantages of loosely coupling are:
1. It improves maintainability.
2. It makes enhancements easy.
3. It produces reusability.

14. Cohesion
     For every component we have to define a well defined single purpose. Such type of components are said to be followed high “cohesion”.
E.g
     If we define a single servlet for all required functionalities of mail application like displaying login page, Validating the user, Displaying Inbox page etc

     Then for every small change entire component will be disturbed and it reduces maintainability and
suppresses reusability of code.

     To resolve this problem. For every task we can define a separate component like login.jsp for displaying login page, validate servlet for validation purpose, inbox servlet, For displaying mails etc

     By using this approach we can perform the modification in any component with out effecting the remaining and it imporves maintainability. It also promotes reusability. When ever validation required the same validate servlet we can use.

The main advantages of High Cohesion are:
1) It improves maintainability.
2) Enhancements is easy.
3) Promotes reusability.

15. typeCasting
     General syntax of typecasting

Compile time checking 1
     There should be some relation ship between the type of ‘d’ and ‘c’. other wise we will get compile time error saying inconvertable types
found    : d
required : c
E.g
String s = “Ashok”;
Object o = (Object)s; // Fine
     Because Object is parent to String

String s = “Ashok”;.
Object o =(StringBuffer)s; // C.E

Compile time checking 2
     ‘c’ must be same or derived type of ‘A’ other wise compile time error. Saying incompatable types
found : c
required : A
E.g
String s = “Ashok”;
Object o = (String)s;
     Because String is child of Object.

String s = “Ashok”;
StringBuffer sb = (Object)s; // C.E

Runtime Checking
     The internal object type of ‘d’ must be same or derived type of ‘c’ other wise we will get RuntimeException saying
ClassCastException : ‘d’ type
E.g
Object o = new String(“Ashok”) ;
StringBuffer sb = (StringBuffer)o; //C.E


Object o = new String(“raju”);
String s = (String)o; // Fine
     Because internal Object type of o is String , so ‘c’(String), ‘d’(String) are same

Consider following figure

Base ob1 = new Derived2(); //Fine
Object ob2 = (Base)ob1; // Fine
Derived2 ob3 = (Derived2)ob2; // Fine
Base ob4 = (Derived2)ob3; // Fine
Derived2 ob5 = (Derived1)ob4; //R.E ClassCastException

Next Tutorial  Collection Framework Tutorial

Previous Tutorial  OOPS Concepts Tutorial Part 2

No comments:

Post a Comment