ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories >> Software >> Java-Related >> Java-J2EE >> Core-Java
 
 


 

Back to Questions Page
 
Question
How two different class threads communicate with each
other?. send example code.
Rank Answer Posted By  
 Question Submitted By :: Athishankar
I also faced this Question!!   © ALL Interview .com
Answer
By Using the concept called Inter Thread Communication
 
0
Siva Kumar G
 
 
Answer
using wait() and notify() two different class threads
communicate with each other. 
For example:

public class LoggingThread extends Thread {
  private LinkedList linesToLog = new LinkedList();
  private volatile boolean terminateRequested;

  public void run() {
    try {
      while (!terminateRequested) {
        String line;
        synchronized (linesToLog) {
          while (linesToLog.isEmpty())
            linesToLog.wait();
          line = (String) linesToLog.removeFirst();
        }
        doLogLine(line);
      }
    } catch (InterruptedException ex) {
      Thread.currentThread().interrupt();
    }
  }

  private void doLogLine(String line) {
    // ... write to wherever
  }

  public void log(String line) {
    synchronized (linesToLog) {
      linesToLog.add(line);
      linesToLog.notify();
    }
  }
}
 
0
Modi[achir Communication]
 
 
Answer
Below I am showing the best example for ur Query.


class Rack
{
 int n;
 boolean avialable  = false;
 synchronized int get()
 {
  if(!avialable )
  try
  {
   wait();
  }
  catch(InterruptedException e)
  {  
   System.out.println("InterruptedException caught");
  }
  System.out.println("got:" + n);
  avialable  = false;
  notify();
  return n;
 }

 synchronized int put(int n)
 {
  if(avialable )
  try
  {
   wait();
  }
  catch(InterruptedException e)
  {  
   System.out.println("InterruptedException caught");
  }
  this.n = n;
  avialable  = true;
  System.out.println("put:" + n);
  notify();
  return n;
  }
}

class Producer implements Runnable
{
Rack k;
Producer(Rack k)
{
this.k = k;
new Thread(this, "Producer").start();
}
public void run()
{
int i = 1;
while(true)
{
k.put(i++);
}
}
}
class Consumer implements Runnable
{
Rack k;
Consumer(Rack k)
{
this.k = k;
new Thread(this, "Consumer").start();
}
public void run()
{
while(true)
{
k.get();
}
}
}
class ProducerConsumerProblem
{
 public static void main(String args[])
    {
	Rack k = new Rack();
	new Producer(k);
	new Consumer(k);
	System.out.println("press control - c to stop. ");
     }
}
 
0
Tulasi Prasad
 
 
 
Answer
The threads in java follow Mutual exclusion( synchronized)
and co-operation; 

for co-operation they need to talk to each other by messages
saying " i am waiting in the wait q anyone can acquire the
lock of the object" or" i have acquired the lock now by
notify() to the last thread who called wait() or by
notifyall() to all the threads who are in the wait q"
 
0
Puneet Khanna
 
 
Question
How can be define MARKER interfce in java
Rank Answer Posted By  
 Question Submitted By :: Neeraj_passion2001
I also faced this Question!!   © ALL Interview .com
Answer
MARKER INTERFACE or TAGGED INTERFACE ARE GIVING SOME SORT 
OF INFORMATION ABOUT THAT PARTICULAR OBJECT and it will 
giving some marker abt thae object

Generally a interface witout methods are called are 
marker/tagged interface..(but this is not corect)

Ex:
clonable,runnable,comparable,serializable


A inrefaces with some ability are ex of marker interface

1)in comparable interface,
   one method compare() will be there.......>
.....



ex:
interface Good{}....>gives some infor.abt obj called as it 
is good

interface Bad{}
 
0
Ravinder
 
 
Question
If there is no implementation of MARKER INTERFACE in java. 
Then how compiler come to know about specification.
Rank Answer Posted By  
 Question Submitted By :: Neeraj_passion2001
I also faced this Question!!   © ALL Interview .com
Answer
Implementing a java Class with the help of Marker Interface 
will inform compiler about specification.
 
3
Kamlesh Yadav
 
 
Answer
Every marker interface has some specific functionality and 
no  methods in that interface.

Ex:
Searializable  - Persistent purpose
Clonnable   - for cloning the object
SingleThreadedModel  - only one object can be create.
 
0
Vijayakumar Chinnasamy
[SOWSIA INDIA PVT LTD]
 
 
Question
Write java code to print "Hello how are you"
Thread1 should have "Hello"
Thread2 should have "how are you"
both the threads should start at the same time
Rank Answer Posted By  
 Question Submitted By :: Guest
This Interview Question Asked @   Huawei
I also faced this Question!!   © ALL Interview .com
Answer
class Callme 
{
		synchronized void call(String msg) 
		{
				System.out.print( msg 
+ " ");
			try 
			{
				Thread.sleep(1000);
			} 
			catch(InterruptedException e) 
			{
				System.out.println
("Interrupted");
			}
			
		}
}
class Caller implements Runnable 
{
		String msg;
		Callme target;
		Thread t;
		public Caller(Callme targ, String s) 
			{
				target = targ;
				msg = s;
				t = new Thread(this);
				t.start();
			}
		public void run() 
			{
				target.call(msg);
			}
}
class Synch 
{
	public static void main(String args[]) 
		{
			Callme target = new Callme();
			Caller ob1 = new Caller
(target, "Hello");
			Caller ob2 = new Caller
(target, "How are you");
// wait for threads to end
			try 
			{
				ob1.t.join();
				
				ob2.t.join();
			}
			catch(InterruptedException e) 
			{
				System.out.println
("Interrupted");
			}
		}
}


The above program will print Hello how are you with two 
diff thread
 
5
Tathagata
 
 
Answer
We can have a simple program, I have issues with staring 2
threads at the same time, you guys can have a look and let
me know how could i start two threads at the same time.


the code is as follows, we would have a separate class for
each thread, and then we would have a tester class

package threads;

public class Thread1 implements Runnable{

	public void run() {
		System.out.println("Hello how are you");
	}
}

package threads;

public class Thread2 implements Runnable{

	public void run() {
		System.out.println("Hello");
	}
}



then this would be the tester class


package threads;

public class Thread_test {

	public static void main(String[] args) {
		Thread thread1 = new Thread(new Thread1());
		Thread thread2 = new Thread(new Thread2());
		thread1.start(); /* starting the two threads at the same
time, Any suggestions */  
		thread2.start();
		
	}
}
 
0
Nil
 
 
Question
how to call One constructor from another;
Rank Answer Posted By  
 Question Submitted By :: Neeraj_passion2001
This Interview Question Asked @   Innodata-Isogen
I also faced this Question!!   © ALL Interview .com
Answer
by using super keyword
 
3
Zorif
 
 
Answer
Hi,

Using super you can invoke constructor which is in parent 
class. to invoke constructor which is in the same class we 
have to use this. for example:

public class Point {
    int m_x;
    int m_y;

    //============ Constructor
    public Point(int x, int y) {
        m_x = x;
        m_y = y;
    }

    //============ Parameterless default constructor
    public Point() {
        this(0, 0);  // Calls other constructor.
    }
    . . .
}
 
4
Ajay
[SOWSIA INDIA PVT LTD]
 
 
Answer
I think we find that anything you could have done in Java by
calling the default constructor from another constructor,
you can do easily in Curl by calling the default constructor
from a factory.
 
4
Talk2sreenivas@gmail.com
[SOWSIA INDIA PVT LTD]
 
 
Answer
. In one constructor, the first statement can be a call on
another constructor in the same class (use keyword this
instead of the class-name) or a call on a constructor of the
superclass (use keyword super instead of the class-name). In
a constructor for a subclass, the first statement must be
either this(…); or super(…); —if not, the call super(); is
automatically inserted for you.
 
4
Talk2sreenivas@gmail.com
[SOWSIA INDIA PVT LTD]
 
 
Answer
We can call the parent class constructor from the child 
class constructor.

class Parent
{
 int a;
 Parent(int x)
 {
  a=x;
 }
}
class Child extends Parent
{
 int b;
 Child(int x,int y)
 {
  super(x);
  b=y;
  System.out.println(a);
  System.out.println(b);
 }
}

class Test
{
public static void main(String args[])
{
 Child obj = new Child(1,2);	 
}
}
 
0
Tulasi Prasad
[SOWSIA INDIA PVT LTD]
 
 
Answer
This.constructername() keyword can be used with in the 
class to refer. To use super class constructer we need to 
give super.consturctername().
 
0
Sridhar
[SOWSIA INDIA PVT LTD]
 
 
Answer
constructor call from one class to another class by useing
the keyword super()(default),super(-)(parameter constructor)
 
0
Srinu
[SOWSIA INDIA PVT LTD]
 
 
Question
write java code to print second max number in the array
Rank Answer Posted By  
 Question Submitted By :: Guest
This Interview Question Asked @   Huawei
I also faced this Question!!   © ALL Interview .com
Answer
public static void main(String[] args){
    int maxNumber = 0;
    int secondMaxNumber = 0;
    if(args.length == 0){
      System.err.println("Number array is empty");
      return;
    }
    for(int i=0; i < args.length; i++){
      int currNumber = Integer.parseInt(args[i]);
      if(maxNumber < currNumber){
        secondMaxNumber = maxNumber;
        maxNumber = currNumber;
      }else if(secondMaxNumber < currNumber){
        secondMaxNumber = currNumber;
      }
    }
    System.err.println("Max. number is "+maxNumber);
    System.err.println("Second Max. is "+secondMaxNumber);
  }
}
 
4
Rajaram
 
 
Answer
public class SecondMaximumNumber{
  public static void main(String[] args){
    int maxNumber = 0;
    int secondMaxNumber = 0;
    if(args.length == 0){
      System.err.println("Number array is empty");
      return;
    }
    for(int i=0; i < args.length; i++){
      int currNumber = Integer.parseInt(args[i]);
      if(maxNumber < currNumber){
        secondMaxNumber = maxNumber;
        maxNumber = currNumber;
      }else if(secondMaxNumber < currNumber){
        secondMaxNumber = currNumber;
      }
    }
    System.err.println("Max. number is "+maxNumber);
    System.err.println("Second Max. number is
"+secondMaxNumber);
  }
}
 
0
Raja Ram
 
 
Answer
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;


public class SecondMax {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] numbers = {9,4,8,0,0,5,9,1,4,2};
		Set arrSet = new HashSet();
		for (int i=0;i<numbers.length;i++) {
			arrSet.add(numbers[i]);
		}
		ArrayList s = new ArrayList(arrSet);
		Collections.sort(s);

		System.out.println("Second element : " + 
s.get(s.size()-2));
	}
}
 
0
Himesh Mistry
 
 
Answer
Himesh Mistry -best solution
 
0
Geetha
 
 
Answer
Ranjaram is correct.I modified and testes it.
public class test123 {
	public static void main(String[] args){
	    int maxNumber = 0;
	    int secondMaxNumber = 0;
	  
	    int[] anArray;           
	    anArray =new int [10];
	    anArray[0] = 100; 
        anArray[1] = 200; 
        anArray[2] = 300; 
        anArray[3] = 400;
        anArray[4] = 500;

	    if(anArray.length == 0){
	      System.err.println("Number array is empty");
	      return;
	    }
	   for(int i=0; i < anArray.length; i++){
	     int currNumber = anArray[i];
	      if(maxNumber < currNumber){
	        secondMaxNumber = maxNumber;
	        maxNumber = currNumber;
	      }else if(secondMaxNumber < currNumber){
	        secondMaxNumber = currNumber;
	      }
	    }
	    System.err.println("Max. number is "+maxNumber);
	    System.err.println("Second Max. 
is "+secondMaxNumber);
	  }
	}
 
0
Sujeev
 
 
Answer
I think, the except for Himesh's program, all other ones 
doesnt handle duplicate values ...
I mean if the numbers are :- 9 9 8 8 7 7 7 1 3 2
the answer should be 8 ideally.
But I doubt whether thats really happening in any other 
programs, except for Himesh's.
Do let me know, if I am wrong. Thanks.
 
0
Sunny
 
 
Answer
hey using collections solving this type of problem becomes
easy but whenever u go for interview most of the time they
ask you to write program without using collection API thts
why i think rajaram's code is correct
 
0
Hitman
 
 
Answer
If we give negative values, what would be the Rajaraman 
code begaves????
 
0
Rajesh S
 
 
Answer
This is I tried.
public class SecondLargestNumber {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] num = new int[]{-101,-105,-2,-7,-22,-
104,-8,-10,-100,-102,-102};
		int big=-1;
		int secbig=-1;
		if(num.length == 1) {
			big = num[0];
			secbig = num[0];
		} else {
			if(num[0] > num[1]) {
				big = num[0];
				secbig = num[1];
			} else {
				big = num[1];
				secbig = num[0];
			}
		} 
		if(num.length > 2){
			for(int i=2;i<num.length;++i){
				if(num[i] > secbig && num
[i] <big) {
					secbig = num[i];
				}
				if(num[i]>=big){
					secbig = big;
					big = num[i];
				}
			}
		}
		System.out.println(big);
		System.out.println(secbig);
	}
}

Please check is it working and let me know. For removing 
duplicates, we will have one more method to remove the 
duplicate elements.
 
0
Rajesh S
 
 
Question
wHAT IS DEFAULT SPECIFIER IN JAVA
wHAT IS DEFAULT CONSTRUCTOR IN JAVA
wHAT IS DEFAULT METHOD IN JAVA
Rank Answer Posted By  
 Question Submitted By :: Anilsamal
This Interview Question Asked @   IBM
I also faced this Question!!   © ALL Interview .com
Answer
obj

default constructor

main
 
0
Avnish Kumar Dubey
 
 
Answer
default specifier specifies access level of class
eg:-
class A{
}
this is default
default constructor is constructor with same name as of 
class and no arguments
eg: class A{
A()
}
there is no default method in java
 
0
Rohit
[ICFAI]
 
 
Answer
default specifier is private

default constructor is className(){}

there is no default method in java
 
0
Guest
[ICFAI]
 
 
Answer
default specifier is-default
default constructor
no default method
 
0
Renu
[ICFAI]
 
 
Answer
DEFAULT SPECIFIER IN JAVA:
There are 4 types of accessibility specifier in java, they
are private,public,protected and default.
There is no key word in java to specify default specifier 
When we do not specify any specifier which means default
specifier,

For Example :
 public String getName(){...} 
// here accessibility specifier  is public

 String getName(){...} 
// here accessibility specifier is defalut, whose scope    
is package-scope means this method is not accessible outside
the current package

DEFAULT CONSTRUCTOR IN JAVA:
Default constructor is the implicit constructor provided by
the compiler when we do not specify any constructor in our class
Default constructor looks like..
---------------------------------
<Access Modifier> <class_name>(){
super();// implicit super call
}

I have not heard about any default method in java..As far my
knowledge there is no default method till JAVA platform 1.4....
 
0
Debasankar
[ICFAI]
 
 
Answer
Default Specifier is : friendly. but there is no keyword 
provided by java

Default Construtor is : the constructor of top level super 
class of each java class that is "Object" class.

Default Method is : main method that would called by 
default through the java interpretor.
 
0
Devesh Dashora
[ICFAI]
 
 
Answer
DEFAULT SPECIFIER IN JAVA IS CALLED AS PACKAGE ACCESS

DEFAULT SPECIFIER IN JAVA IS CALLED AS CLASS NAME() WHICH 
WAS PROVIDED BY COMPILER DURING COMPILATION TIME

THERE IS NO DEFAULT METHOD IN JAVA
 
0
Sanjay
[ICFAI]
 
 
Answer
in java default specifier is public but within the 
samepackage.due to this property of public within same 
package we have to define public before the main method 
bcoz main has to be taken from jdk which is like the 
different package. So if we want to anythig accessing to 
the diffrent package we have to declare that with the 
public keyword explicitly. 

default constructor in java is same name of the class whose 
object has to be declared.
example
class v
{
public static void main(String vip[])
{
System.out.println("i am vip");
}
in this default constructor is v();

as much as i know ,there is no concept of default method
 
0
Vipin Dhiman
[ICFAI]
 
 
Answer
default specifier in java is DEFAULT
which can only be visible within the same package.
You can't access any default member of a class directly 
from outside the package ,  you can access those member 
through another method of that class.

default CONSTRUCTOR in java is the constructor which has 
the same name of the class and has no arguments. if you 
define at least one constructor in a class JVM will not put 
the default constructor in the class like..

public className(){super();}

if there is no constructor in the class JVM will put the 
default constructor in the class like..

public className(){super();}

since its not visible. but implicitly defined by compiler..


there is no concept of DEFAULT method in java spec.
 
5
Safikur
[ICFAI]
 
 
Answer
DEFAULT SPECIFIER IN JAVA:
There are 4 types of accessibility specifier in java, they
are private,public,protected and default.
There is no key word in java to specify default specifier 
When we do not specify any specifier which means default
specifier,the default specifier  access the with in package
only. 
EX:- public void setName(String name)
     {
       this.name=name;
     }
 The above method access any class or any package
   void setName(String name)
     {
       this.name=name;
     }
The above method can access with in package.

DEFAULT CONSTRUCTOR IN JAVA:-
THE default constructor in java is System default constructor.

EX:-
public class A
{
   String name;
    public void setName(String name);
     {
       this.name=name;
     }
}
The above program their is no constructor .then that time
java compiler created default constructor.

Note: we can interested see default constructor frist we
compile java program(javac A.java) after javap A.Then that
time u watch system default constructor.

DEFAULT METHOD IN JAVA:--
The dafault method in java is super().
EX:-
public class A
{
  A()
  {
   System.out.println("IAM CONSTRUCTOR FROM CLASS A ");
  }
}
public class B extends A
{
B()
{
System.out.println("IAM CONSTRUCTOR FROM CLASS B");
}
public static void main(String k[])
{
 B b=new B();
}
}
output:-
IAM CONSTRUCTOR FROM CLASS A 
IAM CONSTRUCTOR FROM CLASS B
hence super() method is default method
 
0
Srinu
[ICFAI]
 
 
Question
Can we declare static variables in JSP page.
Rank Answer Posted By  
 Question Submitted By :: Neeraj_passion2001
This Interview Question Asked @   TCS
I also faced this Question!!   © ALL Interview .com
Answer
yes 

 we should do in jsp declarative tag only.if we specify 
variables in declaration tag, it belongs to class.
<%! static int i=0; %>

and u can not define static variable in scriptlet(<% %>). 
as we know that we can not declare a static variable in a 
method.
 
5
Rajender
 
 
Answer
In Declaration tag (<%!  -- %>), you can able declare the
static variable,class, define methods.
 
5
Vijayakumar Chinnasamy
[SOWSIA INDIA PVT LTD]
 
 
Answer
yes we declare the varible as static in a jsp.we declare the
static varible in the declaration tag.
ex:-
<%! static int a=10;%>
 
0
Srinu
[SOWSIA INDIA PVT LTD]
 
 
Question
What are Advatages of Overloading and Overridding.
Rank Answer Posted By  
 Question Submitted By :: Neeraj_passion2001
This Interview Question Asked @   TCS
I also faced this Question!!   © ALL Interview .com
Answer
Polymorphism
 
0
Avnish Kumar Dubey
 
 
Answer
Overloading means same functions but different arguments..
so with overloading we can take new action with diff 
parameters..


Overriding means same methods with same argument and same 
prototype...
so with overriding we can create new definatin in subclass 
with same method name and prototype..
 
0
Pooja Srivastava
[SOWSIA INDIA PVT LTD]
 
 
Answer
No look at the Question he had ask u Advantages for  
Overloading & Overriding ..we can say in Overriding & 
OverLoading COMPLEXITY Factor Had Been Reduced a lot And 
the performance is Increased & lot of memory space has been 
SAVED..We are not calling the methods every thing we invoke 
on it.. we are using the HANDLE Of Superclass to pass the 
agruments....Thats why ITs Advantages
 
0
Ershad Md Sk
[SOWSIA INDIA PVT LTD]
 
 
Question
In C we use only compiler. Why java uses both compiler and 
interpreter? What is its significance?
Rank Answer Posted By  
 Question Submitted By :: Tejasrajeevnaik
I also faced this Question!!   © ALL Interview .com
Answer
because in java, code is 1st converted into bytecode by the 
compiler nd then it is converted into machine code by the 
interpreter.
 
0
Nidhi,himani
 
 
Answer
in java  code is complied by the complier at that time code 
is convered into byte code after that JVM can convert the 
byte code instructions into Machine code instructions.
 
0
Dhana
 
 
Answer
Java is Platform Independent language it uses Virtual 
System (JVM) in an existing operating system to run 
programs.
The JVM itself is not Platform independent it is platform 
dependent. but its functionality is same for all verison of 
JVM for different OSs.

The Source Code of a Java Program is compiled to avail the 
advantages of compiler like fast development, i mean all 
the errors are checked and displayed by the compiler at once
(in 99.99% cases) to make the code error free.

An error free Java Code is converted into BYTECODE ; 
directly understood by the JVM(for all OSs).

Now consider we compiled a.java >> a.class on linux, the 
a.class is portable and runnable on other OS like Windows 
because class files are fun by JVM but not by the OSs that 
why we can say
Java is platform Independent.
 
5
Kuldeep Sharma
 
 
Answer
In C Lang  Our program is directly Converted into 
Executable code which is a Not portable..& it is Machine 
Specific..in JAVA first .class file is Converted into .java 
File And  the Interpreter wil generate a intermediate Code 
that is BYTE CODE..Which is Machine Independent...it has 0% 
protability Issue............
 
0
Ershad Md Sk
 
 
Answer
If a source code is written in C,then after compilation platform dependent native code is generated which is specific to the platform and whenever if you want to execute the same source code into different platform you have to recompile the program which is wastage of time.
srcprogram-->compile-->platform1---->platform native code
srcprogram-->compile-->platform2-->platform2 native code
               But coming to Java,when you compile your source code ,an intermediate code is generated(.class file)which is common to all the platforms and you can execute the .class file on any platform with the help of jvm to generate native code of the specific platform.

src prg(.javafile)-->compile(on any platform Xplatform)-->bytecode(.classfile)-->jvm(execute on)-->platform(p1 or p2...or pn)(to get native code)
that is what java's passion compile once run anywhere
 
0
Rinky
 
 
Question
How to validate the request (Eg:user name and password) in
session(http session)? not in LDAP server ?
Rank Answer Posted By  
 Question Submitted By :: Katha.net
This Interview Question Asked @   Saksoft
I also faced this Question!!   © ALL Interview .com
Answer
You can Use a HashMap to put all user objects into into it
and put this hashmap into session and whenever required get 
HashMap from session and get object with key as user name 
and then validate it against login 
details...hasanulhuzaibi@gmail.com
 
0
Hasan Ul Huzaibi
 
 
Question
why marker interfaces are there in java
Rank Answer Posted By  
 Question Submitted By :: Sudha
This Interview Question Asked @   Digital-Group
I also faced this Question!!   © ALL Interview .com
Answer
When a class implements a marker interface, then the object
of that class is given with a special behavior. For example
if a class implements remote interface(available in
java.rmi.* package) object of that class acts as a Remote
Object. For this reason java provides Marker Interface.
 
0
Ravinder
 
 
Answer
Marker interfaces can create an environment.

Marker interfaces are used to specify that a class belongs 
to a logical family or grouping - as quoted above, 
Cloneable is used to indicate that a particular class can 
be cloned, or Serializable is used to indicate that a 
particular class can be serialized.
 
0
Siva Kumar G
 
 
Answer
when class implements marker interface our class object
getting special behavoiur.
EX:-
suppose our class implements java.lang.Serializible 
interface our class object getting special behaviour(our
class object converting java support format object to
network support)

Another Example our class implements java.rmi.Remote
interface our class object acts as Remote object.
 
0
Srinu
 
 
Question
If all the methods in abstract class are declared as 
abstract then what is difference between abstract class and 
in interface?
Rank Answer Posted By  
 Question Submitted By :: Namita
This Interview Question Asked @   Synechron , Infosys
I also faced this Question!!   © ALL Interview .com
Answer
In abstract class concrete methods are also there. concrete 
methods are the one which has implementation. so in future 
if any programmer wants to give concrete methods can use 
abstract class not interface. 
In abstract class for methods and variables 
public,private,protected modifier can be use. but in 
interface public and abstract are permitted.
 
0
Namita
 
 
Answer
if all the methods are abstract in abstract class,then the 
only difference left is,,,,

interface can have static and final variables with public 
and abstract access modifier only ,,,while abstract class 
can have any type of variable with any type of access 
modifiers
 
0
Ravi
 
 
Answer
Multiple inheritance is possible only through interface,not 
with abstract class. So if u have an interface we can go 
implementing multiple interfaces rather than extending a 
single abstract class.
 
0
Ravi Sv
 
 
Answer
Methods in the abstract class can be accessed only through 
a class hierarchy i.e through inheritance.

But methods in the interface can be accessed anywhere i.e 
across the class hierarchy.(without inheritance).
 
0
Ashok Kumar
 
 
Answer
You can inherit the abstract class and use only those 
methids which you want.

But in Interface you have you implement every method.
 
3
Prasad Bhagwat
 
 
Answer
i am not agree with the answer provided by prasad bhagwat 
as already mention inthe question that all the method of 
abtract class are declared as abstract and as per rules of 
abstract class impletation you need to provide 
implemenation of all abstract methods present in abstract 
class unitl & unlesss you declared child class also as an 
abstract class.
 
3
Sumit Jain
 
 
Question
Why cant we define System.out.println() inside a class 
directly?
Rank Answer Posted By  
 Question Submitted By :: Plavani
I also faced this Question!!   © ALL Interview .com
Answer
We can't because The method signature matters when you are
declaring the method. we can't give a name with
System.out.println, but we can define println()
 
0
Madhu
 
 
Answer
we can override only class methods and we can't override the
output Stream methods.
 
0
Madhu
 
 
Answer
because println method is not abstract method it is method 
of output stream class so we cant override.and it is just 
overloded method.
 
0
Shahid
 
 
Answer
we Directly write this line i.e System.out.println() 
directly inside a class because println method is  static 
method in output stream class which are sub classes of 
system class.
so every time this method is overridden in our program....
so static methods doesnt require a creation of object.....
 
0
Venkatachalapathy
 
 
Question
public class Garbage
{
    int a=0;
    public void add()
    {
	int c=10+20;
	System.out.println(c);
	System.out.println(a);
    }
    public static void main(String args[])
    {
	Garbage obj=new Garbage();
	System.gc();
	System.out.println("Garbage Collected");
	obj.add();
    }
}
Above is a code in java used for garbage collection. object 
obj has been created for the class Garbage and system.gc 
method is called. Then using that object add method is 
called.System.gc method if called the obj should be garbage 
collected?
Rank Answer Posted By  
 Question Submitted By :: Maverickhari
I also faced this Question!!   © ALL Interview .com
Answer
First We cannot force the garbage collection to garbage the 
object. Garbage collection can never be forced.

So by calling System.gc() will not ensure you that the 
object will be garbage collected.
 
0
Namita
 
 
Answer
say me when this eill actually happen.
 
0
Maverickhari
 
 
Answer
Maverickhari,

Garbage collector is system created thread which runs 
automatically. 

We are not sure when the garbage collection is going to 
happen. this totally depends upon the JVM. Like connection 
pool all the the objects are created in pool JVM will check 
if there is no free memory in pool then it searches for the 
objects which are no longer in use and will garbage collect 
that and allocate to some other object. 

hope this will clear
 
0
Namita
 
 
Answer
Garbage Collector is called by the JVM. and it will collect
the objects which has no reference, in the above case obj
has reachability,so JVM won't force the garbage collector to
collect the obj object.
 
0
Madhu
 
 
Answer
Thanks Namita
 
0
Maverickhari
 
 
Answer
Java provides us one method System.gc()to call garbage
collection forcefully.But by calling System.gc() will not
ensure you that the object will be garbage collected.
 
0
Bhudeep
 
 
 
Back to Questions Page
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com