Core Java question list #4

1. Which object oriented concept is achieved by using overloading and overriding?

Polumorphism

2. Why does Java not support operator overloading?

Operator overloading makes the code vert difficult to read and maintain. To maintain code simplicity, Java doesn't support operator overloading.

(1). Simplicity and Cleanliness.

(2). Avoid Programming Errors.

(3). JVM Complexity.

(4). Easy Development of Tools.

3. Can we define private and protected modifiers for variables in interfaces?

No.

4. What is Externalizble and Serialization?

In case of Serializable JVM has fill control for serializing object. While in case of Externalizable, application gets control for persisting objects. WriteExternal() and readExternal() method provides complete control on format and content of Serialization process to application which can be leverage to increase performance and speed of serialization process.

(1). In case of Serializable, default serialization process is used. While in case of Externalizable custom Serialization process is used which is implemented by application.

(2). JVM gives call back to readExternal() and writeExternal() of Java.io.Externalizable interface for restoring and writing objects into persistence.

(3). Externalizable interface provides complete control of serialization process to application.

(4). readExternal() and writeExternal() supersede any specific implementation of writeObject and readObject methods.

Serializable is a mark interface, tell JVM we need to transmit object like a binary.

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.ObjectStreamException;

 

public class Test1 implements java.io.Serializable{  

private String message;  

 

public String getFoo() {          

    return message;  

}  

 

public void setMessage(String message) {  

    this.message = message;  

}  

 

private void writeObject(java.io.ObjectOutputStream out) throws IOException {  

    System.out.println("writeObject invoked");  

    out.writeObject(this.message == null ? "hohohahaha" : this.message);  

}  

 

private void readObject(java.io.ObjectInputStream in) throws IOException,  

        ClassNotFoundException {  

    System.out.println("readObject invoked");  

    this.message = (String) in.readObject();  

    System.out.println("got message:" + message);  

}  

 

private Object writeReplace() throws ObjectStreamException {  

    System.out.println("writeReplace invoked");  

    return this;  

}  

 

private Object readResolve() throws ObjectStreamException {  

    System.out.println("readResolve invoked");  

    return this;  

}  

 

public Object serialize() throws IOException, ClassNotFoundException {  

    ByteArrayOutputStream baos = new ByteArrayOutputStream();  

    ObjectOutputStream oos = new ObjectOutputStream(baos);  

    oos.writeObject(this);  

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());  

    ObjectInputStream ois = new ObjectInputStream(bais);  

    return ois.readObject();  

}  

        public static void main(String[] args) throws IOException,   

             ClassNotFoundException {  

                 Test1 fooimpl = new Test1();  

                 fooimpl.serialize();  

}

}

 

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectInput;

import java.io.ObjectInputStream;

import java.io.ObjectOutput;

import java.io.ObjectOutputStream;

import java.io.ObjectStreamException;

 

public class Test2 implements java.io.Externalizable {  

    private String message;  

 

    public String getFoo() {  

        return message;   

    }  

 

    public void setMessage(String message) {  

        this.message = message;  

    }  

 

    private Object writeReplace() throws ObjectStreamException {  

        System.out.println("writeReplace invoked");  

        return this;  

    }  

 

    private Object readResolve() throws ObjectStreamException {  

        System.out.println("readResolve invoked");  

        return this;  

    }  

 

    public Object serialize() throws IOException, ClassNotFoundException {  

        ByteArrayOutputStream baos = new ByteArrayOutputStream();  

        ObjectOutputStream oos = new ObjectOutputStream(baos);  

        oos.writeObject(this);  

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());  

        ObjectInputStream ois = new ObjectInputStream(bais);  

        return ois.readObject();  

    }  

 

    public void readExternal(ObjectInput arg0) throws IOException,  

            ClassNotFoundException {  

        System.out.println("readExternal invoked");  

        Object obj = arg0.readObject();  

        System.out.println("Got message:"+obj);

    }  

 

    public void writeExternal(ObjectOutput arg0) throws IOException {  

        System.out.println("writeExternal invoked");  

        arg0.writeObject("Hello world");  

    }  

         public static void main(String[] args) throws IOException,   

              ClassNotFoundException {  

                 Test2 fooimpl = new Test2();  

                  fooimpl.serialize();  

    }

 

 

5. What modifiers are allowed for methods in an Interface?

Only public and abstract modifiers are allowed for methods in interfaces.

你可能感兴趣的:(Core Java question list #4)