Interface
Interface in Java
Interfaces in java
1) create an interface package com.training.slc; public interface Mama { String NAME ="VENU"; void addNumber(); int AddNumber(); } 2) Create a class which implements interface package com.training.slc; public class MamaImpl1 implements Mama { int n1 =10;; int n2 = 100; public static void main(String [] args ) { System.out.println( Mama.NAME );// this line nnned not be since Name is also a member of MamaImpl System.out.println( NAME ); } @Override public int AddNumber() { // TODO Auto-generated method stub return n1+n2+100; } @Override public void addNumber() { // TODO Auto-generated method stub System.out.println(n1+" \n " + n2); } } 3) create another class which implements interface package com.training.slc; public class MamaImpl2 implements Mama { int a1=10; @Override public int AddNumber() { // TODO Auto-generated method stub return a1+10; } @Override public void addNumber() { // TODO Auto-generated method stub System.out.println(a1); } } 4) how to use them? package com.training.slc; public class SomeClass { public static void main(String [] args) { //Mama m = new Mama(); this doesnt work since interface instantiate Mama m1 = new MamaImpl1(); Mama m2 = new MamaImpl2(); System.out.println( "THIS IS M1 interface:"+ m1.AddNumber()); System.out.println( "THIS IS M2 interface: "+ m2.AddNumber()); } }
Please send comments to
vgdarur.javafive@blogger.com
Copyright © 2008 - iForeRunner.com
http://www.iforerunner.com/