Inheritance Notes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Interfaces in java

It is similar to class.

It is a collection of abstract methods.

The interface in java is a mechanism to achieve full abstraction

. There can be only abstract methods in the java interface not method body.

It is used to achieve full abstraction and multiple inheritance in Java.

It cannot be instantiated just like abstract class.

Rules for using interfaces

1. Methods inside Interface must not be static, final,


2. All variables declared inside interface are implicitly public static
final variables
3. All methods declared inside Java Interfaces are implicitly public
and abstract, even if you don't use public or abstract keyword.
4. Interface can extend one or more other interface.
5. Though classes in java doesn't support multiple inheritance, but
a class can implement more than one interface.

The java compiler adds public and abstract keywords before the
interface method and public, static and final keywords before data
members

Abstract class Interface

1) Abstract class can have abstract and Interface can have only
non-abstract methods. abstract methods.

2) Abstract class doesn't support multiple Interface supports multiple


inheritance. inheritance.

3) Abstract class can have final, non-final, Interface has only static and final
static and non-static variables. variables.

4) Abstract class can have static methods, Interface can't have static methods,
main method and constructor. main method or constructor.
5) Abstract class can provide the Interface can't provide the
implementation of interface. implementation of abstract class.

6) The abstract keyword is used to declare The interface keyword is used to


abstract class. declare interface.

Difference between abstract class and concrete class in java

A partial implemented class is called an abstract class , Where as fully


implemented class is commonly known as concrete or normal class.

Abstract class:

It is must to declare a class with an abstract access modifier .

May or may not contain abstract methods.

It is not possible to instantiate a abstract class .

Variables are not final by default.We can able to reassign values.

The abstract methods should implement in the derived classes. If not , the
derived class also become

an abstract class.

Interface implementation is possible.

Concrete class:

Should not declare a concrete class with an abstract access modifier.

Should not contain abstract methods.

Instantiation is possible for a concrete class.

Variables are not final by default.

There is no abstract methods in any level to implement .


Interface implementation is possible.

Abstract class Interface

1) Abstract class can have abstract and Interface can have only
non-abstract methods. abstract methods.

2) Abstract class doesn't support multiple Interface supports multiple


inheritance. inheritance.

3) Abstract class can have final, non-final, Interface has only static and final
static and non-static variables. variables.

4) Abstract class can have static methods, Interface can't have static methods,
main method and constructor. main method or constructor.

5) Abstract class can provide the Interface can't provide the


implementation of interface. implementation of abstract class.

6) The abstract keyword is used to declare The interface keyword is used to


abstract class. declare interface.

You might also like