Loading...

Postulate is the best way to take and share notes for classes, research, and other learning.

More info

Java is the king of OOP

Profile picture of Laura GaoLaura Gao
May 18, 2021Last updated Apr 30, 20226 min read



With its extensive variable typing declarations, variety of class & method types, and optionality between interfaces and classes, Java allows for code optimization and reusability beyond any other language I've used.

Suppose I want to make a CRM to keep track of advice I receive from each of my mentors, as well as scheduling when I should send thank you cards.

public class TksDirector
{
  private ArrayList<String> advices = new ArrayList<String>();
  
  public void recordAdvice(String advice)
  {
    advices.add(advice);
  }
}


public class Activate
{
  private ArrayList<String> advices = new ArrayList<String>();
  
  public void recordAdvice(String advice)
  {
    advices.add(advice);
  }
}

However, it feels pointless to reuse the advice method from both the activate class & the TKS director class.

The solution? With the extends keyword, Java allows some classes ("subclasses") to automatically inherit all the public methods/variables but not constructors or private methods/variables of another class (the "superclass").

// superclass declaration

public class Mentors
{
  private ArrayList<String> advices = new ArrayList<String>();
  
  public void recordAdvice(String advice)
  {
    advices.add(advice);
  }
}


// subclass declaration
public class TksDirector extends Mentors 
{
  // you can use the recordAdvice method here!!
}

But remember when I said --

automatically inherit all the public methods/variables but not constructors or private methods/variables

C'est quoi?? Let's break down each part.

Methods

TksDirector receives all the methods of mentors. But what if I want to do some special postprocessing of advice from my trusted directors? The king of OOP has a way to do that!! Through a process called overriding, use the super keyword to do the same thing that the superclass method does while adding something else.

// subclass declaration
public class TksDirector extends Mentors 
{  
  public void recordAdvice(String advice)
  {
    super.advice(advice);
    System.out.println("Remember to implement this advice!")
  }
}

I also want to add some extra methods that are unique to TKS directors, which, of course, the king of OOP also allows.

// subclass declaration
public class TksDirector extends Mentors 
{
  private String calendly;
  
  public void recordAdvice(String advice)
  {
    super.advice(advice);
    System.out.println("Remember to implement this advice!")
  }
  
  public String call()
  {
     return calendly;
  }
}

One problem: now that we have these 2 different advice methods, how does the program know which one to run?

This gets even further complicated when the types are not the same. The clearest way to illustrate this is with an example. Say I create an object like this:

Mentor harrison = new TksDirector();
harrison.recordAdvice("Tie your habits to your identity");

Does this object implement the method declared in mentor or in TKS director? This chart shows you:



For this case, at runtime is when which method to execute is determined. And runtime uses the TksDirector to determine. So thus we will be executing the TKS director implementation.

Also if we call harrison.call(); it will return an error because at compiling, mentor does not have the call method so it will error.

Yea. Java is wacky. But then again, so are all kings - just look at some European history.

Constructors

As a best practice, all subclasses should have its own constructor. If TksDirectors has the same variables as its superclass, just use super(); with the mentor parameters.

public class Mentors 
{
  private String name;
  public Mentors(String n)
  {
     name = n;
  }
}

public class TksDirector extends Mentors 
{
  public TksDirector(String n)
  {
     super(n);
  }
}

If you want to add variables unique to TksDirector:

public class Mentors 
{
  private String name;
  public Mentors(String n)
  {
     name = n;
  }
}


public class TksDirector extends Mentors 
{
  private String calendly;
  public TksDirector(String n, String c)
  {
     super(n);
     calendly = c;
  }
}

What if TksDirector has no constructor? Super will still be auto called.



If any class, subclass or super, does not have a constructor or if a private variable isn't defined in the constructor, the default object constructor is used. This is just a fancy way to say that:





---

Inheritance is the basic OOP functionality that other languages like JS and Python have. The relationship between a superclass and subclass is known as a is-a relationship. Because a TksDirector is-a mentor. This relationship is denoted by empty upward arrow, like this from an image I stole because I'm too lazy to draw my own:



But for Java, inheritance is only the beginning.

What if I don't want to make mentor objects without it being an activate or a TksDirector?

Just add one word to turn mentor into an abstract class:

public abstract class Mentors
{
  //...
}

Now, differences from regular class:

  1. Cannot be instantiated
  2. Can have abstract methods ( = methods that aren't defined) -> each must be overriden by ever class after

Syntax for abstract methods:

public abstract double area (double sideLength);
//  if no abstract keyword then compile error

When I first learned about this, this felt very useless to declare abstract methods because all of them must be overriden anyway. You're not reusing any method implementations.

However, the purpose is to declare types. To set a template on all the thank you note methods. The purpose of abstract method is to template what outputs are allowed. So you can know that mentor thank you note and activate thank you note both return a string.

I want all walking objects to have the walk() method, but it doesn't feel make very much sense to put a Boston Dynamics robot and a bird and Harrison all in the same class.

Well fret no more, as king of OOP, Java has a way to tackle all yer issues.

public interface WalkingObject
{
  // declare methods
}

public class Bird implements WalkingObject
{
  // override methods
}

All interface methods are public and abstract by default, so to declare a method you don't need keywords public and default.

void walk();

The cool thing about interfaces is that each class can implement as many interfaces as it wants, while each class can only have a maximum of one superclass.

public class Bird implements WalkingObject, FlyingObject
{
  // override methods
}

And that is all the functionality of the king of OOP! To summarize, you can have

  • Regular inheritance with superclass and subclasses

  • Abstract classes where the superclass cannot be instantiated

  • Interfaces


Comments (loading...)

Sign in to comment

Dev/CS

Builders gonna build baby