daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public interface I {
  2.     void m();
  3. }
  4.  
  5. public class C implements I {
  6.     public void m(){}
  7. }
  8.    
  9. I i = new C();
  10.    
  11. public class A {
  12.   // this is your instance variable:
  13.   private B b = new BImpl();
  14. }
  15.  
  16. public interface B {
  17. }
  18.  
  19. public class BImpl implements B {
  20. }
  21.    
  22. public interface I {
  23.     public String doSomething();
  24. }
  25. public class A implements I {
  26.     public String doSomething() {
  27.          return "Hello";
  28.     }
  29. }
  30.  
  31. public class Test {
  32.     public static void main(String[] args) {
  33.         I obj = new A();
  34.  
  35.         System.out.println(obj.doSomething());
  36.     }
  37. }
  38.    
  39. interface i5
  40. {
  41. void display();
  42. }
  43.  
  44.      class A implements i5
  45.       {
  46.     public void display()
  47.       {
  48.     System.out.println("hello this is me abhishek");
  49.      }
  50.     }
  51.  
  52.          class five
  53.          {
  54.         public static void main(String args[])
  55.        {
  56.    
  57. A obj=new A();
  58.        obj.display();
  59.  
  60.            }
  61.  
  62.          }
  63.    
  64. new Cat("garfield");
  65.    
  66. Cat g;
  67.    
  68. Cat g = new Cat("garfield");
  69.    
  70. public interface Printable { //simple interface
  71.     void print();
  72. }
  73.    
  74. public class Cat implements Printable { //a cat is both a cat and a printable
  75.     private String mName;
  76.  
  77.     public Cat(String name) {
  78.         mName = name;
  79.     }
  80.  
  81.     public void meow() {
  82.         System.out.println("meow");
  83.     }
  84.  
  85.     public void print() {
  86.         System.out.println("This is the cat " + mName);
  87.     }
  88. }
  89.    
  90. public class Car implements Printable { //a car is both a car and a printable
  91.     private String mMake;
  92.  
  93.     public Car(String make) {
  94.         mMake = make;
  95.     }
  96.  
  97.     public void rev() {
  98.         System.out.println("wroooom");
  99.     }
  100.  
  101.     public void print() {
  102.         System.out.println("This is a car, the make of the car is " + mMake);
  103.     }
  104. }
  105.    
  106. public class Main {
  107.     public static void main(String[] args) {
  108.         Cat g;                       //creates a reference
  109.                                      //for referencing objects of the type Cat
  110.         g = new Cat("Garfield");     //creates an instance of a cat,
  111.                                      //and lets g reference this instance
  112.                                      //normally, this is all done on one line
  113.                                      //like this following line
  114.         Car t = new Car("Toyota");   //creates a reference to a car,
  115.                                      //an instance of a car, and lets the
  116.                                      //reference t reference that new instance
  117.  
  118.         g.meow();                    //do cat-specific thing
  119.         t.rev();                     //do car-specific thing
  120.  
  121.         g.print();                   //this prints the cat/garfield stats
  122.         t.print();                   //this prints the car/toyota stats
  123.  
  124.         Printable p;                 //this declares a reference to Printable
  125.         p = g;                       //this lets p reference the cat object
  126.                                      //this is possible since
  127.                                      //the cat is a printable
  128.  
  129.         p.print();
  130.         p = t;                       //same goes with a car.
  131.         p.print();                   //since p is now pointing to the car,
  132.                                      //this prints the stats for the toyota
  133.  
  134.         //p.rev();                   //this line is commented, cause it won't
  135.                                      //compile. even though p points to an
  136.                                      //object which is fact a car, p can't find
  137.                                      //the address of rev, p, can only find the
  138.                                      //address of print
  139.     }
  140. }
  141.    
  142. meow
  143. wroooom
  144. This is the cat Garfield
  145. This is a car, the make of the car is Toyota
  146. This is the cat Garfield
  147. This is a car, the make of the car is Toyota
  148.    
  149. public class Printer {
  150.     Printable mToBePrinted; //instance variable of an interface
  151.  
  152.     public Printer(toBePrinted) {
  153.         mToBePrinted = toBePrinted; //now references an object constructed from
  154.                                     //a class that implements Printable
  155.     }
  156.  
  157.     public void perform() {
  158.         mToBePrinted.print();
  159.     }
  160. }
  161.    
  162. Printer printer = new Printer(g);
  163. printer.perform();
  164.    
  165. public interface Foo {
  166.         void doSomething();
  167.     }
  168.  
  169.     public class Bar implements Foo {
  170.         public void doSomething() {
  171.             System.out.println("I'm on it....");
  172.         }
  173.     }
  174.  
  175.     public class Baz  {
  176.         private Bar bar; //  instance variable
  177.  
  178.         public Baz() {
  179.             this.bar = new Bar(); //  create an instance variable
  180.         }
  181.     }
  182.  
  183.     public class Bat {
  184.         private Baz baz;
  185.  
  186.         public Bat(Baz baz) {
  187.             this.baz = baz;  // constructor injection
  188.         }
  189.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top