Tino Intro To Java

Last modified: January 07, 2024

Object Oriented Programming

In Your First Java Program, you wrote:

public class HelloWorld {
    public static void main(String[] args){
        System.out.println("Hello, world!");
    }
}

But what does "public class HelloWorld" do? Why can't we just write System.out.println("Hello, world!")?

OOP and Java

Unlike JavaScript, every piece of code in Java needs to be a part of a class.

A "class" is a blueprint outlining related methods, variables, and other data.

Some classes can directly perform actions, such as the HelloWorld class you defined above, which has a main method that can be called directly on the class. The main method is a special method that serves as the starting point for Java programs, similar to the start() function in CodeHS. Methods you can call directly on a class are called static methods and you will learn more about them later. They work the same way functions like Randomizer.nextInt(a, b) or Math.abs(n) work in Javascript.

For many classes, you will need to create an instance of the class before using it. You have seen this before in CodeHS, where they defined classes such as Rectangle and Circle. To use those classes, you had to first create an instance of the class by writing something like

var cir = new Circle(5);

After that, you could call functions on cir such as cir.getX() or cir.getY(). In Java, you will also create instances of classes and call methods on them in a very similar fashion. Even the syntax is very similar. In Java, if you defined a Circle class, you might create an instance of a Circle by writing

Circle cir = new Circle(5);

An instance of a class is called an object. In the example above, cir is an object because it is an instance of the Circle class.

Defining classes and creating objects is a programming practice called "Object-Oriented Programming" (acronym: OOP)

Remember the acronym OOP. It will be referenced in many places.

OOP Real Life Analogy and Inheritance

One benefit of OOP is inheritance and hierarchy, which allow you to create similar classes and objects that are logically similar to one another without having to write repeated code.

Here is an example:

You might write code for the three separate objects shown below:

Bird Worm Garden

One of the most powerful aspects of object oriented programming is inheritence. Inheritence means you can take any object and derive a new object, called a child class or subclass, which inherits all the capabilities of its parent class or superclass. Subclasses typically extend general behavior to be more specific and sometimes modify the default behavior inherited from the parent class. This feature is extremely useful because it means you can reuse old code and extend it to new applications without modifying the old code.

Conceptually, a child class typically models a more specific type of its parent class. For example, an Airplane and a Car are both specific types of Vehicles, so it would make sense for Car and Airplane to be subclasses (children) of the Vehicle class. That would mean the Vehicle class would be the parent class of both Car and Airplane. It would not make sense for grass to be a subclass of Vehicle because grass is not a type of vehicle.

Let's add some relationships to the Bird, Worm, Garden concept:

Extended Bird Worm Garden

The diagram indicates

  • A Bird and Worm are specialized kinds of Animals
  • A Bird and Worm can do everything an Animal can, but with more functionality specific to their type
  • A Sparrow is a specialized kind of Bird
  • A Sparrow can do everything a Bird and Animal can, but with more functionality or possibly with modified functionality inherited from Bird
  • A Garden contains Birds and Worms (indicated by dotted lines)

Dark Mode

Outline