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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to print nodes of a Binary tree?

1741


What is bytecode verifier?

489


When is finally block not called?

570


What is the difference between normal report & matrix report?

553


FOR EXAMPLE WE R HAVING TWO LIST ELEMENTS ..BOTH LISTS CONTAINS ID,NAME,PLACE ..I NEED TO COMPARE BOTH IDS IN TWO LISTS,IF ID'S R SAME MEANS WE HAVE ADD THE DETAILS(LIKE NAME,PLACE) TO MAP...HOW IS POSSIBLE ?CAN ANY ONE SUGGEST?

2616






What Is Composition?

567


What is a method header?

536


How we can run a jar file through command prompt in java?

508


What is string [] java?

512


What is java thread dump, how can we get java thread dump of a program?

557


What is the static keyword?

575


In a class implementing an interface, can we change the value of any variable defined in the interface?

541


Difference between stack and queue?

578


In multi-threading how can we ensure that a resource isn't used by multiple threads simultaneously?

820


What is java life cycle?

523