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                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
what is the use of abstract class and interface with example?
 Question Submitted By :: Gopalraop
I also faced this Question!!     Rank Answer Posted By  
 
  Re: what is the use of abstract class and interface with example?
Answer
# 1
Abstract classes cannot be instantiated; they must be 
subclassed, and actual implementations must be provided for 
the abstract methods. Any implementation specified can, of 
course, be overridden by additional subclasses. An object 
must have an implementation for all of its methods. You 
need to create a subclass that provides an implementation 
for the abstract method.
for exapmple:
abstract class Shape {

	public String color;
	public Shape() {
	}
	public void setColor(String c) {
		color = c;
	}
	public String getColor() {
		return color;
	}
	abstract public double area();
}
public class Point extends Shape {

	static int x, y;
	public Point() {
		x = 0;
		y = 0;
	}
	public double area() {
		return 0;
	}
	public double perimeter() {
		return 0;
	}
	public static void print() {
		System.out.println("point: " + x + "," + y);
	}
	public static void main(String args[]) {
		Point p = new Point();
		p.print();
	}
}
Interface:
In Java, this multiple inheritance problem is solved with a 
powerful construct called interfaces. Interface can be used 
to define a generic template and then one or more abstract 
classes to define partial implementations of the interface. 
Interfaces just specify the method declaration (implicitly 
public and abstract) and can only contain fields (which are 
implicitly public static final). Interface definition 
begins with a keyword interface. An interface like that of 
an abstract class cannot be instantiated
for example:
interface Shape {

	public double area();
	public double volume();
}
public class Point implements Shape {

	static int x, y;
	public Point() {
		x = 0;
		y = 0;
	}
	public double area() {
		return 0;
	}
	public double volume() {
		return 0;
	}
	public static void print() {
		System.out.println("point: " + x + "," + y);
	}
	public static void main(String args[]) {
		Point p = new Point();
		p.print();
	}
}
 
Is This Answer Correct ?    8 Yes 1 No
Sujanya
 
  Re: what is the use of abstract class and interface with example?
Answer
# 2
Abstract classes cannot be instantiated; they must be 
subclassed, and actual implementations must be provided for 
the abstract methods. Any implementation specified can, of 
course, be overridden by additional subclasses. An object 
must have an implementation for all of its methods. You 
need to create a subclass that provides an implementation 
for the abstract method.
for exapmple:
abstract class Shape {

	public String color;
	public Shape() {
	}
	public void setColor(String c) {
		color = c;
	}
	public String getColor() {
		return color;
	}
	abstract public double area();
}
public class Point extends Shape {

	static int x, y;
	public Point() {
		x = 0;
		y = 0;
	}
	public double area() {
		return 0;
	}
	public double perimeter() {
		return 0;
	}
	public static void print() {
		System.out.println("point: " + x + "," + y);
	}
	public static void main(String args[]) {
		Point p = new Point();
		p.print();
	}
}
Interface:
In Java, this multiple inheritance problem is solved with a 
powerful construct called interfaces. Interface can be used 
to define a generic template and then one or more abstract 
classes to define partial implementations of the interface. 
Interfaces just specify the method declaration (implicitly 
public and abstract) and can only contain fields (which are 
implicitly public static final). Interface definition 
begins with a keyword interface. An interface like that of 
an abstract class cannot be instantiated
for example:
interface Shape {

	public double area();
	public double volume();
}
public class Point implements Shape {

	static int x, y;
	public Point() {
		x = 0;
		y = 0;
	}
	public double area() {
		return 0;
	}
	public double volume() {
		return 0;
	}
	public static void print() {
		System.out.println("point: " + x + "," + y);
	}
	public static void main(String args[]) {
		Point p = new Point();
		p.print();
	}
}
 
Is This Answer Correct ?    3 Yes 1 No
Ravi
 
 
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
What is Remote Interface ? CTS4
if two references are having same hash codes,is that means those are refering to same object? CTS4
What is the difference between the >> and >>> operators?  1
What is the default initialized value of a boolean type variable?  3
Can I create any Marker Interface? If yes then how can I use it???  1
can we create object for static class in java Marlabs5
What services that container provides?  1
what is the difference between pagecontext and servletcontext? JBA-Infotech4
why there are multiple catches for a try block.don't tell me that there can be multiple exception of a code segment that's why.tell me the real fact behind this.  4
what is difference between abstract and interface? can i give real time example for the two topics?  6
what is difference between method overloading & method overridding with example? IBM2
Keywords in Exceptions?  1
What is casting ?  2
how cani read a command line argument?(usingfile object).  3
what are class,constructor and primitive data types?  2
How to avoid the runtime exception ?  2
why java does not support mulitple inheritance directly? TCS2
wht is customised exception? Prudential1
how to handle http request in struts Polaris2
1).Is Object class abstract or not? 2).Is main method(public static void main(String args[])low priority thread or high priority thread? TCS2
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
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