Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

How do you sort a list in java?

205


what is meant by Byte code concept in Java?

245


Why java is not 100% object-oriented?

211


Can constructor return value?

185


What is the platform?

223






What is polymorphism in java? What are the kinds of polymorphism?

234


How do you sort a string in java?

212


How many JVMs can run on a single machine and what is the meaning of Just-In-Time (JIT) compiler?

334


Does sprintf allocate memory?

200


What is ++ a in java?

196


What are advantages and disadvantages of OOPs?

256


In a container there are 5 components. I want to display all the component names, how will you do that?

1194


why java does not support unsigned keyword?

3629


What are exceptions

264


Why string is popular hashmap key in java?

257