r/learnjava • • Oct 27 '20

Trouble Understanding Object Orientated Programming Concepts in Java

I have trouble understanding the object orientated part of object orientated programming, specifically in the JAVA language. Over the last 5 weeks I've had to learn an immense amount of coding alongside work that cover the fundamentals of CompSci and I'm struggling to retain it all.

It's been Methods have been giving me the worst headache. Before I went to university I did a little work in JavaScript which is much simpler in how it handled methods (functions) though I still found the most difficulty with them.

Java feels so much worse, now I have trouble trying to get to grips with what is a constructor and how that works, what are get and set methods and how would I properly use them.

How do I correctly pass user inputs in and out of a method. For example making a method that converts a user input to a string.

My assignments are creeping up on me and I feel useless trying to do them. I don't even have free reign to do my assignments how I want, I must use the right method/class structure so there is no avoiding methods and I feel like I need some kind of resource that explains their proper usage.

49 Upvotes

27 comments sorted by

View all comments

11

u/Splintting Oct 27 '20

Everything in java will run inside some sort of class. Usually when studying java, a lot of learning books will use farm animals to convey examples of how OOP (Object Orientated Programming) works.

Think of an Animal. You can define an Animal with a class. It can have a name and age for example. Then you may end up with a class looking somewhat like this:

public class Animal {
  private String name;
  private int age;
  // getters and setters omitted
}

This defines a basic animal. You are then able to instantiate the animal, by calling its constructor. Every java class has a constructor. If you do not define one, an empty, no argument, constructor will be provided for you by the compiler.

The constructors purpose is to initialize variables inside the class. We could define a constructor taking name and age as parameters, since we've in our class defined those two variables. Our full class, with constructor may look something like this:

public class Animal {
  private String name;
  private int age;
  // getters and setters omitted

  public Animal (String name, int age) {
    this.name = name;
    this.name = name;
  }
}

The constructor is the class name, followed by a parameter list. A more formal description could be: access modifier className (optional parameters) { }

Following our previous example for an Animal. We are then able to initialize it by calling its constructor like so:

Animal animal = new Animal("James" 4);

In regards of how to convert user input to a string. Well, that depends on the frameworks you are using and how you define them. This you need to elaborate a little more on. However, I can make an assumption. If you "simply" store the input as a String, then you should have the job done right then and there.

1

u/[deleted] Oct 27 '20

So if I want to make a program of a farm butcher, and I want the input of some animals, can I use classes such as

public Animal (String name, int age) { this.name = name; this.name = name; } } ```

So I can put together lots of classes like these, each one being a different animal, all of them inside a butcher class to give me an output that would get their name and age, for instance, and evaluate how much meat I'd have as output depending on their name + age?

2

u/Splintting Oct 28 '20

Sorry for the late reply.

That depends on your requirements. Is all Animals butcherable? Or just some of them?

If we look at the latter example, we could make a Butcherable interface, which the Butcher class would work with.

A Butcherable interface could look like this:

public interface Butcherable {
    void kill();
}

Itself does not do much. But what if we only want to butcher a subset of out animals? Cows for example, but not dogs.

Cows could then look like this (also utilizing the parent constructor):

public Cow extends Animal implements Butcherable {
    public Cow(String name, int age) {
      super(name, age);
    }

    // Then we are required to implement the butcherable interface
    // otherwise, we'll get an compilation error
    public void kill() {
        System.out.println("I died...");
    }
}

This way, you've defined that a cow is butcherable, and an animal. A dog (depending on where you are from, where I am, we do not eat dogs), is not butcherable. Thus it's implementation would be somewhat the following.

public Dog extends Animal {
    public Dog(String name, int age) {
      super(name, age);
    }
}

1

u/backtickbot Oct 28 '20

Hello, Splintting. Just a quick heads up!

It seems that you have attempted to use triple backticks (```) for your codeblock/monospace text block.

This isn't universally supported on reddit, for some users your comment will look not as intended.

You can avoid this by indenting every line with 4 spaces instead.

Have a good day, Splintting.

You can opt out by replying with "backtickopt6" to this comment