Interface :: Multiple
Inheritance
Multiple inheritance: Derivation of a
class from two or more classes is called multiple inheritances.
Since Java does not support multiple inheritances, which leads to
duplication in the methods for the derived classes which lead to complication
at run-time. To overcome this drawback java provides a concept of Interfaces.
Defining Interfaces
Interface
is a collection of ‘n’ number of final members and abstract methods that can be
implemented by any number of subclasses.
Interface provides dynamic request dispatching of methods when implemented by
‘n’ number of sub-classes. No objects will be created for Interface because it
implements the particular class so the object will be created for that class.
Syntax
for declaration:
Interface <interfacename>
{
members=value;
returntype
<methodname>(argslist);
}
Syntax
for implementation:
{
overridden methods from
interface ( )
{
body;
}
}
keypoints :
- All the methods
declared inside the interface does not require any body and they are by
default abstract types
- All the members
declared inside the interface are by default final which must be
initialized and cannot be modified in the subclasses
- Once a class
implements an interface it has to over-ride all the methods of the
interface atleast with null body.
- Interfaces cannot
have instances
- One class can
implement any number of interfaces.
- Interface can be
pointed to the objects of those classes which implements the interface
leads to dynamic method dispatching.
Extending
Interfaces
Like
classes, interfaces can also be extended i.e., an interface can be sub
interfaced from other interfaces. The new subinterface will inherit all the
members of the superinterface in the manner similar to subclasses. This is
achieved using the keyword extends as shown below:
Syntax:
Interface
name2 extends name1
{
body
}
One
interface can inherit from another interface and if a class implements the
later interface the class must override all the methods of both the interfaces.
Implementing Interfaces
Interfaces
are used as “superclass” whose properties are inherited by classes.
Syntax:
Class classname implements interfacename
{
body
}
Accessing Interface Variables
Interfaces
can be used to declare a set of constants that can be used in different
classes. The constant values will be available to any class that implements the
interface. The values can be used in any method, where we can use a final
value.
Example Program:
interface demo //definition
of interface
{
int sno=101; //by default final which cannot
be modified
String
sname="GVP";
}
interface demo2 extends demo //extending
interfaces
{
void display(); //by default abstract
}
class example implements demo2 //implementation
of interface
{
public void
display()
{
System.out.println("Sno="+sno+"\n"+"Sname="+sname);
//accessing
interface variables
}
}
class inter
{
public static
void main(String []args)
{
example
e=new example();
e.display();
}
}
Output:
D:\ \programs>javac inter.java
D:\ \programs>java inter
Sno=101
Sname=GVP
//Bank Transactions using Interfaces
interface bank
{
int accno=101;
String
accname="GVP";
int amt=10000;
}
interface acctype extends bank
{
void
calc_interest(int rof);
void
put_details();
}
abstract class members implements acctype
{
float
interest=0.0f;
float bal=0.0f;
public void
put_details()
{
System.out.println("Account
Number="+accno);
System.out.println("Account
Name="+accname);
System.out.println("Amount
="+amt);
System.out.println("Interest="+interest);
System.out.println("Balance="+bal);
}
}
class current_accnt extends
members implements acctype
{
public void
calc_interest(int rof)
{
interest=(rof*amt)/100;
bal=amt-interest;
}
}
class savings_accnt extends members implements acctype
{
public void
calc_interest(int rof)
{
interest=(rof*amt)/100;
bal=amt-interest;
}
}
class interest
{
public static
void main(String []args)
{
current_accnt
ca=new current_accnt();
savings_accnt
sa=new savings_accnt();
if(args[0].equals("current"))
{
ca.calc_interest(10);
ca.put_details();
}
else
if(args[0].equals("savings"))
{
sa.calc_interest(5);
sa.put_details();
}
else
System.exit(0);
}
}
Output:
D:\ \programs>javac interest.java
D:\ \programs>java interest current
Account Number=101
Account Name=GVP
Amount =10000
Interest=1000.0
Balance=9000
No comments:
Post a Comment