umma.dev

Learning Java: Part Three

In this part I look at Object Oriented Programming; a way of structing code in Java applications.

Abstraction

Hides complexity from the user and only shows the relevant information.

Goals

  • Code Reusability
  • Flexibility of implementation
  • Multiple inheritance

Syntax

  • Abstract classes: abstract
  • Interfaces: interface implements

Abstract Classes

An abstract class is a superclass (parent class) which cannot be instantiated.

abstract class Animal {
  abstract void move();
  abstract void eat();
  void label();

  System.out.println("Animal's data:" );
}
class Bird extends Animal {
  void move() {
    System.out.println("Moves by flying.");
  }
  void eat() {
    System.out.println("Eats birdfood.");
  }
}
class TestBird {
  public stat voice main(String[] args) {
    Animal myBird = new Bird();
    myBird.label();
    myBird.move();
    myBird.eat();
  }
}

Interfaces

An interface is a 100% abstract class, meaning there isn’t a mix between concrete and abstract. It can only have static, final and public fields and abstract methods.

Frequently referred to as a blueprint of a class.

Classes can access an interface with the implements keyword.

interface Animal {
  public void eat();
  public void sound();
}
interface Bird {
  int noOfLegs = 2;
  String outerCovering = "feather";
  public void fly();
}
class Eagle implements Aniaml, Bird {
  public void eat() {
    System.out.println("Eats reptiles and amphibians.");
  }
  public void sound() {
    System.out.println("Has a high-pitched whistling sound.");
  }
  public void fly() {
    System.out.println("Flies up to 35,800 feet.");
  }
}
class TestEagleInterfaces {
  public static void main(String[] args) {
    Eagle myEagle = new Eagle();

    myEagle.eat();
    myEagle.sound();
    myEagle.fly();

    System.out.println("Number of legs: " + Bird.noOfLegs);
    System.out.println("Outer covering: " + Bird.outerCovering);
  }
}

Encapsulation

Binds data and it’s related methods together with a class. Protects data by making fields private and giving access the them only through their related methods.

Goals

  • Data protection
  • Code readability

Syntax

private getData() setData()

Code Example

class Animal {
  private String name;
  private double averageWeight;
  private int noOfLegs;

  public String getName() {
    return name;
  }

  public double getAverageWeight() {
    return averageWeight;
  }

  public int getNoOfLegs() {
    return noOfLegs;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setAverageWeight(double averageWeight) {
    this.averaeWeight = averageWeight;
  }

  public void setNoOfLegs(int noOfLegs) {
    this.noOfLegs = noOfLegs;
  }
}
class TestAnimal {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();
    myAnimal.setName("Eagle");
    myAnimal.setAverageWeight(1.5);
    myAnimal.setNoOfLegs(2);

    System.out.println("Name: " + myAnimal.getName());
    System.out.println("Average weight: " + myAnimal.getAverageWeight() + "kg");
    System.put.println("Number of legs: " + myAnimal.getNoOfLegs());
  }
}

Inheritance

Allows a child class to inherit features (fields and methods) of a parent class. One class can only extend one other class.

Goals

  • Code reusability
  • Code readability

Syntax

extends

Code Example

class Bird {
  public String reproduction = "egg";
  public String outerCovering = "feather";

  public void flyUp() {
    System.out.println("Flying up");
  }

  public void flyDown() {
    System.out.println("Flying down");
  }
}
class Eagle extends Bird {
  public String name = "eagle";
  public int lifespan = 15;
}
class TestEagleInheritance {
  public static void main(String[] args) {
    Eagle myEagle = new Eagle();

    System.put.println("Name: " + myEagle.name);
    System.out.println("Reproduction: " + myEagle.reproduction);
    System.out.println("Outer covering: " + myEagle.outerCovering);
    System.out.println("Lifespan: " + myEagle.lifespan);

    myEagle.flyUp();
    myEagle.flydown()
  }
}

Polymorphism

Makes it possible to use the same code structure in different forms.

Goals

  • Better implementing inheritance
  • Code flexibility

Syntax

Static

myMethod() myMethod(int x) myMethod(int x, String y)

Dynamic

ParentClass.myMethod() ChildClass.myMethod()

Static Polymorphism (Method Overloading)

You can have several methods with the same name within a class. The number, names or types of their parameters must be different.

class Bird {
  public void fly() {
    System.put.println("The bird is flying.");
  }
  public void fly(int height) {
    System.put.println("The bird is flying " + height + "feet high.");
  }
  public void fly(String name, int height) {
    System.out.println("The " + name + " is flying " + height + " feet high.");
  }
}
class TestBirdStatic {
  public stat void main(String[] args) {
    Bird myBird = new Bird();

    myBird.fly();
    myBird.fly(1000);
    myBird.fly("Eagle", 1000);
  }
}

Dynamic Polymorphism (Method Overridding)

You can override methods of a parent class from its child class.

class Animal {
  public void eat() {
    System.out.println("This animal eats insects.");
  }
}
class Bird extends Animal {
  public void eat() {
    System.out.println("This bird eats seeds.");
  }
}
class TestBirdDynamic {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();
    myAnimal.eat();
    Bird myBird = new Bird();
    myBird.eat();
  }
}

The final part will look at Java’s built-in data structures.