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
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.
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)
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.
Ashok
Do you have a Java Problem?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|