CP1344: PROGRAMMING IN JAVA - LECTURE -42- INHERITANCE & INTERFACE (DEFINING INTERFACES) - BCA- S3.

preview_player
Показать описание
Inheritance & Interface
classes can be inherited by other classes. We also learned about various forms of inheritance and pointed out that Java does not support multiple inheritance. That is, classes in Java cannot have more than one superclass. For instance, a definition like:
class A extends B extends C
{
}
is not permitted in Java. A large number of real-life applications require the use of multiple inheritance whereby we inherit methods and properties from several, distinct classes. Java provides an alternate approach known as interfaces to support the concept of multiple inheritance. Although a Java class cannot be a subclass of more than one superclass, it can implement more than one interface, thereby enabling us to create classes that build upon other classes without the problems created by multiple inheritance.
Defining Interfaces

An interface is basically a kind of class. Like classes, interfaces contain methods and variables but with a major difference, The difference is that interfaces define only abstract methods and final fields. This means that interfaces do not specify any code to implement these methods and data fields contain only constants. Therefore, it is the responsibility of the class that implements an interface to define the code for implementation of these methods.
The syntax for defining an interface is very similar to that for defining a class. The general form of an interface definition is:
interface InterfaceName
{
variables declaration;
methods declaration;
}
Here, interface is the key word and inferfaceName is any valid Java variable (just like class names). Variables are declared as follows:

static final type VariableName = Value;

Note that all variables are declared as constants. Methods declaration will contain only a list of methods without any body statements. Example:

return-type methodNamel (parameter_list);
Here is an example of an interface definition that contains two variables and one method:

interface Item
{
static final int code = 1001;
static final String name = "Fan";
void display ( ) ;
}
Note that the code for the method is not included in the interface and the method declaration simply ends with a semicolon. The class that implements this interface must define the code for the method.
Another example of an interface is:

interface Area

{
final static float pi = 3.142F;
float compute (float x, float y);
void show ();
}
CLASS INTERFACE
The keyword used to create a class is “class” The keyword used to create an interface is “interface”
A class can be instantiated i.e, objects of a class can be created. An Inteface cannot be instantiated i.e, objects cannot be created.
Classes does not support multiple inheritance. Inteface supports multiple inheritance.
It can be inherit another class. It cannot inherit a class.
It can be inherited by another class using the keyword ‘extends’. It can be inherited by a class by using the keyword ‘implements’ and it can be inherited by an interface using the keyword ‘extends’.
It can contain constructors. It cannot contain constructors.
It cannot contain abstract methods. It contains abstract methods only.
Variables and methods in a class can be declared using any access specifier(public, private, default, protected) All variables and methods in a interface are declared as public.
Variables in a class can be static, final or neither. All variables are static and final.
Рекомендации по теме