Am I the only one struggling to comprehend OOP?


#1



https://www.codecademy.com/en/courses/learn-java/lessons/object-oriented-programming/exercises/generalizations-oop-java


Okay, I seem to be struggling to comprehend the theories in Java, and I was hoping someone could help me understand it a bit. Codecademy has this really bad problem in showing you how to write coding, but not how to think critically and understand it - until of course, they throw you in the deep end with no hints at all.

So, here I am trying to comprehend what the heck is going on in the final lesson of the unit, again.

My question relates to the

Coffee myOrder = new Coffee();

portion of the coding below. What exactly does this mean?

I don't understand why Coffee is written twice in the line of code, can anyone explain that to me? What does each instance of writing "Coffee" in the line above translate to?


class Coffee extends Beverage {
	
	public Coffee() {

	}
	
	public void addSugar(int cubes) {

		System.out.println("You added " + cubes + " sugar cubes.");

	}

	public static void main(String[] args) {
    
    Coffee myOrder = new Coffee();

	}
}


#2

Once to create a variable of a certain type and once for creating a value of that type.
That line does two things and both involve a type.

Consider:

int a = 5;
int b = new Integer(3);

Assigning an Integer to an int variable will unbox it.


#3

I don't understand what you're saying.

You're saying the first 'Coffee' is to create a 'variable' of a certain 'type'... but isn't that the purpose of myOrder? Doesn't myOrder describe that we are making a new 'variable.' Also, wouldn't writing 'int' be describing the 'type' of 'variable?'

Also, isn't the parenthesis behind the second 'Coffee' the area in the coding where you can indicate a 'value' of the 'variable?'

I'm asking what do the 'Coffee' words themselves mean, not what does the line of code do as a whole.

I understand that
Coffee myOrder
is the area which says create a variable under coffee
and
= new Coffee();
is an area to insert a value for the variable.

I just don't understand why 'Coffee' itself is mentioned twice.


#4

Declare a varible named myOrder, with the type Coffee:

Coffee myOrder;

Create an instance of Coffee:

new Coffee()

Do both:

Coffee myOrder = new Coffee();

Both parts require a type. So, twice.

And in the example with int and Integer, or also when for example the type of a variable is a parent class to the value it refers to, then two different types would be mentioned.

class Animal {}
class Zebra extends Animal {}

Animal myAnimal = new Zebra();

#5

Haha! So much to wrap your head around. I really appreciate you taking the time to help me!

I think I get it...

So, basically 1st 'Coffee' is the type of variable being created, simply; myOrder names the variable; new allocates memory for a new object; and then writing 'Coffee' again is required to determine the rest of the rules for the object, with parameters for different respective fields being within the parenthesis divided by commas.

So, in your Animal/Zebra example.

You're basically saying...
Using the data type defined in animal class create a variable named myAnimal... we will create memory for a new object which follows the descriptions/rules within the Zebra class (which is just one of the many classes that may be within the Animal class, all with different fields.)

Sorry, I just feel the best way to learn programing is to translate it into plain English terms - translate the definitions. For example, creating an instance can also be called creating an object.

Am I getting this correctly? Or am I off base?


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.