FuzzDuck
FuzzDuck
Abstract class
FuzzDuck
Abstract class

Abstract Class

A class that can’t be instantiated. Its parameters and methods can be inherited by an ordinary class, which takes the default values from the abstract class.

An abstract class is a class, which cannot be instantiated, as it does not define a specific class. It is used as a parent class, if multiple subclasses have different structures in common. The abstract class’ parameters and methods are inherited by the child classes, which can rewrite the methods, or use the structure from the abstract class.

An example of an abstract class could be the abstract class “Bird”, which have several subclasses like “Duck” and “Goose”. The “Bird” abstract class could have several parameters like Age, name, size and methods like fly(), walk(), and makeSound(). The child classes “Duck” and “Goose” would then inherit the parameters and methods and use the standard values, unless new ones are defined.

Code Example - Abstract class
// --- Declares the abstract class --- //
public abstract class Bird {

 // --- Declares methods, which are inherited by subclasses --- //
 public abstract void fly();
 public abstract int walk();
 public abstract String makeSound();

}
// --- Declares the abstract class --- //
public abstract class Bird {

 // --- Declares methods, which are inherited by subclasses --- //
 public abstract void fly();
 public abstract int walk();
 public abstract String makeSound();

}
// --- Declares the abstract class --- //
abstract class Bird {

 // --- Declares methods, which are inherited by subclasses --- //
 public abstract function fly();
 public abstract function walk();
 public abstract function makeSound();

}
Code Example - Class extending abstract class methods
// --- Declares the Duck class inheriting Bird methods --- //
public class Duck : Bird {

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

 // --- Overriding the inherited method "fly()" to define new method content --- //
 public override void fly(){
  Console.WriteLine("Duck flies");
 }

 // --- Overriding the inherited method "walk()" to define new method content --- //
 public override int walk(){
  Console.WriteLine("Duck walks 5 steps");
  return 5;
 }

 // --- Overriding the inherited method "makeSound()" to define new method content --- //
 public override String makeSound(){
  Console.WriteLine("Duck says Quack");
  return "Quack";
 }

}
// --- Declares the Duck class inheriting Bird methods --- //
public class Duck extends Bird {

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

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

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

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

}
// --- Declares the Duck class inheriting Bird methods --- //
class Duck extends Bird {

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

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

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

 // --- Declares the inherited method "makeSound()" returning a string --- //
 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.WriteLine(ducky.name);

 }

}
// --- 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 --- //
 System.out.println(ducky.age);

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

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

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

 // --- Calling ducky's method 'makeSound()' --- //
 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();