মঙ্গলবার, ১২ মার্চ, ২০১৩

Object Oriented Concept

Inheritence is one of the most important feature of OOP.We know interface can inherit using implements keyword and we can not create an object of interface.
But Interface instance can hold a reference of any object and it is a interesting concept of OOP for design pattern.I want share simple topis using a simple java program

Step One :
Create a project and also create a Interface under the package of the project

public interface Ainterface {
 public void printSomething();

}
Step Two:
Create two class namely ClassA and ClassB as follow

public class ClassA implements Ainterface{
 @Override
 public void printSomething() {
  // TODO Auto-generated method stub
  System.out.print("Class A");
 }
 }


public class ClassB implements Ainterface {
 @Override
 public void printSomething() {
  // TODO Auto-generated method stub
  System.out.print("Class B");
 }

}

Step Three :
Create main class and run the project

public class Main {
 public static void main(String[] args) {
  Test(new ClassA());
  System.out.println();
  Test(new ClassB());

 }
 public static void Test(SamsungInterface s) {
  s.printSomething();
 }

}
Output:
ClassA
ClassB

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন