Question on Interface, Static Method, String

1) Why we need interfaces?  Which all classes implement the interface? Should implement the methods.  We can't save any coding using interfaces.  Then why we should use interface, we can directly write those methods

2) Statement is an interface,there is the methods written (ex:st.executeQuery()-- we can use this but we are not defining this method)

3) Interface Serializable is marker interface, so it don't have anybody, then what is making the class serializable.

4) Why static methods can't override?

5) Please give an example and show that String is immutable.

Jimmy

Please find the explainations below:

1A) An interface provides a means of setting a standard. It defines a contract that promotes reuse. If an object implements an interface then that object is promising to conform to a standard. An object that uses another object is called a consumer. An interface is a contract between an object and its consumer.

An interface also provides a level of abstraction that makes programs easier to understand. Interfaces allow developers to start talking about the general way that code behaves without having to get in to a lot of detailed specifics.

We need interfaces when we want to provide the functionality across many class hierarchies rather than in a single class hierarchy. all classes which implement the interfaces should implement all the methods declared in that interface. But you can have adopter classes for the methods in which you want to provide the implementation.

For example:  there are two class hierarchies a) living things b) non-living things
In living things, we have different types of birds,flies etc..
In non-living things, we have helicaptors, aeroplanes & rockets
and now we want to measure the velocities & distances covered by both of them in a certain period of time..then we can have an interface called "measure" which can have methods like "measureSpeed" and "measureDistance" and so on..for each of the classes they can have different implementations of it.

Hope this will be suffice

2A) JDBC is a specification. All the interfaces & classes defined in this package are converted back to "JDBC driver specific" code. It will be understood when you read the different types of JDBC drivers & how they work.

Please read it.

3A) This is a good question.
Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

 private void writeObject(java.io.ObjectOutputStream out)
     throws IOException
 private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException;

4A) Static methods cant be overriden they can only be hidden becz, static methods are are associated with the class not with the objects. Static methods are implicitly "final", because overriding is only done based on the type of the objects.

That is why static methods wont have "this".

5A) String is immutable, once its constructed, cant be changed.
for ex: String s1=new String("example");
           Hashtable.put ("key", s1);
          String s2=new String("test");
          s1= s1 + s2 ;
         now, the s1 is another  object which has the different value which does not effect the value that is stored in the hashtable.

Ashok

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.