Monday, June 30, 2014

Inner Class different scenario

public class InnerClassStuff {
private String string1;
private String string2;

public InnerClassStuff(String string1, String string2) {
this.string1 = string1;
this.string2 = string2;
}

String getValue() {
return string2;
}
}

interface someExtra {
void M1(String a, String v);
}

class InnerInnner1 {
private class InnerHeritance extends InnerClassStuff {
public InnerHeritance(String string1, String string3) {
super(string1, string3);
}
}
}

class InnerInnner2 {
private class InnerHeritance extends InnerClassStuff implements someExtra {
public InnerHeritance(String string1, String string3) {
super(string1, string3);
}

public void M1(String a, String v) {
}
}
}

class InnerStaticMain {
private static class InnerStatic extends InnerClassStuff implements
someExtra {
public InnerStatic(String string1, String string3) {
super(string1, string3);
}

public void M1(String a, String v) {
}
}
}

class InnerMain {
private static class InnerStatic extends InnerClassStuff implements
someExtra {

public InnerStatic(String string1, String string3) {
super(string1, string3);
}

public void M1(String a, String v) {

}

void someExtra() {
someExtra test = new someExtra() {

public void M1(String a, String v) {

}

};

}
}

}

Comparison of generics with type casting

public class ObjectSpecificCondition {
    private Class classType;

    public boolean check(Object obj) {
        boolean check = ((DateObject) obj).getLocalizationdate();
return check;
    }
    public static void main(String[] args) throws Exception {
           DateObject mockdateObject = new DateObject();
           // caseI: No generics used caseI works fine no exception is generated but more lines of code to write
           ObjectSpecificCondition mockanalysis = new ObjectSpecificCondition(DateObject.class);
           ObjectSpecificCondition1 mockanalysis2 = new ObjectSpecificCondition1();

           if (mockanalysis.checkTypeSpecific(mockdateObject)) {
               mockanalysis.check(mockdateObject);

           }
        // caseII:No generics used caseII throws exception in run time .More lines of code to write.It can not capture incompatible type at compile time
           ObjectSpecificCondition mockanalysis1 = new ObjectSpecificCondition(String .class);
           DateObject mockdateObject1 = new DateObject();
           if (mockanalysis.checkTypeSpecific(mockdateObject1)) {
               mockanalysis.check(mockdateObject1);

           }
          // caseIII;Generics used and line of code is reduced to less
           ObjectSpecificConditionGenerics mockgenerics=new ObjectSpecificConditionGenerics() ;
           mockgenerics.check(mockdateObject);

           // caseIV;Generics used and line of code is reduced to less and error for compataible object is generated at compile time
           ObjectSpecificConditionGenerics mockgenerics1=new ObjectSpecificConditionGenerics() ;
           String mockstring=new String();
//           mockgenerics.check(mockstring); // it is captured at compile time ,i think its good to catch at compile time then to pass it at run time

       }
 

    public ObjectSpecificCondition(Class classType) {
        this.classType = classType;
    }

    boolean checkTypeSpecific(Object busineesObject) {
        if (classType.isAssignableFrom(busineesObject.getClass())) {
            return true;
        }
        else{
            try {
throw new Exception();
} catch (Exception e) {
}
        }
return false;

    }
}
 class ObjectSpecificCondition1 {
// Note:The business object of which type is very important .It might be object implementing collection Or Array object Or a simple object  

    public boolean check(Object busineesObject) {
    boolean check ;
       
        if(busineesObject instanceof DateObject|| busineesObject instanceof Action || busineesObject instanceof DateObject ){
        check= ((DateObject) busineesObject).getLocalizationdate();
        Action action=((Action) busineesObject);
        DateObjectInternalization action1=((DateObjectInternalization) busineesObject);
       
        action.distinguish(new Object());
       
        }
        else{
        throw new IllegalArgumentException();
        }
        return check;
    }

    }
 interface Action{
 boolean distinguish(Object object);


 }

   

 
   
   
    class ObjectSpecificConditionGenerics<T extends DateObject> {
        private T classTypeGenerics;

        public boolean check(T genericsobj) {
            return genericsobj.getLocalizationdate();

        }

    }
    class DateObject implements Action{
        boolean getLocalizationdate() {
        return true;
            // return true Or False according to business logic
        }

public boolean distinguish(Object object) {

return false;
}
    }
    class DateObject1 implements Action{
     
public boolean distinguish(Object object) {

return false;
}
    }
    class DateObjectInternalization extends DateObject{
        boolean getLocalizationdate() {
        return true;
            // return true Or False according to business logic
        }

@Override
public boolean distinguish(Object object) {

return false;
}
public boolean isTrue(){
return true;

}
    }
   

Serialization part 2

import java.io.*;
 
public class PlayerData implements Serializable
{
    public PlayerData(String name, int score, String username,
          String password)
    {
       this.name = name;
       this.score = score;
       this.username = username;
       this.password = password;
    }
   
    public String name;
    public int score;
    public String username;
    public String password;
}


import java.io.*;

import org.junit.Test;
   
public class SerializationExample
{
    public SerializationExample()
    {    
       // Create an instance of our PlayerData class...
       PlayerData playerData = new PlayerData("abishkar", 400, 
             "abishkar", "abishkar");
    
       // Create out file object...
       File theFile = new File("output.txt");
       
       // Create the file output stream...
       FileOutputStream fileOutputStream = null;
       try
       {
           fileOutputStream = new FileOutputStream(theFile);
       }    
       catch(FileNotFoundException e)
       {
          System.out.println(e);
       }
       
       // Create the object output stream...
       ObjectOutputStream objectOutputStream = null;
       
       try
       {
          objectOutputStream = new ObjectOutputStream
                (fileOutputStream);
       
          // Write the object to the object output stream...
          objectOutputStream.writeObject(playerData);
       }
       catch(IOException e)
       {
          System.out.println(e);
       }
       
       // Read the object back into a new instance of our
       // 'PlayerData' class
       PlayerData newPlayerData = null;
       
       // Create the file input stream...
       FileInputStream fileInputStream = null;
       try
       {
           fileInputStream = new FileInputStream(theFile);
       }
       catch(FileNotFoundException e)
       {
          System.out.println(e);
       }
       
       // Create the object input stream...
       ObjectInputStream objectInputStream = null;
       
       try
       {
          objectInputStream = new ObjectInputStream(fileInputStream);
       
          // Read the object from the object input stream...
          newPlayerData = (PlayerData) objectInputStream
                 .readObject();
       }
       catch(ClassNotFoundException e)
       {
          System.out.println(e);
       }
       catch(IOException e)
       {
          System.out.println(e);
       }
    
       // Print what was read in...
       System.out.println("The player's name was: "+newPlayerData.name);
               
       System.out.println("The player's score was:   "+newPlayerData.score);
             
       System.out.println("The player's username was: "+newPlayerData.username);
               
       System.out.println("The player's password was: "+newPlayerData.password);
               
    }
    
    @Test
 public void    M1(){
    
       SerializationExample App = new SerializationExample();
    }
    
}
 

AwaitTermination

package Algorithm;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class awaitTermination {


@org.junit.Test
public void Test() throws InterruptedException {
ExecutorService a = Executors.newFixedThreadPool(20);
for (int i = 0; i <= 20; i++) {
a.execute(new Termination());


}
a.shutdown();
// true is returned if all task is completed and for completion 10 sec is waited but this facility was not provided in istermimanted and it does not wait for all tasks to complete and if that task does not complete within given time then it is
if(a.awaitTermination(10, TimeUnit.SECONDS)){
System.out.println("finihed");

}


}

}

class Termination implements Runnable {

@Override
public void run() {
for (int i = 0; i <= 10; i++) {
System.out.println("in");

}

}

}

Join,Start,Run

package Algorithm;

import org.junit.Test;
// executor framework eliminates the need for creating thread and calling it.It automatically handles it ,we  does not have to concern on it,as the code return below does not need to be write while working with thread using executor framework

/*The clear distinction can be seen in Run Vs Start.
 * In Start main method does not wait for Other threads run operation to complete.
 * It is totally asychronous.But,in run it is syschronous after completion of all threads operation main thread again resume its operation.
 * So,in case of start join can be used so that the thread waits until the operation of other thread is completed.
 *
 */

public class Basic {
private int i;
private int k;

public void SomeMethod1() throws InterruptedException {

Runnable separateTask1 = new Runnable() {

@Override
public void run() {
i = 1;

}

};
separateTask1.run();
}

public void SomeMethod11() throws InterruptedException {

Runnable separateTask1 = new Runnable() {

@Override
public void run() {
i = 1;

}

};
Thread thread = new Thread(separateTask1);
thread.start();

// thread.join();
}

public void SomeMethod2() throws InterruptedException {

Runnable separateTask1 = new Runnable() {

@Override
public void run() {
for (int i = 0; i <= 1000000000; i++) {
k = 2;
}

}

};
separateTask1.run();

}

public void SomeMethod22() throws InterruptedException {

Runnable separateTask1 = new Runnable() {

@Override
public void run() {
for (int i = 0; i <= 1000000000; i++) {
k = 2;
}

}

};
Thread thread = new Thread(separateTask1);
thread.start();
thread.join();

}

@Test
public void Sum() throws InterruptedException {
SomeMethod11();
SomeMethod22();

System.out.println(i + k);
}

}

Executor

package Algorithm;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecutorImplementation {
@org.junit.Test
public void Test() {
ExecutorService a = Executors.newFixedThreadPool(20);
//case1
for (int i = 0; i <= 3; i++) {
a.execute(new a());
// List<Runnable> b = a.shutdownNow();
// System.out.println(b);
}
//pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1 in
// shutdownnow attempt to shut all running task and return list of task that never executed
// in shutdown case no new task will be accepted in our case pool size was 20.3 is already started from pool and now in case2 for i=0 to 5 no execution will be done with exception thrown as
//ask Algorithm.a@68fede rejected from java.util.concurrent.ThreadPoolExecutor@134b58c[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 4]
// case2
a.shutdown();
for (int i = 0; i <= 5; i++) {
// so before executing this task we can check i it shudown with method isShutdown
a.execute(new a());
}
}

public void Test1() {
ExecutorService a = Executors.newFixedThreadPool(5);
for (int i = 0; i <= 5; i++) {
a.execute(new a());
}

for (int i = 0; i <= 5; i++) {
a.execute(new a());
}
}

public void Test2() throws InterruptedException, ExecutionException {
ExecutorService a = Executors.newFixedThreadPool(5);
List<Future> resultet=new ArrayList<Future>();
for (int i = 0; i <= 5; i++) {
Future reult=a.submit(new b());
resultet.add(reult);
}
for(Future individualresult:resultet){
individualresult.get();
}

for (int i = 0; i <= 5; i++) {
a.execute(new a());
}
for (int i = 0; i <= 5; i++) {
Future s = a.submit(new a());
s.get();
}
}

public void Test3() throws InterruptedException, ExecutionException {
ExecutorService a = Executors.newFixedThreadPool(5);
List<Pojo> pojolist = new ArrayList<Pojo>();
for (int i = 0; i <= 5; i++) {
pojolist.add(new Pojo());
}
List<ExecutorImplementation.c> list = new ArrayList<ExecutorImplementation.c>();
for (int i = 0; i <= pojolist.size(); i++) {
ExecutorImplementation.c ind = new ExecutorImplementation.c(
pojolist.get(i));
list.add(ind);

}
List<Future<Pojo>> result = a.invokeAll(list);
// iterate all the list for getting the return value ;
result.get(0).get();

}


public void Test4() throws InterruptedException, ExecutionException {
ExecutorService a = Executors.newFixedThreadPool(5);
List<Pojo> pojolist = new ArrayList<Pojo>();
for (int i = 0; i < 5; i++) {
pojolist.add(new Pojo());
}
List<d> list = new ArrayList<d>();
for (int i = 0; i < pojolist.size(); i++) {
d ind = new d(pojolist.get(i));
list.add(ind);

}
List<Future<Pojo>> result = a.invokeAll(list);

}

class c implements Callable<Pojo> {
private Pojo pojo;

private int i = 0;

c(Pojo pojo) {
this.pojo = pojo;

}

@Override
public Pojo call() throws Exception {
return new Pojo();
}
}
}

class a implements Runnable {
private int i = 0;

@Override
public void run() {
i = 1;
i = i + 1;
System.out.println("This is testig ");
System.out.println(i);
}

}

class b implements Callable<Pojo> {

@Override
public Pojo call() throws Exception {
return new Pojo();
}
}

class d implements Callable<Pojo> {
private Pojo pojo;

private int i = 1;

d(Pojo pojo) {
this.pojo = pojo;

}

@Override
public Pojo call() throws Exception {
i++;
System.out.println(pojo.getId());
System.out.println(pojo.getName());
System.out.println(i);
return pojo;
}
}

class Pojo {
private String name = "ram";

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name
*            the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @param id
*            the id to set
*/
public void setId(int id) {
this.id = id;
}

private int id = 11;

}

Runnable

package Algorithm;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class ExtendImpl implements Runnable {

@Override
public void run() {
System.out.println("3");

}

@Test
public void Start() {
List<Runnable> list = new ArrayList<Runnable>();
Runnable impl1 = new ExtendImpl();
Runnable impl11 = new ExtendImpl();
Runnable impl111 = new ExtendImpl();
Runnable impl1111 = new ExtendImpl();
Runnable impl2 = new ExtendImpl1();
Runnable impl3 = new ExtendImpl2();
list.add(impl1);
list.add(impl2);
list.add(impl3);
list.add(impl11);
list.add(impl111);
list.add(impl1111);
for (Runnable r : list) {
r.run();
}
}
}

class ExtendImpl1 implements Runnable {
// Singleton if it uses the same singleton object
//can be problem when both uses it

@Override
public void run() {
System.out.println("1");
}

}

class ExtendImpl2 implements Runnable {
// Singleton if it uses the same singleton object

@Override
public void run() {
System.out.println("2");
}

}
class Singleton{
private String name ;
}

isTerminated

package Algorithm;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class isTerminated {
@org.junit.Test
public void Test() {
ExecutorService a = Executors.newFixedThreadPool(20);
for (int i = 0; i <= 20; i++) {
a.execute(new terminated());


}
// false is returned if all task is completed
// true is returned if all task is completed and shut down Or shut down now is called
if(!a.isTerminated()){
System.out.println("Finished working ");
}

}

}

class terminated implements Runnable {

@Override
public void run() {
for (int i = 0; i <= 100000; i++) {
System.out.println("in");

}

}

}





shutdownNow

Program1
package Algorithm;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class shutdownNow {
@org.junit.Test
public void Test() {
ExecutorService a = Executors.newFixedThreadPool(10);
for (int i = 0; i <= 20; i++) {
a.execute(new shutdownnowclass());
}
// The main point  of both shut down and shut down now is that no new task will be executed after calling these method .An exception will be thrown if tried   to execute after calling these methods
List<Runnable> listofthatneverexecuted=a.shutdownNow();
System.out.println(listofthatneverexecuted);

}

}

class shutdownnowclass implements Runnable {

@Override
public void run() {
for (int i = 0; i <= 2; i++) {
System.out.println("in");

}

}
}

Program2
package Algorithm;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class shutdown {

@org.junit.Test
public void Test() {
ExecutorService a = Executors.newFixedThreadPool(20);
for (int i = 0; i <= 3; i++) {
a.execute(new shutdownclass());
}
// When shut down is called the execution is shut down .Below code is not executed .An exception is thrown .To cure exception following checking can be done
//@if(!a.isShutdown()){
// }
for (int i = 0; i <= 5; i++) {
a.execute(new shutdownclass());
}
a.shutdown();
// now there is no exception as following line is never executed
if(!a.isShutdown()){
for (int i = 0; i <= 5; i++) {
a.execute(new shutdownclass());
}
}
}

}

class shutdownclass implements Runnable {

@Override
public void run() {
for (int i = 0; i <= 1000000000; i++) {
System.out.println("in");

}

}

}




Framework Prototype 1

 If you are writing framework then
the object identifatication level should be there for /framework .For example Object Class is super for java .
So,in framework specific we need a minimium definition of object
how it should look like.Like for example of there are suppose
when loading in tomcat there are 2000 objects then 2000 object
should have a common behaviour that is not default object
class behaviour but our system specific behavior .
Like Comapring  the object is null.
Like a method check Object(Interface definition){

interface.isNull(){
so it works for all the objects of the system .So this functionality can be provided via framework part
}


abstract class AbstractTest {
Object test;
void MethodSuper(){

}
AbstractTest(Object test){
this.test=test;

}
void Method2Super1(){

}
void Method2Super2(){

}
abstract void SubClassSpecified();



}
 abstract class AbstractTest1 {
Object test;
void MethodSuper2(){

}
AbstractTest1(Object test){
this.test=test;

}
void Method2Super2(){

}
abstract void SubClassSpecified();



}

public class AddBusineessLogic implements SuperCommonTask {

@Override
public void display() {
System.out.println("add result");

}

@Override
public void appliationErro() {
System.out.println("this is application error Add case ");

}

@Override
public void systemErro() {
System.out.println("this is systemerror Add case");

}

@Override
public void validatenput() {
}

@Override
public void ShowError() {
}

@Override
public void IsErrorPresent() {
}

}


public class BusinessLogic {
public int Add(int a,int b){
return a+b;
}
int Subtract(int a,int b){
return a-b;
}
int Divide(int a,int b){
return a%b;
}

}



public class ClientClass {
SuperCommonTask add = new AddBusineessLogic();
SuperCommonTask multiply = new MultiplyBusineessLogic();
SuperCommonTask divide = new DivideBusineessLogic();
CommonPointOfOperation CommonPointOfOperationadd = new CommonPointOfOperation(
add);
BusinessLogic businesslogic=new BusinessLogic();
void EntryPoint(int a ,int b){
add.validatenput();
}

}

public class CommonPointOfOperation {
SuperCommonTask SuperCommon;

CommonPointOfOperation(SuperCommonTask SuperCommon) {
this.SuperCommon = SuperCommon;
}

void ShowError() {
SuperCommon.appliationErro();
}

void ShowApplicationError() {
SuperCommon.appliationErro();
}

void ShowsystemError() {
SuperCommon.systemErro();

}
}

public class DivideBusineessLogic implements SuperCommonTask {
@Override
public void display() {
System.out.println("Multiply result");

}

@Override
public void appliationErro() {
System.out.println("this is application error Multiply case ");

}

@Override
public void systemErro() {
System.out.println("this is systemerror Multiply  case");

}

@Override
public void validatenput() {
}

@Override
public void ShowError() {
}

@Override
public void IsErrorPresent() {
}

}


public class InheritancePolymorphism {
void Method1(){
System.out.println("supermethod");
}

}


public class MultiplyBusineessLogic implements SuperCommonTask {
@Override
public void display() {
System.out.println("Multiply result");

}

@Override
public void appliationErro() {
System.out.println("this is application error Multiply case ");

}

@Override
public void systemErro() {
System.out.println("this is systemerror Multiply  case");

}

@Override
public void validatenput() {
}

@Override
public void ShowError() {
}

@Override
public void IsErrorPresent() {
}

}

public class State1{
public static enum Test{
create("1")
,cancel("3")
,read("3")
,update("2")
,initread("3")
,initcancel("3")
,initupdate("3");

Test(String test){
this.test=test;
}
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}

}
}

public interface SuperCommonTask {
void display();

void appliationErro();

void systemErro();
void validatenput();
void ShowError();
void IsErrorPresent();

}



 class Client {
private State1 state;

public State1 getState() {
return state;
}

public void setState(State1 state) {
this.state = state;
}
}




public class Main {

/**
* @param args
*/
public static void main(String[] args) {
Client client=new Client();
client.setState(State1.cancel);
State1 state1=client.getState();
System.out.println(state1+ state1.getTest());
State1[] test=State1.values();
for(State1 state:test){
System.out.println(state.getTest()+state);;
}

}
}

Exception part 1

package Exception;


 class Main1 {
void Method1() throws Exception1 {
String args1[]={"1","2"};
if(args1[0].equals("1")){
throw new Exception1("this is error");
}

}
}


 class Main2 {
void Method1() throws Exception2 {
String args1[]={"1","2"};
if(args1[3].equals("1")){

}

}
}

public class Main {
public static void main(String[] args)  {
Main2 main1=new Main2();
try {
main1.Method1();
} catch (Exception2 e) {
System.out.println(e.getMessage1());
}
System.out.println("continue with message");

}
}

Generics Part 2

package Generics;

import java.math.BigDecimal;

import org.junit.Test;

class GenericsTest<T > {
private T t;

GenericsTest(T t) {
this.t = t;
}

void Method(String test) {
if (t.equals(test)) {
System.out.println("iiiiiiiiiiiii");

}

}

}
class GenericsTest1 <T extends Object > {
private T t;

GenericsTest1(T t) {
this.t = t;
}

void Method(String test) {
if (t.equals(test)) {
System.out.println("iiiiiiiiiiiii");

}

}

}

public class Generics {
@Test
public void M1(){
GenericsTest<BigDecimal> generics = new GenericsTest<BigDecimal>(new BigDecimal(0));
generics.Method("test");

}

}

Inheritance Part 2

package Inheritance;

public class GeneralClass {
Version1Program version1=new Version2Program();

private Object test;
GeneralClass(Object test){
this.test=test;


}
void GenaralMethod(){
version1.PrintCommom3();

}




}
package Inheritance;

import java.math.BigDecimal;

public class Version1Program {

private String common1;
private String common2;
private BigDecimal common3;
@SuppressWarnings({ "unused", "rawtypes" })
private Class[] test;

Version1Program() {
common1="common1default";
common2="common2default";
common3=new BigDecimal(0);
}
Version1Program(String common1,String common2, BigDecimal common3){
this.common1=common1;
this.common2=common2;
this.common3=common3;
}
Version1Program(Class<?> test){
this(new Class []{test});
}
Version1Program(Class<?> test[]){
this.test=test;
}

Version1Program(String common2)
{
this(null,common2,null);
}
Version1Program(BigDecimal common3)
{
this(null,null,common3);
}

public void PrintCommom1() {
System.out.println(common1);

}
public void PrintCommom2() {
System.out.println(common2);

}
public void PrintCommom3() {
System.out.println(common3);

}


}
package Inheritance;

import java.math.BigDecimal;

public class Version2Program extends Version1Program{
private String common11;
private String common22;
private BigDecimal common33;
@SuppressWarnings({ "unused", "rawtypes" })
private Class[] array;

Version2Program() {
common11="common11default";
common22="common22default";
common33=new BigDecimal(00);
}
Version2Program(String common1,String common2, BigDecimal common3){
super(common1,common2,common3);
this.common11=common1;
this.common22=common2;
this.common33=common3;
}
Version2Program(String common11)
{
this(common11,null,null);
}

Version2Program(BigDecimal common33)
{
this(null,null,common33);
}
Version2Program(@SuppressWarnings("rawtypes") Class[] array){
this.array=array;
}

public void PrintCommom11() {
System.out.println(common11);

}
public void PrintCommom22() {
System.out.println(common22);

}
public void PrintCommom33() {
System.out.println(common33);

}
public void PrintCommom3() {
System.out.println("I love you ............");

}


}


package Inheritance;

import java.math.BigDecimal;

import org.junit.Test;


public class Main {
@Test
public void Method1() {
GeneralClass generalclass=new GeneralClass(new Object());
generalclass.GenaralMethod();
}

}

Inheritance part 1

package InheritancePolymorphism;

public class InheritancePolymorphism {
public void Method1(){
System.out.println("supermethod");

}


}

package InheritancePolymorphism;

public class SubClass1 extends InheritancePolymorphism{

public SubClass1() {
}
public void Method1(){
System.out.println("subclass1");
}
package InheritancePolymorphism;

public class SubClass2 extends InheritancePolymorphism{

public SubClass2() {
}
public void Method1(){
System.out.println("subclass2");
}

}

package InheritancePolymorphism;

import org.junit.Test;




public class Main {
@Test
public void Method1() {
InheritancePolymorphism[] tests={new SubClass1(),new SubClass2()};
for(InheritancePolymorphism test:tests){
test.Method1();
}
}

}

Generics Part1

package GenericsDemo;




public class RealGenerics<E extends SupprGenerics> extends SupprNonGenerics{
E t;
RealGenerics(E t){
this.t=t;
}
String Opeartion1(){
return t.Opeartion1();
}
String Opeartion2(){
return t.Opeartion1();
}

}


package GenericsDemo;

public class SupprGenerics {
private String String1 ;
private String string2;
String Opeartion1(){
return String1;
}
String Opeartion2(){
return string2;
}

}


package GenericsDemo;



public class SupprGenericsExtendLevel1 extends SupprGenerics{
private String String1 ;
private String string2;
String Opeartion11(){
return String1;
}
String Opeartion22(){
return string2;
}

}

package GenericsDemo;



public class SupprGenericsExtendLevel2 extends SupprGenericsExtendLevel1 {
private String String1;
private String string2;
private String string3;

String Opeartion111() {
return String1;
}

String Opeartion222() {
return string2;
}

String Opeartion333(RealGenerics<? super SupprGenericsExtendLevel2> a) {
return string3;
}

}

package GenericsDemo;


public class SupprNonGenerics {
private String String1 ;
private String string2;
String Opeartion11(){
return String1;
}
String Opeartion22(){
return string2;
}

}


Deserialize Byte array to Object

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class Serilization {

private static final byte[] serializedForm = new byte[] {
(byte)0xac, (byte)0xed, 0x00, 0x05, 0x73, 0x72, 0x00, 0x06,
0x50, 0x65, 0x72, 0x69, 0x6f, 0x64, 0x40, 0x7e, (byte)0xf8,
0x2b, 0x4f, 0x46, (byte)0xc0, (byte)0xf4, 0x02, 0x00, 0x02,
0x4c, 0x00, 0x03, 0x65, 0x6e, 0x64, 0x74, 0x00, 0x10, 0x4c,
0x6a, 0x61, 0x76, 0x61, 0x2f, 0x75, 0x74, 0x69, 0x6c, 0x2f,
0x44, 0x61, 0x74, 0x65, 0x3b, 0x4c, 0x00, 0x05, 0x73, 0x74,
0x61, 0x72, 0x74, 0x71, 0x00, 0x7e, 0x00, 0x01, 0x78, 0x70,
0x73, 0x72, 0x00, 0x0e, 0x6a, 0x61, 0x76, 0x61, 0x2e, 0x75,
0x74, 0x69, 0x6c, 0x2e, 0x44, 0x61, 0x74, 0x65, 0x68, 0x6a,
(byte)0x81, 0x01, 0x4b, 0x59, 0x74, 0x19, 0x03, 0x00, 0x00,
0x78, 0x70, 0x77, 0x08, 0x00, 0x00, 0x00, 0x66, (byte)0xdf,
0x6e, 0x1e, 0x00, 0x78, 0x73, 0x71, 0x00, 0x7e, 0x00, 0x03,
0x77, 0x08, 0x00, 0x00, 0x00, (byte)0xd5, 0x17, 0x69, 0x22,
0x00, 0x78 };
public static void main(String[] args) {
Test p = (Test) deserialize(serializedForm);
System.out.println(p);
}
//Returns the object with the specified serialized form
public static Object deserialize(byte[] sf) {
try {
InputStream is = new ByteArrayInputStream(sf);
ObjectInputStream ois = new ObjectInputStream(is);
return ois.readObject();
} catch (Exception e) {
throw new IllegalArgumentException(e.toString());
}
}
}