- Only Method declaration
- No method body-only method prototype
- In Interface we can declare the variables,variables are by default static in nature
- Variables value will not be changed
- No static method in interface
- No main method in interface
- We can not create the object of interface
- Interface is abstract in nature
- Interface is the blue print of class:
interface Test Int
{
//varibles
//methods
}
- Interface contains only static and final variables(by default)
- In interface by default methods are public
- In interface methods are by default abstract.
Abstract:A method have only definition but not
implementation
interface Testint
{
void m1(); //abstract method
}
class TestClass implements TestInt
{
void m1()
{
//implement the body
}
}
- We cannot instantiate interface
TestInt testobj=new Testint();//not valid
TestInt testobj=new TestClass();//valid- Multiple Inheritance can be implemented using interface
Interface:
public interface USBank {
int min_bal=100;
public void credit();
public void debit();
public void transferMoney();
}
public interface BrazilBank {
public void mutualFund();
}
Implementing Interface:
public class HSBCBank implements USBank,BrazilBank{
//we are achieving multiple inheritance is a relationship
//If a class is implementing interface ,then it is mandatory to define/override all the methods of interface
//overriding from USBank
public void credit() {
System.out.println("HSBCBank----credit");
}
public void debit() {
System.out.println("HSBCBank----debit");
}
public void transferMoney() {
System.out.println("HSBCBank-----transferMoney");
}
//seperate method of HSBCBank class
public void educationLoan() {
System.out.println("HSBCBank----eductionLoan");
}
public void carLoan() {
System.out.println("HSBCBank----carLoan");
}
//brazil bank method:Override from brazil bank Interface
public void mutualFund() {
System.out.println("HSBCBank---mutual fund");
}
}
Main Method:
public static void main(String[] args) {
//static polymorphism
HSBCBank hsbc=new HSBCBank();
hsbc.credit();
hsbc.debit();
hsbc.transferMoney();
hsbc.educationLoan();
hsbc.carLoan();
//dynamic polymorphism
//child class object can be referred with interface reference var
USBank usbank=new HSBCBank();
usbank.credit();
usbank.debit();
usbank.transferMoney();
}
No comments:
Post a Comment