Java Interview Reference Guide--reference

Part 1

http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/

JAVA Object Oriented Concepts

Java in based on Object Oriented concepts, which permits higher level of abstraction to solve any problem in realistic way.

Object oriented approach conceptualize the problem solution in real-world object which are easier to reuse across the application. For example Chair, Fan, Dog, computer etc.

In Java, a class is a blueprints, template or prototype that define common behavior of object of same kind. An instance is realization of a particular class and all instances of a class have similar properties, as described in the class definition. For example, you can define a class called House with number of room as attribute and create instances such as house with 2 rooms, house with 3 rooms etc.

Benefits:

Below are few advantages of object oriented software development:

  • Less maintenance cost mostly because it is modular.
  • Better code reusability due to features such as inheritance and hence faster development.
  • Improved code reliability and flexibility
  • Easy to understand due to real-world modeling.
  • Better abstraction at object level.
  • Reduced complexity during the transitions from one development phase to another.

There are four main features of OOPS:

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

Encapsulation:  

Encapsulate provides contract to other object to what to hide and what to expose which can be accessed other objects. In java we use private access modifier to hide method and variable to restrict from outer world. Java also provide different access modifier such as public, default, protected and private, which use to hide the visibility on different level but end goal is to encapsulate the things, which need not to change.  As per best practice A class should have only one reason to change and Encapsulate bring the reality to achieve “one-reason” design principle.

As per best practices in Encapsulation means hiding things that are expected to change often so to avoid breaking other classes.

Benefits: Below are few advantages of Encapsulation: 

  • We can protect the internal state on objects by hiding its attribute.
  • It Improves code modularity by preventing objects interacting with each other in an unexpected way.
  • Increases usability
  • It Maintain the contracts of specific object
  • Encapsulation promotes maintenance
  • Code changes can be made independently

Polymorphism: 

Polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types). That means classes have different functionality while sharing a common interface and can be invoked dynamically by passing specific class reference.

The classic example is the Shape class and all the classes that can inherit from it (square, circle, dodecahedron, irregular polygon, splat and so on).

In this example, every class would have its own Draw () function and the client code could simply do:

Shape shape=new Square ();

Shape.area() to get the correct behavior for any shape.

The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they’re all used the same way.

The process used by object-oriented programming language to implement run-time polymorphism is called dynamic binding.

Note: Polymorphism is the ability to select more specialized methods in runtime depending on the object being used to call it. Polymorphism could occur with no abstract classes involved.

Benefits:

  • Create Reusable code: It means once classes create, implemented and tested then it can be easily used without caring what written in the class.
  • It provides more generic and loosely coupled code.
  • Compile time is much lower and allow faster development.
  • Dynamic binding:
  • Same interface could be used for creating methods with different implementations
  • Complete implementation can be replaced by using same method signatures

Method Overriding to achieve Polymorphism: Overriding deals with two methods; one in the parent class and the other one in the child class and have the same name and signatures.

Overriding lets you define the same operation in different ways for different object types

E.g.

while(it.hasNext()) {

Shape s = (Shape) it.next();

totalArea += s.area(dim); //polymorphic method call. Will call right object method

 }

Java Interview Reference Guide--reference

Method Overloading or Ad-hoc polymorphism or static polymorphism:

Overloading deals with multiple methods in the same class with the same name but different method signatures. Overloading lets you define the same operation in different ways for different data. Some time it says as static polymorphism but actually it is not polymorphism.

Method overloading is nothing more than having two methods with the same name but different argument lists. It has nothing to do with inheritance and polymorphism. An overloaded method is not the same as an overridden method. [Head First Java]

Parametric polymorphism through generics in Java:

Within a class declaration, a field name can associate with different types and a method name can associate with different parameter and return types. Java supports parametric polymorphism via generics.

An example is a list, which can accept the type of data it contains through generics.

    List<String> list = new ArrayList<String>();

Why we can’t override a static method in Java?

Overriding depends on having an instance of a class. The point of polymorphism is that you can subclass a class and the objects implementing those subclasses will have different behaviors for the same methods defined in the superclass (and overridden in the subclasses). A static method is not associated with any instance of a class so the concept is not applicable.

There were two considerations driving Java’s design that impacted this. One was a concern with performance: there had been a lot of criticism of Smalltalk about it being too slow (garbage collection and polymorphic calls being part of that) and Java’s creators were determined to avoid that. Another was the decision that the target audience for Java was C++ developers. Making static methods work the way they do had the benefit of familiarity for C++ programmers and was also very fast, because there’s no going up the class hierarchy to figure out which method to call, you go straight to the class and call the specified method. [Stack overflow]

 Inheritance:

It is the inclusion of behavior (i.e. methods) and state (i.e. variables) of a base class in a derived class so that they are accessible in that derived class. The key benefit of Inheritance is that it provides the formal mechanism for code reuse and avoids duplication

Inherited class extends application’s functionality by reusing parent behavior and adding new functionalities. This will make the design tight coupled because if you want to change the superclass, you must know all the details of the subclasses to avoid breaking

It is form of software reusability where new class (sub class) are created from already existing class (super class) which enhance the functionalities while using some of the properties from super class.

So if you have a Parents class, then you have a Child class that extends Parent, Child inherits all the things that Person has.

Benefits:

  • Improve reusability
  • Establishes logically “is a” relationship: e.g. Dog is a animal
  • Modularize code
  • Avoid duplicity

Drawback:

  • Tightly coupled: Subclass depends on parent class implementation that’s why tight couple.

Abstraction:

Abstraction means develop class in terms of their interfaces and functionality, instead of their implementation details. Abstract class expose interfaces without including actual implementation. It separates the object implementation from its behavior or implementation.  Abstraction reduces the complexity by hiding irrelevant detail.

Benefits:

  • By using abstraction, we can separate the things that can be grouped to another type.
  • Frequently changing properties and methods can be grouped to a separate type so that the main type need not under go changes. This adds strength to the OOAD principle -”Code should be open for Extension but closed for Modification”.
  • Simplifies the representation of the domain models.

Difference between Abstraction and Encapsulation

Encapsulation is a strategy used as part of abstraction. Encapsulation refers to the state of objects – objects encapsulate their state and hide it from the outside; outside users of the class interact with it through its methods, but cannot access the classes state directly. So the class abstracts away the implementation details related to its state.

Abstraction is a more generic term; it can also be achieved by (amongst others) sub classing. For example, the class List in the standard library is an abstraction for a sequence of items, indexed by their position, concrete examples of a List are an ArrayList or a LinkedList. Code that interacts with a List abstracts over the detail of which kind of a list it is using [Stack overflow]

Abstraction is often not possible without hiding underlying state by encapsulation – if a class exposes its internal state, it can’t change its inner workings, and thus cannot be abstracted

What is abstract class and abstract method?

In design, you want the base class to present only an interface for its derived classes. This means, you don’t want anyone to actually instantiate an object of the base class. You only want to upcast to it (implicit upcasting, which gives you polymorphic behavior), so that its interface can be used. This is accomplished by making that class abstract using the abstract keyword.

Provide some restriction on to not to instantiate the abstract class and whoever use should implement the abstract method. And provide polymorphism

Abstract class can contain both abstract methods and concrete methods. In a class, if one method is declared abstract, the class must be declared abstract. However, reverse of this is not necessarily true. If class is declared abstract class, it may not have abstract method in it.

If a method does not provide actual implementation but just provides method signature, it is called abstract method. Actual implementation is left for the sub classes who extend abstract class.

Abstract method cannot be instantiated; another class can only extend it.

When to use an abstract class?

Abstract classes let you define some default behavior and force subclasses to provide any specific behavior.

For example: List is interface whereas AbstractList provide default behavior of List which can be used as is or refined in subclass e.g. ArrayList.

What is Interface?

The interface keyword takes this concept of an abstract class a step further by preventing any method or function implementation at all. You can only declare a method or function but not provide the implementation. The class, which is implementing the interface, should provide the actual implementation. The interface is a very useful and commonly used aspect in OO design, as it provides the separation of interface and implementation and enables you to:

Benefits on interface:

  1. Multiple inheritance
  2. Loose coupling-defined abstraction of operation as separate layer-underline implementation could be anything, JDBC, JPA, JTA etc.
  3. Program to interface not implement
  4. Polymorphism with dynamic binding – Reveal an object’s programming interface without revealing its actual implementation.
  5. Abstract Layer: Separation concerns

Difference between interface and abstract class:

  • An interface is a contract to ask classes (whoever going to implement interface) to implement the interface the way it defines in interface. It is an empty shell with method declaration.
  • Abstract class define some common behavior and ask subclass to define uncommon or specific behavior to that class
  • Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public
  • When inheriting an abstract class, the child class must define the abstract methods, whereas an interface can extend another interface and methods don’t have to be defined
  • A child class can only extend a single abstract (or any other) class, whereas an interface can extend or a class can implement multiple other interfaces.
  • A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility
  • Interface doesn’t contain constructor whereas Abstract class does.
  • Variables declared in a Java interface is by default final. An abstract class may contain non-final variables
  • Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc

Marker interfaces: The interfaces with no defined methods act like markers. They just tell the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently.
Example: java.io.Serializablejava.lang.Cloneablejava.util.EventListener etc.

Composition:

Code reusability could be achieved by implementing inheritance or composition but the composition approach to code reuse provides stronger encapsulation than inheritance, because a change to a back-end class needn’t break any code that relies only on the front-end class.

Composition is the design technique to implement has-a relationship in classes. We can use java inheritance or Object composition for code reuse

Composition is about expressing relationships between objects. Think about the chair example. A chair has a Seat. A chair has a back. And a chair has a set of legs. The phrase “has a” implies a relationship where the chair owns, or at minimum, uses, another object. It is this “has a” relationship which is the basis for composition

Benefits:

  • Control the visibility
  • Implementation can replace run-tim
  • Loose coupled as interface class doesn’t depends on implementation.

Difference between Composition and Inheritance? 

No. Composition (has a) Inheritance (is a)
1 Advocates polymorphism and code reuse Advocates polymorphism and code reuse
2 Object is acquired at Done at run- time Object is acquired dynamically at compile-time
3 Implementation can be replaced at run-time Implementation can be replaced at compile-time
4 Subclass doesn’t depends parent class and favors loose coupling (specifically in interface driven) Subclass depends on parent class implementation that’s why tight couple
5 Used when House has a Bathroom. It is incorrect to say House is a bathroom Inheritance is Uni-directional. e.g. House is a Building. But Building is not a House

Note: Don’t use inheritance just to get code reuse. If there is no ‘is a’ relationship then use composition for code reuse.

Difference between Composition and aggregation in Object Relationship

Aggregation: Aggregation is an association in which one class belongs to a collection. This is a part of a whole relationship where a part can exist without a whole. It is weaker relationship. No cyclic dependency. Example: Order and product

Composition: Composition is an association in which one class belongs to a collection. This is a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted. It is stronger relationship. e.g. Polygon and points, order and order detail 

What are the non-final methods in Java Object class, which are meant primarily for extension?
The non-final methods are:
? equals(),
? hashCode(),
? toString(),
? clone(),
? finalize ().
The other methods like wait (), notify (), notifyAll (), getClass () etc. are final methods and therefore cannot be overridden.

Note: The equals () and hashCode () methods prove to be very important, when objects implementing these two methods are added to collections. If implemented incorrectly or not implemented at all then your objects stored in a collection like a Set, List or Map may behave strangely and also is hard to debug.

Part 2

http://techmytalk.com/2014/06/02/java-interview-reference-guide-part-2/

Access Modifier

Please refer to Java Interview Reference Guide – Part 1 for basic OOPS concepts
Access modifier depicts how other classes access a class and its members (methods and variable.
The access modifiers are

 

  • Private: A private variable or method (not external class) may only be used by an instance of the class that declares the variables or method.
  • Default: A class’s data and method and class itself may be default. A class default features are accessible to any class in the same package.
  • Protected: more accessible than default. Only variable and method may be declared as protected. A protected feature of a class is available to all subclasses of the class that owns the protected features. This access is provided even to subclasses that reside in a different package.
  • Public: A public class, variable or method may be used access any Java program without any restriction

Note:

  • The only access modifier permitted to non-inner class is public there is no such thing as protected or private top-level class.
  • A feature may have at most one access modifier. If a feature has no access modifier then it access default mode.
  • Method overridden privacy: Method may not be overridden to be more private.

The following table shows the access to members permitted by each modifier.

Java Interview Reference Guide--reference

Apart from basic access modifier below table depicts modifiers in Java, which also use to alter the behavior of class and its members such as method, variable, inner classes.

Java Modifiers

Java Interview Reference Guide--reference

Final
Final modifies may be applied to class, methods and variables and its mean that final features may not be changed. E.g.

  • A final class may not be subclasses
  • A final variable may not be modified once it has been assigned a value
  • A final method may not be overridden

Note: If a final variable is a reference to an object, it is the reference that must stay the same, not the object.

Static:

The static modifier can be applied to variables, methods, code block and inner class. If Java instance variables are given separate memory for storage. If there is a need for a variable to be common to all the objects of a single java class, then the static modifier should be used in the variable declaration

  • Static method: java static methods are common to classes and not tied to a java instance. Static method is not allowed to use non-static features of their class although they are free to access class’s static data and method. Static method may not be overridden to be non-static. Java static methods cannot use the ‘this’ keyword.
  • Static block: A block of static code execute only exactly once while class loading.
  • Static variables: Java instance variables are given separate memory for storage. If there is a need for a variable to be common to all the objects of a single java class, then the static modifier should be used in the variable declaration. Any java object that belongs to that class can modify its static variables. Also, an instance is not a must to modify the static variable and it can be accessed using the java class directly. Static variables can be accessed by java instance methods also. When the value of a constant is known at compile time it is declared ‘final’ using the ‘static’ keyword
  • Static Inner class: Only an inner class can be declared using the static modifier and called nested static classes in java.

Outer and Inner classes (or Nested classes)

In Java not all classes have to be defined separate from each other. You can put the definition of one class inside the definition of another class. The inside class is called an inner class and the enclosing class is called an outer class. So when you define an inner class, it is a member of the outer class in much the same way as other members like attributes, methods and constructors.

When you access private data members of the outer class, the JDK compiler creates package-access member functions in the outer class for the inner class to access the private members. This leaves a security hole. In general we should avoid using inner classes.

Use inner class only when an inner class is only relevant in the context of the outer class and/or inner class can be made private so that only outer class can access it. Inner classes are used primarily to implement helper classes like Iterators, Comparators etc. that are used in the context of an outer class.

Difference between static and non-static nested classes

A non-static nested class (or ‘inner class’) has full access to the members of the class within which it is nested. A static nested class does not have a reference to a nesting instance, so a static nested class cannot invoke non-static methods or access non-static fields of an instance of the class within which it is nested.

In case of declaring member fields and methods, non-static inner class cannot have static fields and methods. But, in case of static inner class, can have static and non-static fields and method.

The instance of non-static inner class is created with the reference of object of outer class, in which it has defined; this means it has enclosing instance. But the instance of static inner class is created without the reference of Outer class, which means it does not have enclosing instance. E.g..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Public Class OuterClass
{
 Class InnerClass
 {
 // static int x; not allowed here
 }
 Static class StaticInnerClass
 {
 Static int x; // allowed here
 }
}
Class Test
{
 public static void main(String… str)
 {
 OuterClass oc=new OuterClass ();
 OuterClass.InnerClass obj1 =oc.new InnerClass();//need of inclosing instance
 OuterClass.StaticInnerClass obj2 =new OuterClass.SIC();
// no need of reference of object of outer class
 }
}

Native

The native modifier can refer only to methods. Native indicate that body of a method is to be found outside of JVM.

Note: In the case of abstract methods, the body is in subclass with native method the body lies entirely outside Java virtual memory, in a library.

Transient

The transient modifier applies only to variable. A transient variables is not stored as part of its object’s persistent state. Use for security key, connection etc. that not required to searlized.

Synchronized

The synchronized modifier is used to control access to critical code in multi-threaded program. The synchronized keyword is one of the tools that make your code thread safe.

It is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads

When you mark a block of code as synchronized you use an object as a parameter. When an executing thread comes to such a block of code, it has to first wait until there is no other executing thread in a synchronized block on that same object. However a thread could enter the block of synchronized method other object.

But non-synchronized method in same object can access without checking lock.

If you synchronize on a static method you will synchronize on the class (object) and not on an instance (object). That means while execution of a static method the whole class is blocked. So other static synchronized methods are also blocke

When a thread has entered a synchronized instance method then no other thread can enter any of synchronized instance methods of the same instance

When a thread entered a synchronized static method then no other thread can enter any of synchronized static methods of the same class

Note: No link between synchronized static methods and synchronized non static methods, ie:if static synchronize and non-static synchronize method can run concurrently method unless your non-static method must explicitly synchronize on the class itself (ie, synchronized (MyClass.class) {…}

Volatile

Only variable may be volatile. Such variable might be modified asynchronously so the compiler takes special precaution The volatile modifier guarantees that any thread that reads a field will see the most recently written value

Use of volatile: One common example for using volatile is to use a volatile boolean variable as a flag to terminate a thread

Difference between Volatile and Synchronize:

So where volatile only synchronizes the value of one variable between thread memories and “main” memory, synchronized synchronizes the value of all variables between thread memory and “main” memory, and locks and releases a monitor to boot. Clearly synchronized is likely to have more overhead than volatile.

A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in “main” memory.

Effectively, a variable declared volatile must have it’s data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value.

Abstract

The abstract modifier can be applied to class and methods. A class that is abstract may not be instantiate and must be extends to assess. Abstract could not be applicable on class member variables

Example: Animal class has travel method and each subclass (snake, dog, bird) has its own way of travelling. So it is not possible to provide travel () in the superclass. Instead the Animal superclass declares travel () to be abstract.

Note:

  • If a class contains one more abstract method, the compiler insists that the class must be declared abstract.
  • Abstract is opposed to final. A final class may not be subclasses; an abstract class must be subclasses.

In design, you want the base class to present only an interface for its derived classes. This means, you don’t want anyone to actually instantiate an object of the base class. You only want to up cast to it (implicit up casting, which gives you polymorphic behavior), so that its interface can be used. This is accomplished by making that class abstract using the abstract keyword.

Provide some restriction on to not to instantiate the abstract class and whoever use should implement the abstract method. And provide polymorphism.

When to use an abstract class?

Abstract classes let you define some default behavior and force subclasses to provide any specific behavior.

For example: The Spring framework’s dependency injection promotes code to an interface principle abstract implementation in collection framework.

Java Interview Reference Guide--reference,布布扣,bubuko.com

Java Interview Reference Guide--reference

上一篇:JavaScript创建类的方式


下一篇:Apache CXF 3.0: CDI 1.1 Support as Alternative to Spring--reference