Skip to content

Instantly share code, notes, and snippets.

@idodin
Last active January 22, 2021 14:33
Show Gist options
  • Save idodin/444d2c05b3d7d699be961e7c3e47a58b to your computer and use it in GitHub Desktop.
Save idodin/444d2c05b3d7d699be961e7c3e47a58b to your computer and use it in GitHub Desktop.
OOP Basics

Inheritance

We want to avoid reusing things in code, Inheritance allows us to extend a class definition, keeping everything that was defined in a parent class and optionally adding some more fields / methods to a child class.

class Dog {
  private int age;
  private int weight; 
  private String color;
  private String name; 
  
  
  public Dog(int age, int weight, String color, String name) { 
    this.age = age;
    this.weight = weight;
    this.color = color;
    this.name = name;
  }
  
  public int getAge(){
    return this.age
  }
  
  public void setAge(int age){
    this.age = age
  }
  // etc... 
}
class ShowDog extends Dog {
  private int ranking 
  
  public ShowDog(int age, int weight, String color, String name, int ranking) { 
    super(age, weight, color, name);
    this.ranking = ranking;
  }
  
  public int getRanking(){
    return this.ranking;
  }
  
  public void setRanking(int ranking){
    this.ranking = ranking;
  }
}

Some notes on inheritance that trip people up:

  • super(...) is a method used to call the constructor of your direct parent. If you are adding an extra parameter,you have to first initialize the parameters of the parent class and then initialize the extra parameter that only exists in the child. The super has to come first in the constructor.
  • private variables can't be accessed by the child class. Make sure to use setters and getters!

Introduction to Object Oriented Programming in Java

OOP is one of the key paradigms of modern software development. This acts as a cheat sheet for some of the key feature of Object Oriented Programming with examples in Java and short explanataions / links as relevant.

Useful Links

The Class

A Class is a recipe for creating an Object - a grouping of variables (fields) and procedures (methods) that constitute a class. These classes are typically defined in their own files, each named for the class that makes them.

For example, consider a class that defines a Labrador Dog:

Labrador.java

class Labrador { 
  private int age;
  private int weight; 
  private String name;
  private String color;

  public Labrador(int age, int weight, String name, String color) {
    this.age = age;
    this.weight = weight; 
    this.name = name;
    this.color = color;
  }

  public void sayHello(){
    System.out.println("Hello my name is " + name + " and I'm a " + color + " Labrador!");
  }
}

We can instantiate (make) an object of this class and use the sayHello method like this:

Main.java

class Main {
  public static void main(String[] args) {
    Labrador myLabrador = new Labrador(12, 24, "Squiggles", "Brown");

    myLabrador.sayHello();
  }
}

Fields

Fields can be one of the Java primitive types:

  • short
  • int
  • long
  • float
  • double
  • byte
  • boolean
  • char

Or another Object including String (which is an Object in Java)

Above we use int and String.

Fields also have a visibility which is defined using a modifier which is one of the following (which are visible by other files as shown in the table below):

Visibility Modifier Summary

Methods

Methods are procedures that can be done on the Object.

Methods are defined using a visibility modifier, output type, name and list of inputs with their associated types. The output type can optionally be void which indicates that the method provides no output. Above we have a method that is public, provides no output, is called sayHello and takes no inputs. It prints out a short message to users.

We could define another method:

private double convertToPounds(int kilos1, int kilos2) {
  return (kilos1 + kilos2) * 2.2;
}

This method takes two weights as integers, sums them and converts them to pounds, returning the result as a double.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment