FuzzDuck
FuzzDuck
Interface
FuzzDuck
Interface

Interface

Like an empty class, with empty methods. An interface is used to inherit method names and the methods return values to subclasses. An interface can’t be instantiated.

The interface is not a class in itself. Rather it is an empty shell, in which you can set methods in which you wish other classes to inherit. The methods inside the interface are empty and only used to define the name and return value, which are inherited to subclasses.

An example of an interface could be an interface called “BirdSketch”, in which we define the following methods: fly(), eat(), makeSound(). When a class inherits from the “BirdSketch” interface, it also inherits the three methods. A subclass could be the “Duck” class, which inherits the methods from the interface “BirdSketch”.

Code Example - Creating interface with methods to be implemented in subclasses
// --- Declares the interface --- //
public interface BirdSketch {

 // --- Declares interface methods --- //
 void fly();
 int walk();
 String makeSound();

}
// --- Declares the interface --- //
public interface BirdSketch {

 // --- Declares interface methods --- //
 public void fly();
 public int walk();
 public String makeSound();

}
// --- Declares the interface --- //
interface BirdSketch {

 // --- Declares interface methods --- //
 public function fly();
 public function walk();
 public function makeSound();

}
Code Example - Instantiating the class above, retrieving variables and calling methods / functions
// --- Declares Duck class implementing BirdSketch methods --- //
public class Duck : BirdSketch{

 // --- Declares the variables inside the class --- //
 public int age = 5;
 public String name = "Ducky";

 // --- Declares the inherited method "fly()" returning nothing --- //
 public void fly() {
  Console.WriteLine("Duck flies");
 }

 // --- Declares the inherited method "walk()" returning an int--- //
 public int walk() {
  Console.WriteLine("Duck walks 5 steps");
  return 5;
 }

 // --- Declares the makeSound method --- //
 public String makeSound() {
  Console.WriteLine("Duck says Quack");
  return "Quack";
 }

}
// --- Declares the beginning of the class --- //
public class Duck implements BirdSketch{

 // --- Declares the variables inside the class --- //
 public int age = 5;
 public String name = "Ducky";

 // --- Declares the fly method --- //
 public void fly() {
  System.out.println("Duck flies");
 }

 // --- Declares the walk method --- //
 public int walk() {
  System.out.println("Duck walks 5 steps");
  return 5;
 }

 // --- Declares the makeSound method --- //
 public String makeSound() {
  System.out.println("Duck says Quack");
  return "Quack";
 }

}
// --- Declares Duck class implementing BirdSketch methods --- //
class Duck implements BirdSketch{

 // --- Declares the variables inside the class --- //
 public $age = 5;
 public $name = "Ducky";

 // --- Declares the fly method --- //
 public function fly() {
  echo "Duck flies";
 }

 // --- Declares the walk method --- //
 public function walk() {
  echo "Duck walks 5 steps";
  return 5;
 }

 // --- Declares the makeSound method --- //
 public function makeSound() {
  echo "Duck says Quack";
  return "Quack";
 }

}
Code Example - Instantiating the class above, retrieving variables and calling methods / functions
// --- Declaring starting point of the program run with "main" method --- //
public static void Main(string[] args){

 // --- Instantiating a Duck instance called 'ducky' --- //
 Duck ducky = new Duck();

 // --- Printing ducky's age --- //
 Console.WriteLine(ducky.age);

 // --- Printing ducky's name --- //
 Console.WriteLine(ducky.name);

 // --- Calling ducky's method 'fly()' --- //
 ducky.fly();

 // --- Calling ducky's method 'walk()' --- //
 ducky.walk();

 // --- Calling ducky's method 'makeSound()' --- //
 ducky.makeSound();

 // --- Invoking 'ReadKey()' to make time to read the output --- //
 Console.ReadKey();

}
// --- Declaring starting point of the program run with "main" method --- //
public static void main(String [ ] args){

 // --- Instantiating a Duck instance called 'ducky' --- //
 Duck ducky = new Duck();

 // --- Printing out ducky's age --- //
 System.out.println(ducky.age);

 // --- Printing out ducky's name --- //
 System.out.println(ducky.name);

 // --- Calling ducky's method 'fly()'. Which in the method body prints 'Duck flies' --- //
 ducky.fly();

 // --- Calling ducky's method 'walk()' printing out the returned int --- //
 ducky.walk();

 // --- Calling ducky's method 'makeSound()' printing out the returned String --- //
 ducky.makeSound();

}
// --- Instantiating a Duck instance called 'ducky' --- //
$ducky = new Duck();

// --- Retrieve ducky's age --- //
echo $ducky->age;

// --- Retrieve ducky's name --- //
echo $ducky->name;

// --- Calling ducky's method 'fly()' --- //
$ducky->fly();

// --- Calling ducky's method 'walk()' --- //
$ducky->walk();

// --- Calling ducky's method 'makeSound()' --- //
$ducky->makeSound();