Wednesday 1 February 2017

Java Code Snippets For Practice

Here is the collection of all the java code snippet starting from "Hello world" for the pratice on java programming. It would be useful for those who are learning java programming and it will help to write any written test and to crack interview .

Local Variables
Unary Operator
Static Variable
Static Initialization Blocks (SIB)
Non-Static Members
Constructor & 'THIS' Keyword
Constructor With IIB & SIB
Overloading & Overriding
Abstraction
Inheritance
Interface
Final Keyword
TypeCasting
Enum


Local Variables
1).Local variable in java is variable which are declared within the body of method or block
2).Local variables have the most limited scope. Such a variable is accessible only from the function or block in which it is declared.
3).Local variable should always initialize,they dont have any default value.

Observe all the programs,analyze them and predict the output.


Program # 1
class A
{
 public static void main(String[] args) 
 {
  int i;
  System.out.println("Hello World");
  System.out.println(i);
 }
}
OUTPUT :
Test.java:7: variable i might not have been initialized
System.out.println(i);
^ 1 error -->In above question the local variable not initialized ,and trying to print it hence giving the compile time error.


Program # 2
class A
{
 public static void main(String[] args) 
 {
  int i;
  System.out.println("Hello World");
 }
}
OUTPUT :
Helllow World
-->In above question the local variable not initialized ,and not printing or using anywhere in program hence no error.


Program # 3
class C 
{
 public static void main(String[] args) 
 {
  int i;
  int j;
  j=i;  
  System.out.println("Hello World!");
 }
}
OUTPUT :
C.java:7: variable i might not have been initialized
j=i;
^
1 error

Program # 4
class D 
{
 public static void main(String[] args) 
 {
  int i,j,k;  //declaring
  i=j=k=10;   //initilizing all
  System.out.println(i);
  System.out.println(j);
  System.out.println(k);
 }
}
OUTPUT :
10
10
10

Program # 5
class D 
{
 public static void main(String[] args) 
 {
  int i=j=10; 
  System.out.println(i);
  System.out.println(j);
 }
}
OUTPUT :
-->Compilation Error
D.java:5: cannot find symbol
symbol : variable j
location: class D
int i=j=10;
^
D.java:7: cannot find symbol
symbol : variable j
location: class D
System.out.println(j);
^
2 errors

Program # 6
class E 
{
 public static void main(String[] args) 
 {
  int i;
  i++;
  System.out.println("Hello World!");
 }
}
OUTPUT :
-->COMPILATION ERROR
E.java:6: variable i might not have been initialized
i++;
^
1 error

Program # 7
class F 
{
 public static void main(String[] args) 
 {
  int i;
         i=10;
  System.out.println(i);
  i=20;
  System.out.println(i);
  i=30;
  System.out.println(i);
  j=30;
  System.out.println(j);
 }
}
OUTPUT :
-->Local variable j is not declared hence compilation error
F.java:12: cannot find symbol
symbol : variable j
location: class F
j=30;
^
Test.java:13: cannot find symbol
symbol : variable j
location: class F
System.out.println(j);
^
2 errors

Program # 8
class F 
{
 public static void main(String[] args) 
 {
  int i;
  System.out.println(i=10);
  System.out.println(i);
 
 }
}
OUTPUT :
10
10


Program # 9
class F 
{
 public static void main(String[] args) 
 {
  int i;
  int j;
  j=i=0;
  System.out.println(i);
  System.out.println(j);
 
 }
}
OUTPUT :
0
0


Program # 10
class F 
{
 public static void main(String[] args) 
 {
  int i,j;
  i=10;
  j=20;
  System.out.println(i);
  System.out.println(i+i);
  System.out.println(i+j);
 
 }
}
OUTPUT :
10
20
30

Program # 11
class F 
{
 public static void main(String[] args) 
 {
  int i=10,j,k=20;
  i=20;
  j=i+k;
  System.out.println(i);
  System.out.println(j);
  System.out.println(k);
  System.out.println(i*k);
 
 }
}
OUTPUT :
20
40
20
400
Unary Operator

Program # 1
class A 
{
 public static void main(String[] args) 
 {
  int i=0;
  System.out.println(i++);
  System.out.println(i);
 }
}

OUTPUT :
0
1


Program # 2
class A
{
 public static void main(String[] args) 
 {
  int i=0;
  System.out.println(++i);
  System.out.println(i);
 }
}

OUTPUT :
1
1


Program # 3
class A 
{
 public static void main(String[] args) 
 {
  int i=1;
  System.out.println(i++);
  System.out.println(i);
  i=1;
  System.out.println(i--);
  System.out.println(i);
  i=1;
  System.out.println(++i);
  System.out.println(i);
  i=1;
  System.out.println(--i);
  System.out.println(i);
 }
}

OUTPUT :
1
2
1
0
2
2
0
0


Program # 4
class A 
{
 public static void main(String[] args) 
 {
  int i=1;
  System.out.println(i++);
  System.out.println(i);
  int i=0;
  System.out.println(i--);
  System.out.println(i);
 }
}

OUTPUT :
A.java:8: i is already defined in main(java.lang.String[])
int i=0;
^
1 error

Program # 5
class G 
{
 public static void main(String[] args) 
 {
  int i=0;
  int j=i++;
      //usage value    0
      //final value    1
  System.out.println(i);
  System.out.println(j);
 }
}

OUTPUT :
1
0

Program # 6
class A
{
 public static void main(String[] args)
 {
  int i=0;
  System.out.println(i++ + i + --i + ++i + i);
  System.out.println(i);
  //usage            0     1    0     1    1 =sum of expression =3
  //final            1     1    0     1    1 =final value of  i =1
 }
}

OUTPUT :
3
1


Program # 7
class G 
{
 public static void main(String[] args) 
 {
  int i=0;
  int j=++i;
  System.out.println(i);
  System.out.println(j);
 }
}

OUTPUT :
1
1

Program # 8
class G 
{
 public static void main(String[] args) 
 {
  int i=0;
  int j=i++ + i;
  //usage value 0  + 1
  //final value 1    1
  System.out.println(i);
  System.out.println(j);
 }
}

OUTPUT :
1
1

Program # 9
class G 
{
 public static void main(String[] args) 
 {
  int i=0;
  int j=i++ + ++i + i++ + ++i ;
        //Usage value  0     2     2    4
 //final value  1     2     3    4
  System.out.println(i);
  System.out.println(j);
 }
}

OUTPUT :
4
8

Program # 10
class G 
{
 public static void main(String[] args) 
 {
  int i=0;
                int j=i++ + i++ + i++ + i ;
  //usage 0 + 1     2     3
  //final 1   2     3     3
  System.out.println(i);
  System.out.println(j);
 }
}

OUTPUT :
3
6

Program # 11 -->MAKE UNCOMMENT REST AND CHECK OUTPUT
class G
{
 public static void main(String[] args) 
 {
  int i=0;
                i=i++;     // 0
                //i=i--;   // 0
                //i=++i;   // 1
                //i=--i;   //-1
  System.out.println(i);
 }
}

OUTPUT :
0

Program # 12
class H 
{
 public static void main(String[] args) 
 {
  int i=10;
  System.out.println(test(i));
  System.out.println(i);
 }
 static int test(int i)
 {
  return i++;       //will return 10,post increment will not reflect to method
      // return ++i; --> will return 11
 }
}

OUTPUT :
10
10

Program # 13
class H 
{
 public static void main(String[] args) 
 {
  int i=0;
  int j=i++ + test(i++) + i++ + i;
 //usage value  0      1/1        2    3
 //final value  1      2          3    3

  System.out.println(i);
  System.out.println(j);
 }
 static int test(int i)
 {
  return i++;  //post increment of i will not reflect in main method
 }
}

OUTPUT :
3
6

Program # 14
class H 
{
 public static void main(String[] args) 
 {
  int i=0;
         int j=i++ + test(i) + i++ + i;
            //usage    0      1/1      1    2
            //final    1      1        2    2
  System.out.println(i);
  System.out.println(j);
 }
 static int test(int i)
 {
  return i++;  //post increment of i will not reflect in main method
 }
}

OUTPUT :
2
4

Program # 15
class H 
{
 public static void main(String[] args) 
 {
  int i=0;
  int j=i++ + test1(i++) + test2(++i);
 //usage value  0       1/2           3
 //final value  1       2             3
  System.out.println(i);
  System.out.println(j);
 }
 static int test1(int i)
 {
  return ++i; 
 }
 static int test2(int i)
 {
  return i++;
 }  
}

OUTPUT :
3
5
Static Variable

Program # 1 Note : In below program i and j are global static variable ,if a global variable not initilize it will take its default values.
class A 
{
 static int i;//Global variable
 static int j=20;
 public static void main(String[] args) 
 {
  System.out.println(i);
  System.out.println(j);
 }
}

OUTPUT :
0
10

Program # 2 Note : in below program we trying to access a non-static variable i from a static method. which is not allowed ,we can access only static variable from static context ,hence compilation error.
class B 
{
        int i; 
 static void test()
 {
  System.out.println("From test"+i);
 }
 public static void main(String[] args) 
 {
  System.out.println("From main"+i);
 }
}

OUTPUT :
-->Compilation Error
B.java:6: non-static variable i cannot be referenced from a static context
System.out.println("From test"+i);
^
Test.java:10: non-static variable i cannot be referenced from a static context
System.out.println("From main"+i);
^
2 errors

Program # 3
class B 
{
 static int i;
 static void test()
 {
  System.out.println("From test"+i);
 }
 public static void main(String[] args) 
 {
  System.out.println("From main"+i);
 }
        static int j; //static Global variable can be declare from any where
}

OUTPUT :
From main0

Program # 4
class C
{
 static void test()
 {
  System.out.println("From test "+i);
 }
 static int i;
 public static void main(String[] args) 
 {
  System.out.println("From main "+i);
  i=10; //re-initilized a global variable,this variable is not local but global hence 10 will reflect in other method also
  test();
 }
}

OUTPUT :
From main 0
From test 10

Program # 5
class D 
{
 static int i,j=10;
 static int k;
 public static void main(String[] args) 
 {
  System.out.println(i);
  System.out.println(j);
  System.out.println(k);
  System.out.println(m);
  System.out.println(n);
  System.out.println(p);
 }
 static int m,n=20,p;
}

OUTPUT :
0
10
0
0
20
0

Program # 6 Note : a global variable can not be re-initialized globally.but it can be inside main method
class E 
{
 static int i;
        i=50; 
 public static void main(String[] args) 
 {
  System.out.println(i);
 }
}

OUTPUT :
--Compilation Error
Test.java:4: expected
i=50;
^
1 error


Program # 7 Not : But global variable can be re-initialize from locally in side any method
class E 
{
 static int i;
 static int j;
 public static void main(String[] args) 
 {
  i=10; //re-initilize global variable.
  System.out.println("From main :"+i);
  test();
  System.out.println("From test :"+j);
 }
 static void test()
 {
  j=50;
 }
}

OUTPUT :
From main :10
From test :50

Program # 8 Note : static variable can be access by className.variableName.
class F 
{
 static int i;
 public static void main(String[] args) 
 {
  System.out.println(i);
  System.out.println(F.i);
  i=10;
  System.out.println(i);
  System.out.println(F.i);
  F.i=20;
  System.out.println(i);
  System.out.println(F.i);
 }
}

OUTPUT :
0
0
10
10
20
20

Program # 9
class G 
{
 static int i;
 public static void main(String[] args) 
 {
  int i=10;
  System.out.println(i);  //This i is local.
  System.out.println(G.i);
  i=20;  //local variable re-initilize
  G.i=30;//global variable re-initilize
  System.out.println(i);
  System.out.println(G.i);
 }
}

OUTPUT :
10
0
20
30

Program # 10
class H 
{
 static int i=10;
 static double j=20.74;
}

class H
{
}

OUTPUT :
Runtime error:
Both class are valid compile successful,but runtime error.BECAUSE OF NO MAIN METHOD

Program # 11 Note : i must be declare (in case of global variable initialization not required)before assigning to other variable(TOP TO BOTTOM)
class A 
{
 static int i;
 static int j=i;
 public static void main(String[] args) 
 {
  System.out.println(i);
  System.out.println(j);
 }
}

OUTPUT :
0
0

Program # 12 Note : In the below program there are total 3 member(i,j,one main()),2 attribute(i,j),2 initilizer(i,j)
class B 
{
 static int i=10;
 static int j=i+20;
 public static void main(String[] args) 
 {
  System.out.println(i);
  System.out.println(j);
 }
}

OUTPUT :
10
30

Program # 13
class D 
{
 static int i=j;
 static int j=10;
 public static void main(String[] args) 
 {
  System.out.println(i);
  System.out.println(j);
 }
}
Note : j(global var) must be declare before the i;
OUTPUT :
Compilation Error:
D.java:3: illegal forward reference
static int i=j;

Program # 14
class E 
{
 static int i=10;
 static int j=i;
 static int k=m;
 static int m=j;
 public static void main(String[] args) 
 {
  System.out.println(i);
  System.out.println(j);
  System.out.println(k);
  System.out.println(m);
 }
}

OUTPUT :
compilation error as above program

Program # 15
class F 
{
 static void test()
 {
  System.out.println(i);
 }
        //static int i=10;
 public static void main(String[] args) 
 {
  System.out.println(i);
  test();
 }
        static int i=10;
}

OUTPUT :
Note :i=10 will reflect every where because it is global variable ,no matter where it has been declared
10
10

Program # 16
class G
{
 static int i=test();
 static int test()
 {
  return 10;
 }
 public static void main(String[] args) 
 {
  System.out.println(i);
 }
}

OUTPUT :
10

Program # 17
class H 
{
 static int i=test();
 static int j=10;
 static int test()
 {
  return j;
 }
 public static void main(String[] args) 
 {
  System.out.println(i);
  System.out.println(j);
 }
}
Note : top to bottom ,storing default value then initilizing
OUTPUT :
0
10

Program # 18
class A 
{
 static int x=10;
 static int y=test();
 static int z=20;
 static int test()
 {
  return z;
  //return x; while loading test method will return z=0 and x=10;
 }
 public static void main(String[] args) 
 {
  System.out.println(x);
  System.out.println(y);
  System.out.println(z);
 }
}

OUTPUT :
10
0
20

Program # 19
class B 
{
 static int x=test();
 static int test()
 {
         System.out.println("A :"+x);
     return 10;
 }
 public static void main(String[] args) 
 {
  System.out.println("B :"+x);
 }
}

OUTPUT :
A :0
B :10

Program # 20
class B 
{
 static int i;
 static void test()
 {
        System.out.println("From test");
 }
}

OUTPUT :
Compile success.
Exception in thread "main" java.lang.NoSuchMethodError: main
once loading complete compiler will search main method..hence exception

Program # 21
class B 
{
 static int x=test();
 static int test()
 {
         System.out.println("From test");
     return 10;
 }
}
Note : while loading class all static member executes, after loading compiler will search for main method ,hence giving run time exception
OUTPUT :
From test
Exception in thread "main" java.lang.NoSuchMethodError: main

Static Initialization Blocks (SIB)

A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.
Here is an example:
static {
    // code which is needed for initialization goes here
}


Program # 1
class B 
{
 static 
 {
  System.out.println("SIB");
 }
}

OUTPUT:
SIB
Exception in thread "main" java.lang.NoSuchMethodError: main

Program # 2
class C 
{
 /*static 
 {
  System.out.println("SIB1");
 }*/
 public static void main(String[] args) 
 {
  System.out.println("Hello World!");
 }
 static 
 {
  System.out.println("SIB2");
 }
}

OUTPUT:
SIB2
Hello World!


Program # 3
class C 
{
 static 
 {
  main(null);//calling main method with null arg
 }
 public static void main(String[] args) 
 {
  System.out.println("Hello World");
 }

}

OUTPUT:
Hello World
Hello World

Program # 4
class C 
{
 static 
 {
         System.out.println(1);
  main(null);
  System.out.println(2);
 }
 public static void main(String[] args) 
 {
  System.out.println("From main");
 }

}

OUTPUT:
1
From main
2
From main

Program # 5
class C 
{
 static int i=test();
 static int test()
 {
  main(null);
  return 20;
 }
 /*static 
 {
        System.out.println(1);
  main(null);
  System.out.println(2);
 }*/
 public static void main(String[] args) 
 {
  System.out.println("From main :"+i);
 }

}

OUTPUT:
From main :0
From main :20
-------
from main:0
1
from main:20
2
from main:20

Program # 6
class C 
{
 static int i=test1();
 static int test1()
 {
  System.out.println(1);
  test2();
  System.out.println(2);
  return 10;
 }
 static void test2()
 {
  System.out.println(3);
  main(null);
  System.out.println(4);
 }
 static 
 {
        System.out.println(5+":"+i);
  i=20;
  main(null);
  System.out.println(6+":"+i);
 }
 public static void main(String[] args) 
 {
  System.out.println(7+":"+i);
  i=30;//it is global hence re-initilization will reflect other blocks also
 }

}
->two initilizer is there test1(),static block ,so first execute these two initilizer
OUTPUT:
1
3
7:0
4
2
5:10
7:20
6:30
7:30

Program # 7
class C 
{
 static int i=20;
 static
 {
         i=10;
 }
 public static void main(String[] args) 
 {
  System.out.println(i);
 }

}

OUTPUT:
10


Program # 8
class C 
{
 static
 {
        int i=10;
 System.out.println(i);
 i++;
 }
 public static void main(String[] args) 
 {
  
 }
}

OUTPUT:
10

Program # 9
class C 
{
 static int i=20;//declaring before the SIB
 static
 {
        i=10;
    System.out.println(i);
    i++;
 }

 public static void main(String[] args) 
 {
  System.out.println(i);
 }

}

OUTPUT:
10
11

Program # 10
class C 
{
 static
 {
         int i=10;//Cannot use outside the SIB
 }
 public static void main(String[] args) 
 {
  System.out.println(i);
 }
}

OUTPUT:
C.java:9: cannot find symbol
symbol : variable i
location: class C
System.out.println(i);
^
1 error

Program # 11 Note : we we can develope any no of class in a single java file.if all the class are non-public then can save with any name .java,if there is any public class then we have save with that name .java ,one file can have only one public class
class A 
{
 static int i;
 static void test()
 {
         System.out.println("From test:"+i);
         System.out.println("From test:"+A.i);
 }
}
class B
{
 public static void main(String[] args) 
 {
  System.out.println("From main :"+A.i);
 }

}

OUTPUT:
From main :0

Program # 12
class C 
{
 static int i;
 static void test()
 {
         System.out.println("From test:"+i);
         //System.out.println("From test:"+A.i);
 }
}
class D
{
 static int i=10;
 public static void main(String[] args) 
 {
  int i=20;
  System.out.println("From main :"+i);
  System.out.println("From main :"+D.i);
  System.out.println("From main :"+C.i);
  C.test();
 }

}

OUTPUT:
From main :20
From main :10
From main :0
From test:0

Program # 13
class E 
{
 static 
 {
         System.out.println("From E.SIB");
 }
 public static void main(String[] args)
 {
         System.out.println("From E.main");
 }

}
class F
{
 static 
 {
        System.out.println("From F.SIB");
 }
 public static void main(String[] args) 
 {
  System.out.println("F-main begin");
  E.main(args);//E class will load in memory
  System.out.println("F-main end");
 }

}

OUTPUT:
java E
--
From E.SIB
From E.main
--
java F
--
From F.SIB
F-main begin
From E.SIB
From E.main
F-main end

Program # 14
class I 
{
 static void test()
 {
         System.out.println("From Test");
 }
 static 
 {
         System.out.println("From I.SIB");
 }

}
class J
{
 public static void main(String[] args) 
 {
  System.out.println("---111---");
  I.test();
  System.out.println("---222---");
  I.test();
  System.out.println("---333---");
  I.test();
  System.out.println("---444---");
 }
 
 static 
 {
         System.out.println("From J.SIB");
 }

}

OUTPUT:
From J.SIB
---111---
From I.SIB
From Test
---222---
From Test
---333---
From Test
---444---

Program # 15
class K 
{
 static int i=10;
 static 
 {
         System.out.println("From K.SIB");
 }
 static void test()
 {
         System.out.println("From K-Test");
 }

}
class L
{
 
 static 
 {
         System.out.println("From L.SIB");
 }
 public static void main(String[] args) 
 {
  System.out.println("---aaa---");
  K.test();
  System.out.println("---bbb---");
  K.test();
  System.out.println("---ccc---");
  System.out.println(K.i);
  System.out.println("---ddd---");
 }
 
}

OUTPUT:
From L.SIB
---aaa---
From K.SIB
From K-Test
---bbb---
From K-Test
---ccc---
10
---ddd---

Program # 16
class C 
{
 static int i;
 static int j=10;
 static 
 {
         System.out.println("From C.SIB");
 }
 static void test()
 {
         System.out.println("From Test");
 }
 public static void main(String[] args) 
 {
  System.out.println("MAIN BEGIN");
  test();
  System.out.println("MAIN END");
 }
 
}

OUTPUT:
From C.SIB
MAIN BEGIN
From Test
MAIN END

Program # 17
class C 
{
 static int i;
 static 
 {
    i=10;
       System.out.println("From C.SIB");
 }
 static void test()
 {
       System.out.println("From C-Test");
 }
}
class D
{
 static 
 {
       System.out.println("D-SIB");
 }
 public static void main(String[] args) 
 {
  System.out.println(1);
  C.test();
  System.out.println(2);
  System.out.println(C.i);
 }
 
}

OUTPUT:
D-SIB
1
From C.SIB
From C-Test
2
10

Non-Static Members


Program # 1 Note : non-static member will not load in memory while loading class,whenever object get created then every non-static member loads in memory.
class A
{
 int i;
 public static void main(String[] args) 
 {
  A a1=new A();
  System.out.println(a1.i);
 }
 
}

OUTPUT:
0

Program # 2
class B
{
 void test()
 {
  System.out.println("From test");
 }
 public static void main(String[] args) 
 {
  B b1=new B();
  b1.test();
  System.out.println("Hello world");
 }
 
}

OUTPUT:
From test
Hello world

Program # 3
class C
{
 int i;
 static void test()
 {
  C c1=new C();
  System.out.println(c1.i);
 }
}

OUTPUT:
//compilation successfull
runtime exception due to main method missing

Program # 4
class D
{
 void test1()
 {
         System.out.println("hello");
 }
 static void test2()
 {
  D d1=new D();
  d1.test1();
 }
}

OUTPUT:
//test2() only will load in the memory,while executation only main method runs by default other method only load but not execute while loading ,
//while loading calling test1() will not invoke from test2,because test2 is not executing.
Exception

Program # 5
class E
{
 int i;
 static //static block loads in memory and also get execute before main method,top to bottom
 {
  E e1=new E();
  System.out.println(e1.i);
 }
}

OUTPUT:
//0
//main method exception

Program # 6
class F
{
 int i;
 void test1()
 {
 }
 public static void main(String[] args)
 {
  F f1=new F();
  f1.i=10;
  f1.test1();
  
 }
}

OUTPUT:


Program # 7
class G
{
 void test1()
 {
            System.out.println("TEST1");
 }
 static 
 {
  G obj=new G();
  obj.test1();
 }
}

OUTPUT:
TEST1
Exception in thread "main" java.lang.NoSuchMethodError: main

Program # 8
class H
{
 int i;
 public static void main(String[] args)
 {
  H obj=new H();
  System.out.println(obj.i);
  obj.i=10;
  System.out.println(obj.i);
 }
}

OUTPUT:
0
10

Program # 9
class I
{
 int x;
 int y=10;
 public static void main(String[] args)
 {
  I obj=new I();
  System.out.println(obj.x);
  System.out.println(obj.y);
  obj.x=20;
  obj.y=40;
  System.out.println(obj.x);
  System.out.println(obj.y);
 }
}

OUTPUT:
0
10
20
40

Program # 10
class K
{
 int i;
 public static void main(String[] args)
 {
  K k1=new K();
  k1.i=10;
  K k2=new K();
  k2.i=20;
  System.out.println(k1.i);
  System.out.println(k2.i);
 }
}

OUTPUT:
10
20

Program # 11
class A
{
 int i;
 public static void main(String[] args)
 {
  A a1=new A();
  System.out.println(a1.i);
  A a2=new A();
  System.out.println(a2.i);
  A a3=new A();
  System.out.println(a3.i);
  a1.i=10;
  a2.i=20;
  a3.i=30;
  System.out.println(a1.i);
  System.out.println(a2.i);
  System.out.println(a3.i);
 }
}

OUTPUT:
0
0
0
10
20
30

Program # 12
class B
{
 int i;
 double d;
 boolean b;
 public static void main(String[] args)
 {
  B b1=new B();
  System.out.println(b1.i);
  System.out.println(b1.d);
  System.out.println(b1.b);
 }
}

OUTPUT:
0
0.0
false

Constructor & 'THIS' Keyword

1.same name as a class name.
2.it should not have a return type.
3.it cant be static,final or abstract.
4.executes automatically ,when object created.

Program # 1
class C
{
 int i;
 int j=10;
 C()
 {
  i=20;
 }
 public static void main(String[] args)
 {
  C c1=new C();
  System.out.println(c1.i);
  System.out.println(c1.j);
 }
}

OUTPUT:
20
10

Program # 2
//4 members are there ,constructors also a member
class D
{
 int i,j;
 D()
 {
  i=20;
  j=40;
 }
 public static void main(String[] args)
 {
  D d1=new D();
  System.out.println(d1.i);
  System.out.println(d1.j);
 }
}

OUTPUT:
20
40

Program # 3
class E
{
 E()
 {
  System.out.println("E()");
 }
 public static void main(String[] args)
 {
  E e1=new E();
  System.out.println("----");
  E e2=new E();
 }
}

OUTPUT:
two times constructor will execute,it excutes whenever object created.
E()
----
E()

Program # 4
//DEFAULT CONSTRUCTOR only will invoke when we create non-parameterized constructor
class G
{
 G()
 {
         System.out.println("G()");
 }
 G(int i)
 {
  System.out.println("G(int)");
 }

 public static void main(String[] args)
 {
  G g1=new G();
  System.out.println("----");
  G e2=new G(20);
  System.out.println("----");
  G g3=new G();
  System.out.println("----");
 }
}

OUTPUT:
G()
----
G(int)
----
G()
----

Program # 5
class H
{
 H(int i)
 {
         System.out.println("H(int)");
 }
 public static void main(String[] args)
 {
  H h1=new H(10);
  System.out.println("----");
  H h2=new H(20);
  System.out.println("----");
  //H h3=new H();
  System.out.println("----");
 }
}

OUTPUT:
H(int)
----
H(int)
----
----

Program # 6
class J
{
 J(int x)
 {
         System.out.println("X(int x)");
 }
 J(byte y)
 {
         System.out.println("X(int y)");
 }
 public static void main(String[] args)
 {
  J j1=new J(90);
  System.out.println("Done");
 }
}

X(int x)

OUTPUT:
Done

Program # 7
class K
{
 K(int i,int j)
 {
         System.out.println("int,int");
 }
 K(double i,int j)
 {
         System.out.println("double,int");
 }
 public static void main(String[] args)
 {
  K k1=new K(10,20);
  System.out.println("Done");
  K k2=new K(90.9,30);
  System.out.println("Done");
 }
}

OUTPUT:
int,int
Done
double,int
Done

Program # 8
class L
{
 L()
 {
         System.out.println("L()");
 }
 L(int i)
 {
         System.out.println("L(int)");
 }
 L(int i,int j)
 {
         System.out.println("L(int,int)");
 }
 public static void main(String[] args)
 {
  L l1=new L();
  System.out.println("----");
  L l2=new L(10);
  System.out.println("----");
  L l3=new L(10,20);
  System.out.println("----");
 }
}

OUTPUT:
L()
----
L(int)
----
L(int,int)
----
'THIS' Keyword


Program # 9
class M
{
 M()
 {
         System.out.println("M()");
 }
 M(int i)
 {
  this();
         System.out.println("M(int)");
 }
 public static void main(String[] args)
 {
  M m1=new M();
  System.out.println("----");
  M m2=new M(10);
  System.out.println("----");
 }
}

OUTPUT:
we can call another constructor from one constructor by using 'this()',this() remains in the same class.(***)
M()
----
M()
M(int)
----

Program # 10
class N
{
 N()
 {
         this(10);
     System.out.println("N()");
 }
 N(int i)
 {
         System.out.println("N(int)");
 }
 public static void main(String[] args)
 {
  N n1=new N();
  System.out.println("----");
  N n2=new N(10);
  System.out.println("----");
 }
}

OUTPUT:
N(int)
N()
----
N(int)
----

Program # 11
class O
{
 O()
 {
           this(2,5);
    System.out.println("O()");
 }
 O(int i)
 {
  this();
         System.out.println("O(int)");
 }
 O(int i,int j)
 {
  System.out.println("O(int,int)");
 }
 public static void main(String[] args)
 {
  O o1=new O();
  System.out.println("----");
  O o2=new O(10);
  System.out.println("----");
  O o3=new O(10,30);
  System.out.println("----");
 }
}

OUTPUT:
O(int,int)
O()
----
O(int,int)
O()
O(int)
----
O(int,int)
----

Program # 12
class P
{
 P()
 {
    System.out.println("P()");
 }
 P(int i)
 {
  this();
         System.out.println("P(int)");
 }
 P(char c1)
 {
  this();
         System.out.println("P(char)");
 }
 P(boolean b1)
 {
  this('a');
  System.out.println("P(boolean)");
 }
 P(double d)
 {
  this(10);
  System.out.println("P(double)");
 }
 public static void main(String[] args)
 {
  P o1=new P();
  System.out.println("----");
  P o2=new P('a');
  System.out.println("----");
  P o3=new P(10);
  System.out.println("----");
  P o4=new P(10.9);
  System.out.println("----");
  P o5=new P(false);
  System.out.println("----");
 }
}

OUTPUT:
P()
----
P()
P(char)
----
P()
P(int)
----
P()
P(int)
P(double)
----
P()
P(char)
P(boolean)
----

Program # 13
class Q 
{
 Q(int i)
 {
  this();
  System.out.println("Q()");
 }
 Q()
 {
  this(20);
  System.out.println("Q(int)");
 }

 public static void main(String[] args) 
 {
  System.out.println("Hello World!");
 }
}

OUTPUT:
Compiler error : recursive constructor invocation ,eventhough we r not creating object

Program # 14
class R 
{
 R()
 {
  this(10);
  System.out.println("R()");
 }
 R(int i)
 {
  System.out.println("R(int)");
 }

 /*public static void main(String[] args) 
 {
  System.out.println("Hello World!");
 }*/
}

OUTPUT:
Compile successfully,but cant run due to no main method

Constructor With IIB & SIB
Instance Initialization Block (IIB):
Instance Initializer block is used to initialize the instance data member. It run each time when object of the class is created.

Program # 1
class S 
{
 S()
 {
  System.out.println("S()");
 }
 {
  System.out.println("IIB");
 }

 public static void main(String[] args) 
 {
  S s1=new S();
  System.out.println("------");
  S s2=new S();
  System.out.println("------");
 }
}

OUTPUT:
/*
IIB
S()
------
IIB
S()
------
*/

Program # 2
class T 
{
 T()
 {
  System.out.println("T()");
 }
 {
  System.out.println("IIB");
 }
 T(int i)
 {
         System.out.println("T(int)");
 }
 public static void main(String[] args) 
 {
  T s1=new T();
  System.out.println("------");
  T s2=new T(20);
  System.out.println("------");
 }
}

OUTPUT:
/*
IIB
T()
------
IIB
T(int)
------
*/

Program # 3
class A 
{
 A()
 {
  System.out.println("A()");
 }
 {
  System.out.println("IIB1");
 }
 {
  System.out.println("IIB2");
 }
 /*A(int i)
 {
         System.out.println("A(int)");
 }*/
 public static void main(String[] args) 
 {
  A s1=new A();
  System.out.println("------");
  A s2=new A();
  System.out.println("------");
 }
}

OUTPUT:
IIB1
IIB2
A()
------
IIB1
IIB2
A() ------

Program # 4
class B 
{
 {
  System.out.println("IIB1");
 }
 B()
 {
  System.out.println("B()");
 }
 {
  System.out.println("IIB2");
 }
 B(int i)
 {
         System.out.println("B(int)");
 }
 public static void main(String[] args) 
 {
  B s1=new B();
  System.out.println("------");
  B s2=new B(90);
  System.out.println("------");
 }
}

OUTPUT:
/*
IIB1
IIB2
B()
------
IIB1
IIB2
B(int)
------
*/

Program # 5
class C 
{
 {
  System.out.println("IIB1");
 }
 public static void main(String[] args) 
 {
  C s1=new C();
  System.out.println("------");
  C s2=new C();
  System.out.println("------");
 }
}

OUTPUT:
/*
IIB1
------
IIB1
------
*/

Program # 6
class D 
{
 static 
 {
         System.out.println("SIB");
 }
 {
  System.out.println("IIB");
 }
 public static void main(String[] args) 
 {
  D s1=new D();
  System.out.println("------");
  D s2=new D();
  System.out.println("------");
 }
}

OUTPUT:
/*
SIB
IIB
------
IIB
------
*/

Program # 7
class E 
{
 static 
 {
         System.out.println("SIB1");
 }
 {
  System.out.println("IIB1");
 }
 static 
 {
          System.out.println("SIB2");
 }
 E()
 { 
  System.out.println("E()");
 }

 public static void main(String[] args) 
 {
  System.out.println("Main bein");
  E s1=new E();
  System.out.println("------");
  E s2=new E();
  System.out.println("manin end");
 }
}

OUTPUT:
/*
SIB1
SIB2
Main bein
IIB1
E()
------
IIB1
E()
manin end
*/

Program # 8
class G 
{
 {
  System.out.println("G-IIB1");
 }
 G(int i)
 { 
  System.out.println("G(int)");
 }
 {
  System.out.println("G-IIB2");
 }
 G()
 {
  this(10);
  System.out.println("G()");
 }
 public static void main(String[] args) 
 {
  G s1=new G();
  System.out.println("------");
  G s2=new G(20);
  System.out.println("------");
 }
}

OUTPUT:
/*
G-IIB1
G-IIB2
G(int)
G()
------
G-IIB1
G-IIB2
G(int)
------ */

Program # 9
class I
{
 {
  System.out.println("IIB1");
 }
 static
 {
  System.out.println("SIB1");
 }
 I()
 {
  System.out.println("I()");
 }
 {
  System.out.println("IIB2");
 }
 static
 {
  System.out.println("SIB2");
 }
 I(double d)
 {
  this();
         System.out.println("I(double)");
 }
 public static void main(String[] args) 
 {
  System.out.println("main-begin");
  I obj1=new I();
  System.out.println("----");
  I obj2=new I(90.5);
  System.out.println("main-end");
 }
}

OUTPUT:
/*
SIB1
SIB2
main-begin
IIB1
IIB2
I()
----
IIB1
IIB2
I()
I(double)
main-end
*/

Program # 10
class J
{
 {
  System.out.println("J-IIB1");
 }
 J()
 {
  System.out.println("J()");
 }
 {
  System.out.println("J-IIB2");
 }
}
class K
{
 {
  System.out.println("K-IIB1");
 }
 K()
 {
  System.out.println("K()");
 }
 {
  System.out.println("K-IIB2");
 }
 K(int i)
 {
  System.out.println("k(int)");
 }
 static
 {
  System.out.println("K-SIB");
 }
 public static void main(String[] args) 
 {
  System.out.println("main-begin");
  K obj1=new K();
  System.out.println("----");
  J obj2=new J();
  System.out.println("----");
  K k2=new K(20);
  System.out.println("main-end");
 }
}

OUTPUT:
/*
K-SIB
main-begin
K-IIB1
K-IIB2
K()
----
J-IIB1
J-IIB2
J()
----
K-IIB1
K-IIB2
k(int)
main-end
*/

Program # 11
class L
{
 static
 {
         System.out.println("L-SIB");
 }
 L()
 {
  System.out.println("L()");
 }
 {
  System.out.println("L-IIB");
 }
}
class M
{
 static
 {
  System.out.println("M-SIB");
 }
 M()
 {
  System.out.println("M()");
 }
 {
  System.out.println("M-IIB");
 }
 M(int i)
 {
  L obj=new L();
  System.out.println("M(int)");
 }
 public static void main(String[] args) 
 {
  System.out.println("main-begin");
  M obj1=new M();
  System.out.println("----");
  M k2=new M(20);
  System.out.println("----");
  System.out.println("main-end");
 }
}

OUTPUT:
M-SIB
main-begin
M-IIB
M()
----
M-IIB
L-SIB
L-IIB L()
M(int)
----
main-end

Overloading & Overriding


Program # 1
class A
{
 void test1(int i)
 {
  System.out.println("Test1(int)");
 }
 int test1()
 {
  System.out.println("Test()");
  return 10;
 }
 public void test1(double d)
 {
  System.out.println("Test1(double)");
 }
 void test1(int i,int j)
 {
  System.out.println("test1(int,int)");
 }

}
class Manager
{
 public static void main(String[] args)
 {
  A a1=new A();
  a1.test1(10);
  a1.test1();
  a1.test1(20.9);
  a1.test1(10,11);}
}

OUTPUT:
/*
Test1(int)
Test()
Test1(double)
test1(int,int)
*/

Program # 2
class D 
{
 static void test() 
 {
  System.out.println("Hello World!");
 }
 public int test()
 {
  return 20;
 }
}

OUTPUT:
//compiler error : duplicate methods
//Parameter must be diffrent

Program # 3
abstract class E 
{
 abstract void test();
 void test() 
 {
  int i=10;
 }
}

OUTPUT:
//compiler error : duplicate methods

Program # 4
abstract class F
{
 abstract void test();
 abstract void test(int i);
 void test(int i,int j)
 {
  System.out.println("Test1(int,int)");
 }

}
class G extends F
{
 void test()
 {
  System.out.println("Test()");
 }
 void test(int i)
 {
  System.out.println("Test(int)");
 }
 public static void main(String[] args) 
 {
  G g1=new G();
  g1.test();
  g1.test(10);
  g1.test(1,20); }
}

OUTPUT:
/*
Test()
Test(int)
Test1(int,int)
*/

Program # 5
class H
{
 void test1()
 {
  System.out.println("Test1()");
 }
 void test2()
 {
  System.out.println("Test2()");
 }
}
class I extends H
{
 void test2()
 {
  System.out.println("Modified Test2()");
 }
 public static void main(String[] args) 
 {
  I obj=new I();
  obj.test1();
  obj.test2(); }
}

OUTPUT:
/*
Test1()
Modified Test2()
*/

Program # 6
abstract class J
{
 abstract void test(int i);
}  
class K extends J
{
 void test()
 {
  System.out.println("Test()");
 }
 /*void test(int i)
 {
  System.out.println("test(int)");
    }*/

}

OUTPUT:
/*
K.java:6: K is not abstract and does not override abstract method test(int) in J
class K extends J
^
1 error
*/

Program # 7
class L
{
 void test1()
 {
  System.out.println("test1()");
 }
}
class M extends L
{
 int test1()
 {
  System.out.println("test1()");
  return 10;
 }
}

OUTPUT:
/*
M.java:11: test1() in M cannot override test1() in L; attempting to use incompatible return type
found : int
required: void
int test1()
^
1 error
*/

Program # 8
abstract class N
{
 abstract int test1();
}  
class O extends N
{
 void test1()
 {
 }
}

OUTPUT:
//compiler error return type r not same,could not overrirded

Program # 9
abstract class U
{
 public abstract void test1();
}
class V extends U 
{
 void test1()           //error
 //public void test1() //compile success
 {
 }
}

OUTPUT:
/*
V.java:8: test1() in V cannot override test1() in U; attempting to assign weaker access privileges; was public
void test1()
^
1 error
*/

Abstraction

1.Abstract method : No defination just declaration,all declare method should have abstract keyword
2.If class cantaining any abstract method ,class should be declare as abstract class.
3.we cant create an object of an abstract class but can create just reference variable.(C c1=null;),
4.null is keyword which is used to initilize any reference variable
5.An abstract method can be use by inheritance by creation object.
4.Abstract class also generate .class file on compiling.

Program # 1
abstract class E
{
 abstract void test1();
 void test2()
 {
         System.out.println("Test2");
 }
}
class F extends E
{
 void test1()
 {
  System.out.println("Test1");
 }
 public static void main(String[] args) 
 {
  F f1=new F();
  f1.test1();
  f1.test2();
  System.out.println("Done");
 }
}

OUTPUT:
Test1
Test2
Done

Program # 2
abstract class G
{
 void test1()
 {
  System.out.println("Test1");
 }
 abstract void test2();
 void test3()
 {
         System.out.println("Test3");
 }
}
class H extends G
{
 void test2()
 {
  System.out.println("Test2");
 }
 void test4()
 {
  System.out.println("Test4");
 }
 public static void main(String[] args) 
 {

  H h1=new H();
  h1.test1();
  h1.test2();
  h1.test3();
  h1.test4();
  System.out.println("Done");
 }
}

OUTPUT:
Test1
Test2
Test3
Test4
Done

Program # 3
abstract class J
{
 abstract void test1();
 abstract int test2();
 abstract int test3(double d);
 void test4()
 {
  System.out.println("Test4");
 }
}
abstract class K extends J
{
 void test1()
 {
  System.out.println("Test1");
 }
}
class L extends K
{
 int test2()
 {
  System.out.println("Test2");
  return 10;
 }
 int test3(double d)
 {
  System.out.println("Test3");
  return 20;
 }
}
class M
{
 public static void main(String[] args) 
 {

  L h1=new L();
  h1.test1();
  h1.test2();
  h1.test3(2.3);
  h1.test4();
  System.out.println("Done");
 }
}

OUTPUT:
Test1
Test2
Test3
Test4
Done

Program # 4
/*
1.An abstract method can have all concrete method even though we can not create the object of an abstract class
*/
abstract class N
{
 void test1()
 {
        System.out.println("Test1");
 }
}  
class O extends N
{
}
class P
{
 public static void main(String[] args) 
 {
  //N n1=new N();
  //n1.test1();
  O o1=new O();
  o1.test1();
  System.out.println("Done");
 }
}

OUTPUT:
Test1
Done

Program # 5
//whenevr object create constructor executes,we can keep a constructor inside a abstract class.but constructor cannot be abstract.
//constructor can not inherit to base class ,so we can not implement the abstract class,hence constructor can not be abstract
abstract class Q
{
 Q()
 {
  System.out.println("Q()");
 }
 abstract void test1();
 abstract void test2();
 void test3()
 {
        System.out.println("Test3");
 }
}  
class R extends Q
{
 R()
 {
  System.out.println("R()");
 }
 void test1()
 {
  System.out.println("Test1");
 }
 void test2()
 {
  System.out.println("Test2");
 }
}
class S
{
 public static void main(String[] args) 
 {
  R r1=new R();
  System.out.println("------");
  r1.test1();
  r1.test2();
  r1.test3();
 }
}

OUTPUT:
Q()
R()
------
Test1
Test2
Test3

Inheritance


Program # 1
class A
{
 int i;
}
class B extends A
{
 int j;
 public static void main(String[] args) 
 {
  B obj1=new B();
  System.out.println(obj1.i);
  System.out.println(obj1.j);
 }
}

OUTPUT:
0
0

Program # 2
//inside class D there are 3 members
class C
{
 int i;
 void test()
 {
        System.out.println("Test");
 }
}
class D extends C
{
 int j;
}
class E
{
 public static void main(String[] args) 
 {
  D d1=new D();
  d1.i=10;
  d1.j=20;
  d1.test();
  System.out.println(d1.i);
  System.out.println(d1.j);
 }
}

OUTPUT:
Test
10
20

Program # 3
class F
{
 int i;
 static int j;
 void test1()
 {
        System.out.println("Test1");
 }
 static void test2()
 {
  System.out.println("Test2");
 }
}
class G extends F
{
 int k;
 static void test3()
 {
  System.out.println("Test3");
 }
 public static void main(String[] args) 
 {
  F.j=10;
  F.test2();
  G.test2();
  G.test3();
  System.out.println(G.j);
  F f1=new F();
  f1.i=20;
  f1.test1();
  G g1=new G();
  g1.i=30;
  g1.k=50;
  g1.test1();
    }
}

OUTPUT:
Test2
Test2
Test3
10
Test1
Test1

Program # 4
class H
{
 int x;
}
class I extends H
{
 int y;
}
class J extends I
{ 
 int z;
}
class Manager
{
 public static void main(String[] args) 
 {
  H h1=new H();
  I i1=new I();
  J j1=new J();
  System.out.println(h1.x);
  System.out.println(i1.x);
  System.out.println(j1.x);
  System.out.println(i1.y);
  System.out.println(j1.y);
  System.out.println(j1.z);
    }
}
//while creating the object of J all class H,I and J will load in memory

OUTPUT:
all 0

Program # 5
class K
{
 K()
 {
  //super();  //compiler keep super internally 
  System.out.println("K()");
 }
}
class L extends K
{
 L()
 {
  //super();  //compiler keep super internally 
  System.out.println("L()");
 }
}
class Manager
{
 public static void main(String[] args)
 {
  K k1=new K();
  System.out.println("------");
  L l1=new L();
  System.out.println("------");
 }
}
//constructor and initilization block (SIB,IIB) are not inherit to sub class
//every constructor should have the first statement either super() or this() calling statement
//if we are not keeping the super() or this() as a first statement in constructor ,compiler will keep super().
//even a parent class have super() statement to Object class.

OUTPUT:
K()
------
K()
L()
------

Program # 6
class M extends Object
{
 M()
 {
  super();
  System.out.println("M()");
 }
}
class N extends M
{
 N()
 {
  super();
  System.out.println("N()");
 }
}
class O extends N
{
 O()
 {
  super();
  System.out.println("O()");
 }
}
class Manager2
{
 public static void main(String[] args) 
 {
  M m1=new M();
  System.out.println("------");
  N l1=new N();
  System.out.println("------");
  O o1=new O();
  System.out.println("------");
 }
}
//total 4 constructor are executing in this program

OUTPUT:
M()
------
M()
N()
------
M()
N()
O()
------

Program # 7
class P 
{
 P()
 {
   System.out.println("P()");
 }
}
class Q extends P
{
 Q()
 {
   System.out.println("Q()");
 }
}
class R extends Q
{
 R()
 {
   System.out.println("R()");
 }
}
class S extends R
{
 S()
 {
   System.out.println("S()");
 }
}
class T extends S
{
 T()
 {
   System.out.println("T()");
 }
 public static void main(String[] args) 
 {
  P m1=new P();
  System.out.println("------");
  Q l1=new Q();
  System.out.println("------");
  R o1=new R();
  System.out.println("------");
  S s1=new S();
  System.out.println("------");
  T t1=new T();
  System.out.println("------");
 }
}

OUTPUT:
R()
S()
------
P()
Q()
R()
S()
T()
------

Program # 8
class A 
{
 A()
 {
   System.out.println("A()");
 }
 A(int i)
 {
   System.out.println("A(int)");
 }
}
class B extends A
{
 B()
 {
   System.out.println("B()");
 }
 B(int i)
 {
   System.out.println("B(int)");
 }
}
class Manager
{
 public static void main(String[] args) 
 {
  A m1=new A();
  System.out.println("------");
  A l1=new A(20);
  System.out.println("------");
  B o1=new B();
  System.out.println("------");
  B s1=new B(30);
  System.out.println("------");
 }
}

OUTPUT:
A()
------
A(int)
------
A()
B()
------
A()
B(int)
------

Program # 9
class E 
{
 E()
 {
   System.out.println("E()");
 }
}
class F extends E
{
 F(int i)
 {
   System.out.println("F(int)");
 }
}
class Manager2
{
 public static void main(String[] args) 
 {
  E m1=new E();
  System.out.println("------");
  F l1=new F(20);
 }
}

OUTPUT:
E()
------
E()
F(int)

Program # 10
class G 
{
 G(int i)
 {
  System.out.println("G(int)");
 }
}
class H extends G
{
 H()
 {
  //super(10); if remove this super(10) will give compiler error
  System.out.println("H()");
 }
}
class Manager3
{
 public static void main(String[] args) 
 {
  G g1=new G(90);
  System.out.println("------");
  H h1=new H();
 }
}

OUTPUT:
H.java:11: cannot find symbol
symbol : constructor G()
location: class G
{
^
1 error
------------------
G(int)
------
G(int)
H()


Program # 11
class I
{
 I(double j)
 {
  System.out.println("I(double)");
 }
}
class J extends I
{
 J()
 {
  super(9.9);
  System.out.println("J()");
 }
 J(double d){
  super(d);
  System.out.println("J(double)");
 }
}
class Manager4
{
 public static void main(String[] args) 
 {
  I i1=new I(9.9);
  System.out.println("----------");
  J j1=new J();
  System.out.println("----------");
  J j2=new J(3.4);
 }
}

OUTPUT:
I(double)
----------
I(double)
J()
----------
I(double)
J(double)

Program # 12
class K
{
 K()
 {
  System.out.println("K()");
 }
 {
  System.out.println("K-IIB");
 }
}
class L extends K
{
 L()
 {
  System.out.println("L()");
 }
 {
  System.out.println("L-IIB");
 }
} 
class Manager5
{
 public static void main(String[] args) 
 {
  K k1=new K();
  System.out.println("----------");
  L l1=new L();
  System.out.println("----------");
 }
}

OUTPUT:
K-IIB
K()
----------
K-IIB
K()
L-IIB
L()
----------

Program # 13
class M
{
 M()
 {
  System.out.println("M()");
 }
 {
  System.out.println("M-IIB-1");
 }
 {
  System.out.println("M-IIB-2");
 }
}
class N extends M
{
 N()
 {
  System.out.println("N()");
 }
 {
  System.out.println("N-IIB-1");
 }
 {
  System.out.println("N-IIB-2");
 }
} 
class Manager6
{
 public static void main(String[] args) 
 {
  M k1=new M();
  System.out.println("----------");
  N l1=new N();
  System.out.println("----------");
 }
}

OUTPUT:
M-IIB-1
M-IIB-2
M()
----------
M-IIB-1
M-IIB-2
M()
N-IIB-1
N-IIB-2
N()
----------

Program # 14
class O 
{
  {
   System.out.println("O-IIB");
 }
 O()
 {
   System.out.println("O()");
 }
}
class P extends O
{
 P()
 {
   System.out.println("P()");
 }
 {
   System.out.println("P-IIB");
 }
 P(int i)
 {
  this();//IT will call constructor of same class
   System.out.println("P(int)");
 }
}
class Manager7
{
 public static void main(String[] args) 
 {
  O m1=new O();
  System.out.println("------");
  P o1=new P();
  System.out.println("------");
  P s1=new P(30);
  System.out.println("------");
 }
}

OUTPUT:
O-IIB
O()
------
O-IIB
O()
P-IIB
P()
------
O-IIB
O()
P-IIB
P()
P(int)
------

Interface

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated they can only be implemented by classes or extended by other interfaces.

Program # 1
abstract interface E
{
 abstract void test1();
 void test2();
 public void test3();
}
Note:
1.Abstract keyword before an interface is optional.
2.inside an interface we can keep two type of member 1.constant (2)abstract method
3.we can not keep any concrete method ,SIB,IIB inside interface.
4.By default every member inside an interface are abstract and public ,public and abstract are optional.

Program # 2
class F implements E
{
 public void test1()
 {
  System.out.println("Test1");
 }
 public void test2()
 {
  System.out.println("Test2");
 }
 public void test3()
 {
  System.out.println("Test3");
 }
 public static void main(String[] args) 
 {
               // E e1=new E(); can not be instantiated.
                E e1=null; //allow
  F f1=new F();
  f1.test1();
  f1.test2();
  f1.test3();
  System.out.println("done");
 }
}

OUTPUT:
while implementing interface method use access specifier as 'public'
Test1
Test2
Test3
done

Program # 3
interface G
{
        void test1();
 int test2();
 void test3(int i);
}
class H implements G
{
 public void test1()
 {
  System.out.println("Test1");
 }
 public int test2()
 {
  System.out.println("Test2");
  return 10;
 }
 public void test3(int i)
 {
  System.out.println("Test3");
 }
}
class Manager
{
 public static void main(String[] args) 
 {
  H f1=new H();
  f1.test1();
  f1.test2();
  f1.test3(90);
  System.out.println("done");
 }
}

OUTPUT:
Test1
Test2
Test3
done

Program # 4 Note:one class can implement any number of interfaces,it has to implement every methods of interface.
interface I
{
    void test1();
}
interface J
{
    void test2();
}
class K implements I,J //in K class inherited method must implement otherwise compiler error
{
 public void test1()
 {
  System.out.println("Test1");
 }
 public void test2()
 {
  System.out.println("Test2");
 }
}
class Manager1
{
 public static void main(String[] args) 
 {
  K f1=new K();
  f1.test1();
  f1.test2();
  System.out.println("done");
 }
}

OUTPUT:
Test1
Test2
done

Program # 5 Note:The keywords Order must be proper first should be extends then implements
interface L
{
    void test1();
}
class M
{
 void test2()
 {
  System.out.println("Test2");
 }

}
class N extends M implements L
//class N implements L extends M
{
 public void test1()
 {
  System.out.println("Test1");
 }
}
class O
{
 public static void main(String[] args) 
 {
  M m1=new M();
  m1.test2();
  N f1=new N();
  f1.test1();
  f1.test2();
  System.out.println("done");
 }
}

OUTPUT:
Test2
Test1
Test2
done

Program # 6 Note: method inside interface P and Q both should be implement either in R or S class.
interface P
{
    void test1();
}
interface Q
{
    void test2();
}
class R
{
 public void test1()
 {
  System.out.println("Test1");
 }

}
class S extends R implements P,Q
{
 public void test2()
 {
  System.out.println("Test2");
 }
}
class Manager2
{
 public static void main(String[] args) 
 {
  S s1=new S();
  s1.test1();
  s1.test2();
  System.out.println("done");
 }
}

OUTPUT:
Test1
Test2
done

Program # 7 Note: we can use interface to interface extends.
interface T
{
    void test1();
}
interface U
{
    void test2();
}
interface V extends T,U
{
 void test3();
}
class W implements V
{
 public void test1()
 {
  System.out.println("Test1");
 }
 public void test2()
 {
  System.out.println("Test2");
 }
 
 public void test3()
 {
  System.out.println("Test3");
 }
}
class Manager3
{
 public static void main(String[] args) 
 {
  W s1=new W();
  s1.test1();
  s1.test2();
  s1.test3();
  System.out.println("done");
 }
}

OUTPUT:
Test1
Test2
Test3
done

Program # 8
abstract class X
{
 static void test1()
 {
  System.out.println("Test1");
 }
 static int i=20;
 void test2()
 {
  System.out.println("Test2");
 }
}
interface Y
{ 
 static int j=30;
}
class Z
{
 public static void main(String[] args) 
 {
  X.test1();
  System.out.println(X.i);
  System.out.println(Y.j);
 }
}

OUTPUT:
Test1
20
30 Note:
1.inside the abstract class or interface if static member is there then static member can be access by class name.
2.every attribute of interface are by default public ,static and final
3.static method can not be abstract because static method can not inheriting in sub class.
4.interface method should not be static ,because further they has to implement.
OUTPUT:
Test1 20 30

Program # 9
interface A
{
} 
compile successfull and generate .class file.

Final Keyword

Final is almost opposite to the polymorphism Final value can't be re-assigned. A final keyword can we used with 1)Variable 2)method 3)class

Program # 1
class A
{
 public static void main(String[] args) 
 {
  int i=0;
  final int j=1;
  System.out.println(i);
  System.out.println(j);
  i=10;
  //j=1;
  System.out.println(i);
  System.out.println(j);

 }
}

OUTPUT:
i value can be modify anytime but j cant cause of final
0
1
10
1

Program # 2
class B 
{
 public static void main(String[] args) 
 {
  final int i=10;
  i++;
  System.out.println(i);
 }
}

OUTPUT:
compilation error:
B.java:6: cannot assign a value to final variable i
i++;
^
1 error

Program # 3 Note :Here array x is final ,we are modifying x[0],x[1] ie index value ,not array x hence no error.
class C
{
 public static void main(String[] args) 
 {
  final int[]x=new int[2];
  x[0]=10;
  x[1]=20;
  System.out.println(x[0]);
  System.out.println(x[1]);
 }
}

OUTPUT:
/*
10
20
*/

Program # 4 Note : Here reassigning a final array hence error
class D
{
 public static void main(String[] args) 
 {
  final int[]x=new int[3];
  x=new int[3]; 
  System.out.println("done");
 }
}

OUTPUT:
/*
D.java:7: cannot assign a value to final variable x
x=new int[3];
*/

Program # 5 Note : we are modifying the i value not e1,hence valid
class E
{
 int i;
 public static void main(String[] args) 
 {
  final E e1=new E();
  e1.i=20;
  System.out.println("done");
 }
}

OUTPUT:
done

Program # 6 Note : reference variable f1 can not re-initialize because of final
class F
{
 int i;
 public static void main(String[] args) 
 {
  final F f1=new F();
  //f1=new F(); --here trying to re-initialize a final ref var
  //f1=null;    b--here trying to re-initialize a final ref var
  System.out.println("done");
 }
}

OUTPUT:
done

Program # 7 Note : a global final variable sud be initilize
class F 
{
 final int i=0;
 public static void main(String[] args)
 {
  F f1=new F();
  //f1.i=20;  --cannot re-initilize final variable
  //System.out.println(f1.i);
 }
}

OUTPUT:


Program # 8 Note : final argument in main method can not be re-assigned
class G
{
 public static void main(final String[] args) 
 {
  System.out.println("Hello world");
  args=null;
 }
}

OUTPUT:
compiler error

Program # 9
class H
{
 final int i=10;
}
class Manager
{
 public static void main(final String[] args) 
 {
  H obj=new H();
  System.out.println(obj.i);
  obj.i=10;//error :can't assign a final value.
  System.out.println(obj.i);
 }
}

OUTPUT:
error :can't assign a final value.

Program # 10 Note : Eventhough not calling test() ,final x can't be reassigned in test().
class I
{
 final int x=10;
 void test()
 {
  x=11;
 }
}
/*
I.java:7: cannot assign a value to final variable x
  x=10;
*/


Program # 11 1.Global final variable must be initilized at any cost. By the time of object creation
global final variable can't survive with its default value
2.Not only at the place of declaration ,it can be initilize in constructor or in the IIB.
class J
{
 final int i;
}
class Manager1
{
 public static void main(String[] args) 
 {
  J obj=new J();
  System.out.println(obj.i);
 }
}

OUTPUT:
/*
J.java:2: variable i might not have been initialized
class J
*/

Program # 12
class J
{
 final int i;
}

OUTPUT:
/*
J.java:2: variable i might not have been initialized
class J
*/

Program # 13 Note :a global final variable can be initialize inside constructor.
class L
{
 final int i;
 L()
 {
  i=10;
 }
}

OUTPUT:
/*
compile successfull
*/

Program # 14
class M
{
 final int i=10;
 M()
 {
  i=10;
 }
}

OUTPUT:
/*
J.java:7: cannot assign a value to final variable i
i=10;
*/

Program # 15 Note : if multiple constructor final value should be initilizing in each constructor.
class N
{
 final int i;
 N()
 {
  i=10;
 }
 N(int j)
 {
  i=10;
 }
 N(int k,double l)
 {
                i=10;
 }
}

OUTPUT:
successful compile

Program # 16 Note :Non-static final global works on object wise.create object N() nad N(int i) and analyse
class N
{
 final int i;
 N()
 {
  i=5;
 }
 N(int i)
 {
  this.i=i;
 }
 N(int i,double d)
 {
        this.i=i;
 }
}
public class Demo
{
 public static void main(String[] args)
 {
    N obj1=new N();
                  N obj2=new N(10);
                  N obj3=new N(15,800);
    System.out.println(obj1.i);
    System.out.println(obj2.i);
    System.out.println(obj3.i);
 }
}

OUTPUT:
/*
5
10
15
*/

Program # 17 Note :In every constructor final variable should be initialize ,this this() is used to initialize the final in the second constructor. if removing this() then it will give compiler error


class Q
{
 final int i;
 Q()
 {
  i=10;
 }
 Q(String s1)
 {
  this(); 
 }
}

OUTPUT:
//compile success

Program # 18 Note :IIB executing on object creation,hence through IIB we can initilize the final global variable

class R
{
 final int i;
 {
  i=0;
 }
}

OUTPUT:
/*
compile sucess
*/

Program # 19 Note :while crating obj first IIB will execute and i will get initilized,then constructor will execute and try to re-initilize the final value hence error

class S
{
 final int i;
 S()
 {
  i=0;
 }
 {
  i=0;
 }
}

OUTPUT:
/*
J.java:7: variable i might already have been assigned
i=0;
*/

Program # 20
Note : we can keep multiple IIB in a class.
class S
{
 final int i;
 {
  i=0;
 }
 {
  i=0;
 }
}

OUTPUT:
/*
J.java:7: variable i might already have been assigned
i=0;
*/

Program # 21
/*
Note :
Arguments of constructors and Method can be declare as a final,if argument declare as final then that should not be re-initilized.
Argument is also a local variable,if lacal variable is final then that should not be re-initilized.
while creating object we have to supply value to i,j.
j is final here so j shoult not re-initilized here
*/
class U
{
 U(int i,final int j)
 {
  i=20;
  j=20;
 }
}

OUTPUT:
/*
J.java:7: final parameter j may not be assigned
j=20;
*/

Program # 22 Note :
Static final global variable : means it acts as a perfect constant,static means only single copy on one execution Hence it can't be re-initilized

class V
{
 static final int i=10;
 public static void main(final String[] args) 
 {
  System.out.println(i);
  i=20;
  System.out.println(i);
 }
}

OUTPUT:
/*
J.java:7: final parameter j may not be assigned
j=20;
*/

Program # 23
//global final variable can't survive with its default value,no matter it static or non-static.
class W
{
 final static int i;
}

OUTPUT:
/*
J.java:2: variable i might not have been initialized
*/

Program # 24
Note :gloabal final variable can be initilize in static block.
class W
{
     static final int i;
 static
 {
  i=0;
 }
}

OUTPUT:
/*
compile success
*/

Program # 25
class W
{
     static final int i=0;
 static
 {
  i=0;
 }
}

OUTPUT:
/*
J.java:7: cannot assign a value to final variable i
i=0;
*/

Program # 26
class Z
{
     static final int i;
 static
 {
  i=0;
 }
 static
 {
  i=0;
 }
}

OUTPUT:
/*
J.java:11: variable i might already have been assigned
i=0;
*/

Program # 27 Note :final keyword can be use with method also, so a final method can not be override in the subclass.
class C
{
 void test1()
 {
 }
 final void test2()
 {
 }
}  
class D extends C
{
 void test1()
 {
 }
 void test2()
 {
 }
}

OUTPUT:
K.java:16: test2() in D cannot override test2() in C; overridden method is final
void test2(

Program # 28
//Note : class can be also declare final.final class can not have a sub class

final class F
{
 //some member
}
class G extends F
{
}

OUTPUT:


Program # 29
Note:
Abstract method means that method should be get implement in sub-class,so we can not use a final keyword with an abstract method.

abstract class E
{
 final abstract void test();
}

OUTPUT:
/*
J.java:4: illegal combination of modifiers: abstract and final
final abstract void test();
*/

Program # 30
abstract final class E
{
 //
}

OUTPUT:
/*
J.java:2: illegal combination of modifiers: abstract and final
abstract final class E
*/

Program # 31 Note : Compile success but can't create an object of abstract class
abstract class I
{
 public static void main(String[] args) 
 {
  System.out.println("Hello World!");
 }
}

OUTPUT:


Program # 32
/*
static method is not inheriting in sub-class ,hence the combination of :
[static & abstract] and [final & abstract] are not possible for method
*/
abstract class J
{
 static abstract void test1();
}

OUTPUT:
/*
K.java:4: illegal combination of modifiers: abstract and static
*/

Program # 33
/*
Every interface attributes are public static and final so in interface all attributes must be initilized at place of declaration.
we can't put SIB in interface
*/
interface A
{
  int i;
}

OUTPUT:
/*
A.java:3: = expected
int i;
^
1 error*/

Program # 34
interface B
{
 String s1=null;
 int j=0;
}

OUTPUT:
/*
compile success
*/

Program # 35
//a1.j--> compiler will modify reference variable to class name.
class A
{
 int i;
 static int j;
 public static void main(final String[] args) 
 {
  System.out.println(j);
  System.out.println(A.j);
  A a1=new A();
  System.out.println(a1.i);
  System.out.println(a1.j);
 }
}

OUTPUT:
/*
0
0
0
0
*/

Program # 36
/*
1.In every non-static definition block there is a default reference variable that is 'this'.
2.'this' is available in all non-static definition block,constructor,IIB and non-static methods
3.In main method this is not available ,so here we have to use reference variable to access.
4.Non-static member should be access with refernce variable.compiler will incorporate this inside the non-static block to access non-static member
*/
class B
{
 int i;
 void test1()
 {
  System.out.println("test1:"+i);
  System.out.println("test1 by using this keyword:"+this.i);
  i=10;
  test2();
 }
 void test2()
 {
  System.out.println("test2:"+i);
  System.out.println("test1 by using this keyword:"+this.i);
  i=20;
  test3();
 }
 void test3()
 {
  System.out.println("test3:"+i);
  System.out.println("test1 by using this keyword:"+this.i);
  i=30;
 }
 public static void main(final String[] args) 
 {
  B b1=new B();
  System.out.println("main1:"+b1.i);
  b1.i=5;
  b1.test1();
  System.out.println("main2:"+b1.i);
 }
}

OUTPUT:
/* main1:0
test1:5
test1 by using this keyword:5
test2:10
test1 by using this keyword:10
test3:20
test1 by using this keyword:20
main2:30
*/

Program # 37
class G
{
 int i;
 G obj;
 public static void main(final String[] args) 
 {
  G g1=new G();
  System.out.println(g1.i);
  System.out.println(g1.obj);
  g1.obj=new G();
  g1.obj.i=10;
  g1.i=20;
  System.out.println(g1.i);
  System.out.println(g1.obj.i);
 }
}

OUTPUT:
/*
0
null
20
10
*/

Program # 38
class C
{
 int i;
}
class D
{
 C c1;
 public static void main(final String[] args) 
 {
  D d1=new D();
  System.out.println(d1.c1);
 }
}
class E
{
 C c1=new C();
 public static void main(final String[] args) 
 {
  E e1=new E();
  System.out.println(e1.c1);
  System.out.println(e1.c1.i);
 }
}
class F
{
 C c1=null;
 public static void main(final String[] args) 
 {
  F f1=new F();
  f1.c1=new C();
  System.out.println(f1.c1);
  System.out.println(f1.c1.i);
 }

}

OUTPUT:
/* D:
null
--------------
E:
C@3e25a5
0
--------------
F:
C@3e25a5
0
*/

Program # 39
class P
{
 int i=1;
}
class Q extends P
{
 int i=2;
}
class R extends Q
{
 int i=3;
}
class Manager1
{
 public static void main(final String[] args) 
 {
  P p1=new P();
  System.out.println(p1.i);

  P p2=new Q();
  System.out.println(p2.i);

  Q q1=new Q();
  System.out.println(q1.i);

  R r1=new R();
  System.out.println(r1.i);

  P p3=r1;
  System.out.println(p3.i);
  System.out.println(((Q)p3).i);
 }
}

OUTPUT:
/*
1
1
2
3
1
2
*/

Program # 40
class A
{
 void test1()
 {
  System.out.println("test1:"+this);
  test2();//this.test2();
 }
 void test2()
 {
  System.out.println("test2:"+this);
  test3();//this.test3();
 }
 void test3()
 {
  System.out.println("test3:"+this);
 }
 public static void main(final String[] args) 
 {
  A a1=new A();
  System.out.println("Main:"+a1);
  a1.test1();
 }
}

OUTPUT:
Main:A@1a46e30
test1:A@1a46e30
test2:A@1a46e30
test3:A@1a46e30

Type Casting

Type Conversion: A variable of one type can receive the value of another type
Widening conversion : Variable of lower capacity can be assigned to another variable of higher capacity.
Narrowing conversion :Variable of higher capacity can be assigned to another variable of lower capacity, which may lead to loss of data .
Type Casting : Whenever a larger type is converted to a smaller type, we have to explicitly specify the type cast operator
This prevents accidental loss of data.

Among 8 primitive data type 6 are numeric that only matters here :

Byte < Short < int < long < float < double(wider) | other are char,boolean



Program # 1
class A
{
 public static void main(String[] args) 
 {
  int i=10;
  double d=i;//Here integer converting to double ,
                           //auto widening happening ,hence no casting required
  System.out.println(i);
  System.out.println(d);

 }
}

OUTPUT:
/*
10
10.0
*/

Program # 2
class B
{
 public static void main(String[] args) 
 {
  byte b=10;
  int i=b;
  double d=i;
  float f=b;
  long l=i;
  System.out.println("done");
 }
}

OUTPUT:
/*
done
*/

Program # 3
class C
{
 public static void main(String[] args) 
 {
  int i=10;
  test(i);
 }
 static void test(double d)
 {
  System.out.println("test(double)");
 }
}

OUTPUT:
/*
test(double)
*/

Program # 4
class D
{
 public static void main(String[] args) 
 {
  int i=test();
  System.out.println("done");
 }
 static byte test()
 {
  return 10;
 }
}

OUTPUT:
done

Program # 5
class E
{
 public static void main(String[] args) 
 {
  int i=10;
  double d=(double)i;//explicit conversion,not requirred ,compiler will do automatic
  System.out.println("done");
 }
}
//done

Program # 6
class F
{
 static long test()
 {
  int i=10;
  return i;
 }
 public static void main(String[] args) 
 {
  double d=test();
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 7
class G
{
 static float test1(short s)
 {
  return test2(s);
 }
 static long test2(int i)
 {
  return i;
 }
 public static void main(String[] args) 
 {
  byte b=10;
  double d=test1(b);
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 8
class H
{
 public static void main(String[] args) 
 {
  double d=10.9;
  int i=(int)d;
  System.out.println(d);
  System.out.println(i);
 }
}

OUTPUT:
/*
10.9
10
*/

Program # 9
class I
{
 public static void main(String[] args) 
 {
  long x=10;
  test((int)x);
  System.out.println("done");
 }
 static void test(int i)
 {
 }
}

OUTPUT:
/*
done
*/

Program # 10
class Demo
{
 public static void main(String[] args) 
 {
  byte a=10;
  short b=20;
  int c=10;
  long d=20;
  float f=20;
  double g=20.09;
  a=b;//error or keep a=(byte)b;
  b=a;
  d=c;
  c=d;//error or keep c=(int)d;
  g=f;
  f=g;//error or keep f=(flot)g;
  System.out.println("done");
 }
}

OUTPUT:
Demo.java:11: possible loss of precision
found : short
required: byte
a=b;
^
Demo.java:14: possible loss of precision
found : long
required: int
c=d;
^
Demo.java:16: possible loss of precision
found : double
required: float
f=g;


Program # 11
class K
{
 static int test(long x)
 {
  return (int)x;
 }
 public static void main(String[] args) 
 {
  int i=10;
  byte b=(byte)test(i);
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 12
class L 
{
 public static void main(String[] args) 
 {
  double d=20;
  int i=(byte)d;
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 13
class M 
{
 public static void main(String[] args) 
 {
  double d=20.9;
  int i=(short)(int)(byte)(long)d;
  System.out.println("done");
 }
}

OUTPUT:
//done
//Consider below hierarchy for further programs

class A 
{
}
class B extends A
{
}
class C extends B
{
}
class D extends C
{
}
class E extends D
{
}
class F extends E
{
}
see further general example in diffrent .java files


Program # 14
class Manager1
{
 public static void main(String[] args) 
 {
  A a1=new A();
  B b1=new B();
  C c1=new C();
  D d1=new D();
  E e1=new E();
  F f1=new F();
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 15
class Manager2
{
 public static void main(String[] args) 
 {
  A a1=new A();
  A a2=a1;
  B b1=new B();
  B b2=b1;
  C c1=null;
  C c2=c1;
  D d1,d2;
  d1=d2=new D();
  E e1,e2=new E();
  e1=e2;
  F f1,f2=null;
  f1=f2;
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 16
/*
reference variable a1 is A type,object is B type ,right side B is subclass of A.
so automatically B is converting to upper one,autoupcasting happening.
*/
class Manager3
{
 public static void main(String[] args) 
 {
  A a1=new B();  //autoupcasting
  B b1=new D();
  C c1=new E();
  D d1=new F();
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 17
class Manager4
{
 public static void main(String[] args) 
 {
  B b1=null;
  C c1=null;
  D d1=new D();
  E e1=null;
  F f1=new F();
  b1=f1;
  c1=e1; //automatic E type can be converted in C type by compiler
  e1=f1;
  b1=d1;
  d1=e1;
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 18
class Manager5
{
 public static void main(String[] args) 
 {
  E e1=new E();
  test(e1);
 }
 static void test(C c1) //automatic E type can be converted in C type by compiler
 {
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 19
class Manager6
{
 static D test()
 {
  F f1=new F();
  return f1; //autoupcasting into D type
 }
 public static void main(String[] args) 
 {
  B b1=test(); //D is auto upcasting into B type
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 20
class Manager7
{
 static B test1(E e1)
 {
  return test2(e1);
 }
 static C test2(D d1)
 {
  return d1;
 }
 public static void main(String[] args) 
 {
  F f1=new F();
  A a1=test1(f1);
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 21
class Manager8
{
 public static void main(String[] args) 
 {
  A a1=new C();
  B b1=(B)a1; //has to do explicit downcasting ,if not doing then error
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 22
class Manager9
{
 public static void main(String[] args) 
 {
  D d1=new F();
  E e1=(E)d1;
  System.out.println("done");
 }
}

OUTPUT:
//done

Program # 23
class Manager10
{
 public static void main(String[] args) 
 {
  E e1=new F();
  test((F)e1);
  System.out.println("done");
 }
 static void test(F f1)
 {
  System.out.println("test(F)");
 }
}

OUTPUT:
/*
test(F)
done
*/

Program # 24
class Manager11
/*
compiler will check only assignment ,hence no error while compiling
a1 is pointing to A type object that can not be converted into B type,hence runtime error*/
{
 public static void main(String[] args) 
 {
  A a1=new A();
  B b1=(B)a1; //without upcasting if attempt downcasting give run time error.
  System.out.println("done");
 }
}

OUTPUT:
/*
compilation success but runtime exception
Exception in thread "main" java.lang.ClassCastException: A cannot be cast to B
at Manager11.main(Manager11.java:7)
*/

Program # 25
class Manager12
{
 public static void main(String[] args) 
 {
  A a1=new B();
  B b1=(B)a1;
  System.out.println("done");
 }
}

OUTPUT:
/*
done
*/

Program # 26
class Manager13
{
 public static void main(String[] args) 
 {
  B b1=new D();
  C c1=(C)b1;
  D d1=(D)c1;
  System.out.println("done");
 }
}

OUTPUT:
/*
done
*/

Program # 27
class Manager14
{
 public static void main(String[] args) 
 {
  B b1=new E();
  C c1=(C)b1;
  D d1=(D)b1;
  E e1=(E)b1;
  //F f1=(F)b1;
  System.out.println("done");
 }
}

OUTPUT:
/*
done
*/

Program # 28
class Manager15
{
 public static void main(String[] args) 
 {
  A a1=new B();
  System.out.println(a1 instanceof A);
  System.out.println(a1 instanceof B);
  System.out.println(a1 instanceof C);
  System.out.println(a1 instanceof D);
 }
}

OUTPUT:
/*
true
true
false
false
*/

Program # 29
class Manager16
{
 public static void main(String[] args) 
 {
  C c1=new D();
  System.out.println(c1 instanceof A);
  System.out.println(c1 instanceof B);
  System.out.println(c1 instanceof C);
  System.out.println(c1 instanceof D);
  System.out.println(c1 instanceof E);
  System.out.println(c1 instanceof F);
 }
}

OUTPUT:
/*
true
true
true
true
false
false
*/

Program # 30
class Manager17
{
 public static void main(String[] args) 
 {
  A a1=new C();
  if(a1 instanceof A)
  {
   A a2=(A)a1;
  System.out.println(1);
  }
  if(a1 instanceof B)
  {
   B b1=(B)a1;
  System.out.println(2);
  }
  if(a1 instanceof C)
  {
   C c1=(C)a1;
  System.out.println(3);
  }
  if(a1 instanceof D)
  {
   D d1=(D)a1;
  System.out.println(4);
  }
  if(a1 instanceof E)
  {
   E e1=(E)a1;
  System.out.println(5);
  }
  if(a1 instanceof F)
  {
   F f1=(F)a1;
  System.out.println(6);
  }
  System.out.println("done");
 }
}

OUTPUT:
/*
1
2
3
done
*/

Program # 31
class A
{
 int i;
 void test1()
 {
  System.out.println("A-test1");
 }
}
class B extends A
{
 int j;
 void test2()
 {
  System.out.println("B-test2");
 }
}
class Manager1
{
 public static void main(String[] args) 
 {
  A a1=new A();
  a1.test1();
  a1.i=10;
  System.out.println(a1.i);
  B b1=new B();
  b1.test1();
  b1.i=10;
  b1.test2();
  b1.j=20;
  System.out.println(b1.i);
  System.out.println(b1.j);
 }
}

OUTPUT:
/*
A-test1
10
A-test1
B-test2
10
20
*/

Program # 32
/*
If ref variable a1 is pointing to B object,then we can't use member of 
B class using ref veariable a1 ,for that we have to down cast
WHENEVER autoupcasting is there ,we can not access member object class 
for that we have to do downcast explicitly
*/
class A
{
 int i;
 void test1()
 {
  System.out.println("A-test1");
 }
}
class B extends A
{
 int j;
 void test2()
 {
  System.out.println("B-test2");
 }
}
class Manager2
{
 public static void main(String[] args) 
 {
  A a1=new B();
  a1.i=10;
  a1.test1();
  //a1.j=20;
  //a1.test2();
  System.out.println("done");
 }
}

OUTPUT:
/*
A-test1
done
*/

Program # 33
//downcasting ref var a1 to B type ,now member of class B can be access
class A
{
 int i;
 void test1()
 {
  System.out.println("A-test1");
 }
}
class B extends A
{
 int j;
 void test2()
 {
  System.out.println("B-test2");
 }
}
class Manager3
{
 public static void main(String[] args) 
 {
  A a1=new B();
  a1.i=10;
  a1.test1();
  B b1=(B)a1;//downcasting ref var a1 to B type ,now member of class B can be access
  b1.j=20;
  b1.test2();
  b1.i=30;
  b1.test1();
  System.out.println("done");
 }
}

OUTPUT:
/*
A-test1
B-test2
A-test1
done
*/

Program # 34 Example of polymorphism
/*
while calling 2nd time obj2 is D type pointing to D object,while calling method autoupcasting happening.
if polymorphysm is not occuring then method overriding will not happen.
if super class methos is static then while overriding in sub class have to use static,otherwise it will give compiler error
if using static eventhough upcasting is there ,method will not override
static method are not overriding,because static method are not inheriting to subclass
 */
class C
{
 //void test1()
 static void test1()
 {
  System.out.println("c-test1");
 }
}  
class D extends C
{
 //void test1()
 static void test1()
 {
  System.out.println("D-test1");
 }
}
class Manager4
{
 static void method(C c1)
 {
  c1.test1();//showing polymorphysm
 }
 public static void main(String[] args) 
 {
  C obj1=new C();
  D obj2=new D();
  method(obj1);
  method(obj2);
 }
}

OUTPUT:
/*
c-test1
D-test1
*/

Program # 35
class Manager4
{
 static
 {
  System.out.println("Manager4-SIB");
 }
 public static void main(String[] args) 
 {
  System.out.println("Manager4-main");
 }
}
class Manager5 extends Manager4
{
 static
 {
  System.out.println("Manager5-SIB");
 }

}
class Manager6
{
 static
 {
  System.out.println("Manager6-SIB");
 }
 public static void main(String[] args) 
 {
 System.out.println("Manager6-main-b");
 Manager4.main(args);
 System.out.println("Manager6-main-e");
 }
}
class Manager7
{
 static
 {
  System.out.println("Manager7-SIB");
 }
 public static void main(String[] args) 
 {
  System.out.println("Manager7-main-b");
         Manager5.main(args);
  System.out.println("--------");
  Manager4.main(args);
  System.out.println("Manager7-main-e");
 }
}

OUTPUT:
/*
Manager4:-
Manager4-SIB
Manager4-main
==============
MANAGER5:--
Manager4-SIB
Manager5-SIB
Manager4-main
==============
MANAGER6:-
Manager6-SIB
Manager6-main-b
Manager4-SIB
Manager4-main
Manager6-main-e
================
MANAGER7:-
Manager7-SIB
Manager7-main-b
Manager4-SIB
Manager4-main
--------
Manager4-main
Manager7-main-e
*/
Related Posts Plugin for WordPress, Blogger...