This document compares and contrasts abstract classes and interfaces in Java. It defines abstract classes as partially abstract reusable code that can contain both abstract and concrete methods, while interfaces are fully abstract reusable contracts that can only contain abstract methods. The document provides examples of abstract class and interface syntax and usage, highlighting that abstract classes can have constructors and non-public members while interfaces cannot. It concludes that abstract classes are not completely abstract but interfaces are.
4. output:-
Second Show call
Syntax and use of abstract classes
package ppt;
abstract class First
{
abstract void show(){ };
}
class Second extends First
{
void show()
{
System.out.println(“Second Show call");
}
}
public class Abstractclass {
public static void main(String[]args) {
Second y=new Second();
y.show();
}
}
5. package ppt;
interface X //interface
{
void show(); //abstract method in interface
}
class Y implements X /*implementing interface X
{ using class Y*/
public void show() {
System.out.println("show call X");
}
}
public class Testinterface {
public static void main(String[] args) {
X obj =new Y(); // creating object of Y having type
obj.show(); X
}
}
Syntax
and use
of
interface
Output:--
show call X
6. Abstract class
Abstract class can extend
only one class or one abstract
class at a time
Abstract class can extend
from a class or from an
abstract class
Abstract class can have
both abstract and concrete
methods
A class can extend only one
abstract class
In abstract class keyword
‘abstract’ is mandatory to
declare a method as an
abstract
Interface
Interface can extend any
number of interfaces at a time
Interface can extend only
from an interface
Interface can have only
abstract methods
A class can implement any
number of interfaces
In an interface keyword
‘abstract’ is optional to
declare a method as an
abstract
7. Abstract Class
Abstract class can
have protected , public and
private abstract methods.
Abstract class can have static,
final or static final variable with
any access specifier.
Constructor is there in abstract
classes.
Constructor of abstract class not
called directly(can be called using
super)
Interface
Interface can have only public
abstract methods i.e. By default.
Interface can have only static final
(constant) variable i.e. By default.
Constructor is not there in
interface.
No constructor no object.
8. Abstract Classes are not 100% abstract whereas
interface is 100% abstract
package ppt;
abstract class First
{
abstract void show(); //abstract
method
void print(){
System.out.println(“Print call");
} //concrete method
}
}
class Second extends First
{
void show()
{
System.out.println(“Second
Show call");
}
}
public class Abstractclass {
public static void main(String[]args) {
Second y=new Second();
y.show();
y.print();
}
}
Output:-
Second Show call
Print call
9. Abstract Classes are not 100% abstract whereas
interface is 100% abstract
package ppt;
interface X //interface
{
void show(); /*abstract method in
void print(); interface*/
}
class Y implements X
/*implementing interface X
using class Y*/
{
public void show() {
System.out.println("show call Y");
}
public void print() {
System.out.println(“print call Y");
}
}
public class Testinterface {
public static void main(String[] args) {
X obj =new Y(); /* creating object of Y
obj.show(); having type X*/
obj.print();
}
}
Output:--
show call Y
print call Y