Object-Oriented Java

Access modifiers in Java:

public int everyoneCanUse = 333;

protected String classAndSubclass = “not for everyone”;

private double onlyMe = 222.2222;

Java classes:

In addition to your Main.java driver class, you will need to create a separate file.

Here is an example of a basic Potato class:

public class Potato{

String typeOfPotato = “default”;

String name = “default”;

int weight = 1;

public void grow(){

weight++;

System.out.printf(“The potato called %s has grown\n”,

name);

}

}

And here’s what your main class should look like to create and interact with objects of the Potato class:

public class Main{

public static void main(String[] args){

Potato myPotato = new Potato();

myPotato.grow();

}

}

Java constructors:

Here’s a revised class example, this time with a constructor for the Potato class, and no default values. Instead, it relies on values passed to it when it’s created in the main class.

Here’s the new Potato class with a single constructor:

public class Potato{

String typeOfPotato;

String name;

int weight;

public Potato(String typeOfPotato, String name, int weight) {

this.typeOfPotato = typeOfPotato;

this.name = name;

this.weight = weight;

}

public void grow(){

weight++;

System.out.printf(“The potato called %s has grown\n”,

name);

}

}

And here is the new Main.java:

public class Main{

public static void main(String[] args){

Potato myPotato = new Potato(“Idaho”, “Joe”, 123);

myPotato.grow();

}

}

Java inheritance:

Now let’s make a new type of potato. It’s a more specific kind of potato, called a sweet potato.

It will have everything that a regular potato has, but it has some new stuff, like sweetness. It uses the existing Potato.java above, but it will have its own class called SweetPotato, and the main method will be slightly different too.

public class SweetPotato extends Potato {

int sweetness;

public SweetPotato(String typeOfPotato,

String name, int weight, int sweetness) {

super(typeOfPotato, name, weight);

this.sweetness = sweetness;

}

public void tasteTest() {

if (sweetness >= 3) {

System.out.println(“This potato is nice and sweet”);

} else {

System.out.println(“Bitter for a sweet potato”);

}

}

}

And here is the new object that is being created in the main method of the Main class:

public class Main{

public static void main(String[] args){

SweetPotato potat = new SweetPotato(“Cool”, “Alice”, 55, 3);

potat.grow();

potat.tasteTest();

}

}

Java abstract classes (classes which can’t be instantiated and can only be subclassed):

public abstract class Car

{

int FUEL_CAPACITY;

int horsePower;

int numberOfDoors;

int weight;

int fuel;

int currentSpeed;

int price;

String make;

String model;

public void accelerate(int amount) {

this.currentSpeed += amount;

this.fuel -= amount;

}

public void brake(int amount) {

this.currentSpeed -= amount;

}

}

There is no car that is only called a car. There is no animal that is only called mammal. In these cases, you need to have a more specific version of it. All the specific versions can have similar traits, inherited from an abstract superclass, also known as a base class.

An abstract class isn’t very useful on its own. It is usually used with a subclass that extends it:

public class NissanSentra extends Car {

//blank, just for class demo

}

Java interfaces:

Here is an interface for plant formula. It makes sweet potatoes grow faster:

public interface PlantFormula

{

public void superGrow();

}

The SweetPotato class needs to implement the abstract method in the interface. Notice @Override and superGrow():

public class SweetPotato extends Potato implements PlantFormula {

int sweetness;

@Override

public void superGrow() {

weight += 10;

System.out.printf(“Wow! %s grew a lot!”, name);

}

public SweetPotato(String typeOfPotato,

String name, int weight, int sweetness) {

super(typeOfPotato, name, weight);

this.sweetness = sweetness;

}

public void tasteTest() {

if (sweetness >= 3) {

System.out.println(“This potato is nice and sweet”);

} else {

System.out.println(“Bitter for a sweet potato”);

}

}

}

Why use an interface instead of inheritance? Well, a sweet potato has plant formula, but it is not a type of plant formula. However, it is a type of potato, so that’s why it’s a subclass of the Potato superclass.

Java new:

When creating a new instance of a class, you need to use the new keyword:

SweetPotato potat = new SweetPotato(“Cool”, “Alice”, 55, 3);

Enums in Java (like a class, must be declared in a separate file outside of the Main class, such as Main.java and a separate SomeEnumName.java):

public enum Mood{

HAPPY, MAD, SAD, BORED

}

And another for the weather in Weather.java:

public enum Weather {

RAIN, HAIL, SNOW, HOT, COOL, COLD

}

Then, in your Main.java’s Main class’s main method, do this:

Mood myMood = Mood.HAPPY;

Weather currentWeather = Weather.RAIN;

if (currentWeather == Weather.RAIN) {

myMood = Mood.SAD;

}

System.out.println(myMood);

In some cases, you can get away with using strings instead of enums, but enums are good because they constrain you to a fixed list of constant values, whereas a string can be anything.

← Previous | Next →

Java Topic List

Main Topic List

Leave a Reply

Your email address will not be published. Required fields are marked *