Interface

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

INTERFACE

Que:
1. What is interface?
2. Explain with example how multiple inheritance can be implemented by interface.
3. Define interface in Java.
4. Explain how to implement multiple inheritances in java through interface?
5. Explain interface in Java.
6. Explain Interface concept with example.
Ans:

 An interface is a collection of abstract methods (i.e. methods without having


definition).
 Interface specifies what a class must do,but not how it does it.
 Each class that includes an interface must implement all of the methods.
 One class can implement any number of interfaces.
 Defining an Interface
 Interface is defined muchlike a class.
 This is the general form of an interface.

Accessinterface name
{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);

type final-varname1 = value;


type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}

 The access modifier is either public or no modifier.


 When interface is declared public, it is used anywhere and when its declared with no
modifier than it can be used only in a package in which it is declared.
 Name is the name of the interface.
 All methods of an interface are abstract methods. It means without its bodies.
 All method end with a semicolon after the parameter list.
 Variables can be declared inside interface. They are implicitly final and static,
meaning they cannot be changed by the implementing class. They must also be
initialized.
 All methods and variables are implicitly public.

Implementing Interfaces
 Once an interface has been defined, one or more classes can implement that interface.
 Toimplement an interface, include the implements clause in a class definition, and
then createthe methods defined by the interface.
 The general form of a class that includes the implementsclause looks like this:

Access classclassname[extends superclass] [implements interface


[,interface...]]
{
// class-body
}

 For Example,
interface Callback
{
void callback(intparam);
}
class Client implements Callback
{
public void callback(int p) // Implement Callback's interface
{
System.out.println("callback called with " + p);
}
}
classTestIface
{
public static void main(String args[])
{
Client c = new Client();
c.callback(42);
}
}

 OUTPUT:
callback called with 42

multiple inheritance using interface

 Class can implements more than one interface.so we can create multiple inheritance by
implementing more than one interface.

interface i1
{
void print();
}

interface i2
Page 2 of 9
{
void show();
}

public class demo implements i1,i2


{
public void print()
{
System.out.println("hello from i1");
}
public void show()
{
System.out.println("hello from i2");
}
public void display()
{
System.out.println("hello ");
}
}
Class InterfaceDemo
{
public static void main(String[] args) {

demo obj =new obj();


obj.print();
obj.show();
}
}

Similarities and Differences between Class and Interface

 The similarities are between Class and Interface are as below:

1) They both can contain variables and methods. (With difference being class methods
have implementation code whereas the interface methods can only have declarations)
2) They can both be inherited using Inheritance. (extends keyword for classes and
implements keyword for interfaces)
 difference between class and interface are as below:

Sr. Interface Class


No
We cannot instantiate (i.e. we can instantiate a class
1
create object) an interface
2 All the methods in an All the methods of class are
Page 3 of 9
interface are abstract (i.e. not abstract.
without definitions)
The variable is static and class can contain any type of
3 final. variable

An interface can extend Classcan not extend multiple


multiple interfaces (i.e. classes (i.e multiple
4
multiple inheritances can inheritance can not be
be achieved through it). achieved).

Abstract class
Que:
1. Differentiate Abstract class and Interface.
2. Explain Abstract Keyword with example.
3. Explain how abstract class differs from final class.
4. Define Abstract class.
5. Explain Abstract class in Java.
6. Explain abstract and final keywords with example.
Ans:

 A class that is declared with abstract keyword, is known as abstract class.


 It can have abstract and non-abstract method.
 An abstract method is a method that is declared without an implementation.
 If a class hasat least one abstract method, then the class must be declared abstract.
 It is not possible to create object of an abstract class.
 To use an abstract class, we have to inherit abstract from another class.
 If we inherit an abstract class, we have to provide implementations to all the abstract
methods in it.
 Syntax:
abstract class class-name
{
abstract type-name(parameter-list); //abstract Method

type-name(parameter-list) //Non-Abstract Methods;


{
//statements;
}
}

 Example:

// A Simple demonstration of abstract.

abstract class A
{

Page 4 of 9
abstract void callme();

void callmetoo()
{
System.out.println("This is a concrete method.");
}
}
Class B extends A
{
void callme()
{
System.out.println("B's implementation of callme.");
}
}
class AbstractDemo
{
public static void main(String args[])
{
B b = new B();
b.callme();
b.callmetoo();
}
}

Difference between Interface and abstract class


Que:
1. Compare following:
(i) Abstract Class Vs Interfaces
Ans:
Abstract class Interface
1 Abstract class contains Interface contains only
abstract methods and Non- Absrtact methods.
Abstract Methods. Java-8 also use default and
static methods.
2 abstract keyword is used to interface keyword is used to
create an abstract class create interface
3 Subclasses subclasses
use extends keyword to use implements keyword to
extend an abstract class. implement interfaces.
4 Abstract classes can have interfaces can’t have
constructors. constructors.
5 Abstract classe methods can interface methods are
have access modifiers as implicitly public.
public, private, protected,
static

Page 5 of 9
6 A subclass can extend only A subclass can implement
one abstract class. more than one interface.
7 Syntax: Syntax:
abstract class name Interface name
{ {
//abstract and Non- //abstract method
Abstract Methods }
}

Final Keyword
Que:
1. When do we declare a method or class as final.
2. Explain final keyword with example.
3. Define Final keyword in java.
Ans:
 The keyword final has three users.
1. It can be used to create equivalent of named constants.
2. It also can used to prevent inheritance.
3. It can used to prevent overriding.

1.Final Variable
 A variable can be declared with the use of final keyword then it is known as ‘Final
Variable’.
 To prevent the contents of variable from being modified then assign that variable as
Final Variable.
 This means that you must initialize a final variable when it is declared.
 In C/ C++, we have to use const keyword for define constant variable, whether in Java
we are using final keyword to define as constant.
 To define Constant:

final datatype variable=value;

final int PI=3.14


 It is a common coding convention to choose all uppercase identifiers for final
variables.
 Variables declared as final do not occupy memory on a per-instance basis. Thus, a
final variable is essentially a constant.

2.Final Class
 Sometimes you will want to prevent a class from being inherited. To do this, precede
the class declaration with final.
 Declaring a class as final implicitly declares all of its methods as final, too.

Page 6 of 9
 As you might expect, it is illegal to declare a class as both abstract and final since an
abstract class is incomplete by itself and relies upon its sub classes to provide complete
implementation.
 here is an example of a final class:

Final class A
{
// ...
}
// the following class is illegal.
Class B extends A
{
// ERROR! Can't subclass A
// ...
}
 As the comments imply, it is illegal for B to inherit A since A is declared as final.

3.Using Final to Prevent Overriding


 While method overriding is one of Java’s most powerful features, there will be times
when you will want to prevent it from occurring.
 To disallow a method from being overriding, specify final as a modifier at the
start of its declaration. Methods declared as final can’t be overridden.

Example:

Class A
{
A ()
{
final void meth ()
{
System.out.println (“This is Final Method.”);
}
}
}
Class B extends A
{
Void meth () //Error:= Can’t Override
{
System.out.println (“Illegal”);
}
}

Garbage Collection

 Memory allocated to object released automatically in java is called Garbage collection.


Page 7 of 9
 It is the process by which Java programs perform automatic memory management.
 When Java programs run on the JVM, objects are created in the memory.
 When there are no references to an object, it is assumed to be no longer needed.
 The garbage collector finds these unused objects and deletes them to free up memory.

finalize method

 finalize method is a method that the Garbage Collector always calls just before the
deletion of the object.
 This method is used to perform some final operations or clean up operations on an
object before an object is destroyed.
 Here is the general form of finalize() method:

protected void finalize( )


{
//Keep some resource closing operations here

 Example:
class A
{
int i = 50;

@Override
protected void finalize() throws Throwable
{
System.out.println("From Finalize Method");
}
}

public class Test


{
public static void main(String[] args)
{

A a1 = new A();

A a2 = new A();

a1 = a2;

System.out.println("done");

Page 8 of 9
}
}

Page 9 of 9

You might also like