Sunday 27 November 2016

5 Difference Between "Implements Runnable" And "Extends Thread" In Java


 Difference between "implements Runnable" and "extends Thread" in Java

1. Inheritance Option:   The limitation with "extends Thread" approach is that if you extend Thread,  you can not extend anything else . Java does not support multiple inheritance.  In reality , you do not need Thread class behavior , because in order to use a thread you need to instantiate one anyway.
On the other hand,
Implementing the Runnable interface gives you the choice to extend any class you like , but still define behavior that will be run by separate thread.

2. Reusability :  In "implements Runnable" , we are creating a different Runnable class for a specific behavior  job (if the work you want to be done is job). It gives us the freedom to reuse the specific
behavior job whenever required.
"extends Thread"  contains both thread and job specific behavior code. Hence once thread completes execution , it can not be restart again.   


3Object Oriented Design:  Implementing Runnable should be preferred . It does not specializing or modifying the thread behavior . You are giving thread something to run. We conclude that Composition is the better way. Composition means two objects A and B satisfies has-a  relationship.
"extends Thread"  is not a good Object Oriented practice.

4. Loosely-coupled : "implements Runnable" makes the code loosely-coupled and easier to read .
Because the code is split into two classes . Thread class for the thread specific code and your Runnable implementation class for your job that should be run by a thread code.
"extends Thread"  makes the code tightly coupled . Single class contains the thread code as well as the job that needs to be done by the thread.

5. Functions overhead :  "extends Thread"  means inheriting all the functions of the Thread class which we may do not need .  job can be done easily by Runnable without the Thread class functions overhead.


Source :- http://javahungry.blogspot.com/2015/05/implements-runnable-vs-extends-thread-in-java-example.html

Monday 18 July 2016

State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.

public : Public class is visible in other packages, field is visible everywhere (class must be public too)
private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.
protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.
default :What you get by default ie, without any access modifier (ie, public private or protected).It means that it is visible to all within a particular package.

Thursday 7 July 2016

What is the purpose of finalization?

The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.

Can an anonymous class be declared as implementing an interface and extending a class?

An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

Monday 4 July 2016

Java Interview Questions

You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?

ArrayList

How can you minimize the need of garbage collection and make the memory use more effective?

Use object pooling and weak object references.

There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it?

If these classes are threads I’d consider notify() or notifyAll(). For regular classes you can use the Observer interface.

What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?

You do not need to specify any access level, and Java will use a default package access level.

If Runnable interface is better than Thread class, than why we are using Thread class? What is the need for Thread class?

What are Encapsulation, Inheritance and Polymorphism?

Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse.
Inheritance is the process by which one object acquires the properties of another object.
Polymorphism is the feature that allows one interface to be used for general class actions.

What is the difference between Assignment and Initialization?

Assignment can be done as many times as desired whereas initialization can be done only once.

What are Class, Constructor and Primitive data types?

Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform.
Constructor is a special kind of method that determines how an object is initialized when created.
Primitive data types are 8 types and they are: byte, short, int, long, float, double, boolean, char.

What is the difference between an argument and a parameter?

While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.

What is final, finalize() and finally?

final : final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods. A final method can’t be overridden. A final variable can’t change from its initialized value.
finalize() : finalize() method is used just before an object is destroyed and can be called just prior to garbage collection.
finally : finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block.
The finally block will execute whether or not an exception is thrown.

What is the difference between this() and super()?

this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor.

What is the difference between superclass and subclass?

A super class is a class that is inherited whereas sub class is a class that does the inheriting.

What are inner class and anonymous class?

Inner class : classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private.
Anonymous class : Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.

What is the difference between Integer and int?

a) Integer is a class defined in the java. lang package, whereas int is a primitive data type defined in the Java language itself. Java does not automatically convert from one to the other.
b) Integer can be used as an argument for a method that requires an object, whereas int can be used for calculations.

What is a cloneable interface and how many methods does it contain?

It is not having any method because it is a TAGGED or MARKER interface.

What is the difference between abstract class and interface?

a) All the methods declared inside an interface are abstract whereas abstract class must have at least one abstract method and others may be concrete or abstract.

b) In abstract class, key word abstract must be used for the methods whereas interface we need not use that keyword for the methods.

c) Abstract class must have subclasses whereas interface can’t have subclasses.

What is the difference between exception and error?

What is the difference between process and thread?

Process is a program in execution whereas thread is a separate path of execution in a program.

What is synchronization?

Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time.

Are there any global variables in Java, which can be accessed by other part of your program?


No, it is not the main method in which you define variables. Global variables is not possible because concept of encapsulation is eliminated here.

If you’re overriding the method equals() of an object, which other method you might also consider?

hashCode()

You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?

Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.

What’s the difference between a queue and a stack?

Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule.

How can a subclass call a method or a constructor defined in a superclass?

Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass’s constructor.

What’s the main difference between a Vector and an ArrayList

Java Vector class is internally synchronized and ArrayList is not.

Can an inner class declared inside of a method access local variables of this method?

It’s possible if these variables are final.

  1. What can go wrong if you replace && with & in the following code:
  2. String a=null;
    if (a!=null && a.length()>10)
    {...}
  3. A single ampersand here would lead to a NullPointerException.


Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?

A. Yes, it does. The FileNoFoundException is inherited from the IOException. Exception’s subclasses have to be caught first.

Can you call one constructor from another if a class has multiple constructors

Yes. Use this() syntax.

What’s the difference between the methods sleep() and wait() ?

The code sleep(1000); puts thread aside for exactly one second.
The code wait(1000), causes a wait of up to one second.
A thread could stop waiting earlier if it receives the notify() or notifyAll() call.

The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

How can you force garbage collection?

You can’t force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.