Simple interface with Package

Interfaces

|  [ Implements ] [ Extends ]

Classes

 

Use of Interface in java programs

Creating method indexing for easy understanding long programs

Creating final keywords

Multiple inheritance possible in java programs using interfaces

 

Rules of Interfaces

Interfaces and indexing methods must be public

Indexing methods must be override in implemented class

 

Interfaces inter1 in InterPack1 package

package InterPack1;

public interface Inter1   // Interfaces

{

public int x = 10;                // it’s final variable

public void method1();  // Indexing | Only Dec

}


Class A in classStore package

package classStore;

import InterPack1.*;

public class A implements Inter1

{

public void method1()   // override interfaces method

{

//x++;   // Can’t increment | dec final interfaces variable

System.out.println(“Welcome in Interface1”);

}

public void setA()             // own method

{

System.out.println(“This is Class Method”);

}

}


Class Example1 in mainPackage

package mainPackage;

import classStore.*;

import InterPack1.*;

public class Example1

{

public static void main(String args[])

{

// How to create interfaces.

class

//Inter1 ref = new Inter1();         // Error

Inter1 ref = new A();      // Assign object of A class to Inter1 ref…

ref.method1(); // We just call override interface1 method

//ref.setA();                       // Can’t call this method

A a1 = new A();

a1.setA();

a1.method1();

}

}

 

Output:

Welcome in Interface1

This is Class Method

 

Video available on YouTube.


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe our website


Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

454total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

EmailEmail
PrintPrint