Java Programming Fundamentals -2
12. Why java does not support multiple inheritance
In Java any class can extend only a single class. However a class can implement several interfaces. So if multiple inheritance needs to be implemented we can use interfaces.
13. What is deep cloning & shallow cloning
Shallow Copy
Generally clone method of an object, creates a new instance of the same class and copies all the fields to the new instance and returns it. This is nothing but shallow copy. Object class provides a clone method and provides support for the shallow copy. It returns ‘Object’ as type and you need to explicitly cast back to your original object.
Since the Object class has the clone method (protected) you cannot use it in all your classes. The class which you want to be cloned should implement clone method andoverwrite it. It should provide its own meaning for copy or to the least it should invoke the super.clone(). Also you have to implement Cloneable marker interface or else you will get CloneNotSupportedException. When you invoke the super.clone() then you are dependent on the Object class’s implementation and what you get is a shallow copy.
Deep Copy
When you need a deep copy then you need to implement it yourself. When the copied object contains some other object its references are copied recursively in deep copy. When you implement deep copy be careful as you might fall for cyclic dependencies. If you don’t want to implement deep copy yourselves then you can go for serialization. It does implements deep copy implicitly and gracefully handling cyclic dependencies.
More details are here.14. What is Generics?
Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.
15. What is annotation?
An annotation is a special form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. Unlike Javadoc tags, Java annotations can be reflective in that they can be embedded in class files generated by the compiler and may be retained by the Java VM to be made retrievable at run-time
16. What is static import?
The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:
import static java.lang.Math.PI;
or :
import static java.lang.Math.*;
Once the static members have been imported, they may be used without qualification:
double r = cos(PI * theta);
The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.
17. What is Marker/Tag interface?
A tag interface (also called a marker interface) is simply an interface with no methods. Some examples of tag interfaces from the JDK :
However, even though it has no methods, a tag interface always carries type information. In some cases, type information itself can be used to solve a problem. For example, Java's serialization mechanism requires an object to implement Serializable before it will serialize it. As stated in itsjavadoc :
The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.
18. New features in Java 1.5
19. Template in generics
- GenericsThis long-awaited enhancement to the type system allows a type or method to operate on objects of various types while providing compile-time type safety. It adds compile-time type safety to the Collections Framework and eliminates the drudgery of casting.
- Enhanced for LoopThis new language construct eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays.
- Autoboxing/UnboxingThis facility eliminates the drudgery of manual conversion between primitive types (such as int) and wrapper types (such as Integer).
- Typesafe EnumsThis flexible object-oriented enumerated type facility allows you to create enumerated types with arbitrary methods and fields. It provides all the benefits of the Typesafe Enum pattern ("Effective Java," Item 21) without the verbosity and the error-proneness.
- VarargsThis facility eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length argument lists.
- Static ImportThis facility lets you avoid qualifying static members with class names without the shortcomings of the "Constant Interface antipattern."
- Metadata (Annotations)This language feature lets you avoid writing boilerplate code under many circumstances by enabling tools to generate it from annotations in the source code. This leads to a "declarative" programming style where the programmer says what should be done and tools emit the code to do it. Also it eliminates the need for maintaining "side files" that must be kept up to date with changes in source files. Instead the information can be maintained in the source file.
A collection with a generic type has a type parameter that specifies the element type to be stored in the collection.
Eg:
class HashMapextends AbstractMap implements Map {
// ...
public V get(Object k) {
...
int hash = k.hashCode(); ...
}
// ...
}
Genericity is not limited to classes and interfaces, you can define generic methods. Static methods, nonstatic methods, and constructors can all be parameterized in almost the same way as for classes and interfaces, but the syntax is a bit different. Generic methods are also invoked in the same way as non-generic methods.
Eg:
void printCollection(Collection c) {
for(Object o:c) {
System.out.println(o);
}
}
There are three types of wildcards:There are three types of wildcards:
- "? extends Type": Denotes a family of subtypes of type Type. This is the most useful wildcard
- "? super Type": Denotes a family of supertypes of type Type
- "?": Denotes the set of all types or any
Comments
Post a Comment