Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface I {
- void m();
- }
- public class C implements I {
- public void m(){}
- }
- I i = new C();
- public class A {
- // this is your instance variable:
- private B b = new BImpl();
- }
- public interface B {
- }
- public class BImpl implements B {
- }
- public interface I {
- public String doSomething();
- }
- public class A implements I {
- public String doSomething() {
- return "Hello";
- }
- }
- public class Test {
- public static void main(String[] args) {
- I obj = new A();
- System.out.println(obj.doSomething());
- }
- }
- interface i5
- {
- void display();
- }
- class A implements i5
- {
- public void display()
- {
- System.out.println("hello this is me abhishek");
- }
- }
- class five
- {
- public static void main(String args[])
- {
- A obj=new A();
- obj.display();
- }
- }
- new Cat("garfield");
- Cat g;
- Cat g = new Cat("garfield");
- public interface Printable { //simple interface
- void print();
- }
- public class Cat implements Printable { //a cat is both a cat and a printable
- private String mName;
- public Cat(String name) {
- mName = name;
- }
- public void meow() {
- System.out.println("meow");
- }
- public void print() {
- System.out.println("This is the cat " + mName);
- }
- }
- public class Car implements Printable { //a car is both a car and a printable
- private String mMake;
- public Car(String make) {
- mMake = make;
- }
- public void rev() {
- System.out.println("wroooom");
- }
- public void print() {
- System.out.println("This is a car, the make of the car is " + mMake);
- }
- }
- public class Main {
- public static void main(String[] args) {
- Cat g; //creates a reference
- //for referencing objects of the type Cat
- g = new Cat("Garfield"); //creates an instance of a cat,
- //and lets g reference this instance
- //normally, this is all done on one line
- //like this following line
- Car t = new Car("Toyota"); //creates a reference to a car,
- //an instance of a car, and lets the
- //reference t reference that new instance
- g.meow(); //do cat-specific thing
- t.rev(); //do car-specific thing
- g.print(); //this prints the cat/garfield stats
- t.print(); //this prints the car/toyota stats
- Printable p; //this declares a reference to Printable
- p = g; //this lets p reference the cat object
- //this is possible since
- //the cat is a printable
- p.print();
- p = t; //same goes with a car.
- p.print(); //since p is now pointing to the car,
- //this prints the stats for the toyota
- //p.rev(); //this line is commented, cause it won't
- //compile. even though p points to an
- //object which is fact a car, p can't find
- //the address of rev, p, can only find the
- //address of print
- }
- }
- meow
- wroooom
- This is the cat Garfield
- This is a car, the make of the car is Toyota
- This is the cat Garfield
- This is a car, the make of the car is Toyota
- public class Printer {
- Printable mToBePrinted; //instance variable of an interface
- public Printer(toBePrinted) {
- mToBePrinted = toBePrinted; //now references an object constructed from
- //a class that implements Printable
- }
- public void perform() {
- mToBePrinted.print();
- }
- }
- Printer printer = new Printer(g);
- printer.perform();
- public interface Foo {
- void doSomething();
- }
- public class Bar implements Foo {
- public void doSomething() {
- System.out.println("I'm on it....");
- }
- }
- public class Baz {
- private Bar bar; // instance variable
- public Baz() {
- this.bar = new Bar(); // create an instance variable
- }
- }
- public class Bat {
- private Baz baz;
- public Bat(Baz baz) {
- this.baz = baz; // constructor injection
- }
- }
Add Comment
Please, Sign In to add comment