[go: up one dir, main page]

0% found this document useful (0 votes)
5 views2 pages

Interface AbstractClass

The document explains the concepts of interfaces and abstract classes in Java. Interfaces define common behavior for different classes and can only declare abstract methods and constants, while abstract classes represent concepts that cannot be instantiated and must have their abstract methods overridden. It highlights the use of the 'implements' keyword for classes to adopt interfaces and the 'abstract' keyword for defining abstract classes and methods.

Uploaded by

sakshambhalap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Interface AbstractClass

The document explains the concepts of interfaces and abstract classes in Java. Interfaces define common behavior for different classes and can only declare abstract methods and constants, while abstract classes represent concepts that cannot be instantiated and must have their abstract methods overridden. It highlights the use of the 'implements' keyword for classes to adopt interfaces and the 'abstract' keyword for defining abstract classes and methods.

Uploaded by

sakshambhalap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Interface

The aim of interface is to dictate common behavior among objects from diverse classes.

Java does not allow multiple inheritance. To tie elements of different classes together Java uses an interface.
Interface body can only declare 2 things :

Abstract methods (empty) , must be public

Constants (public static final)

Eg :

interface employee

public static final double sal;


public void calsal();

Class can implement an interface using the keyword ‘implements’.

A class has to implement ALL the methods declared in the interface

Eg :

class manager implements employee


{

public void calsal()

{ sal = 50000; }

An interface can extend other interface.

A class can extend only one class but can implement as many interfaces.

ABSTRACT CLASSES

An abstract class is the one that simply represents a concept and whose objects can’t be created.

It is created through the use of keyword abstract

Abstract methods are methods with no method statements.

Keyword ‘abstract’ is used

Should be overridden

abstract class Shape


{ String name;

double area;

public abstract void disp(); // as disp() is abstract : must be overridden in derived class, // will not
have a body

}
class Circle extends Shape
{ double radius;

double calcArea()

{ radius=5.2;

return 3.15*radius*radius;

public void disp()// if not defined, will throw an error as abstract methods must be overridden

{ System.out.println("Radius = " + radius);


System.out.println("Area = " + calcArea());

} }

class Rectangle extends Shape

{ double length,breadth;

double calcArea()

{ length=9;

breadth=3;

return length*breadth;
}

public void disp()// if not defined, will throw an error as abstract methods must be overridden

{ System.out.println("Length = " + length);

System.out.println("Breadth = " + breadth);

System.out.println("Area = " + calcArea());

} }

public class abstractclass1


{ public void main()

{ //Shape s=new Shape(); // will give error, Shape is abstract

Circle c = new Circle();

c.calcArea();

c.disp();

Rectangle r = new Rectangle();

r.calcArea();
r.disp();

} }

You might also like