what is the use of abstract class and interface with example?

Answers were Sorted based on User's Feedback



what is the use of abstract class and interface with example?..

Answer / sujanya

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 ?    19 Yes 5 No

what is the use of abstract class and interface with example?..

Answer / ravi

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 ?    10 Yes 7 No

Post New Answer

More Core Java Interview Questions

What does ide stand for?

0 Answers  


Explain about method local inner classes or local inner classes in java?

0 Answers  


Is string a keyword in java?

1 Answers  


Define packages in java?

0 Answers  


How do generics work?

0 Answers  






Is there any sort function in java?

0 Answers  


Why to use nested classes in java?

0 Answers  


why pointer is not used in java?

3 Answers  


What is difference between char array and string?

0 Answers  


Is 0 true or false in java?

0 Answers  


what is the main class of all the classes

5 Answers   Photon,


What is java basic concept?

0 Answers  


Categories