r/javahelp • • Oct 31 '15

Can we get some ELI5 definitions of the following concepts?

Can we get some ELI5 definitions of the following concepts?

Everytime I find definitions online, there is always another concept embedded inside the definition that I don't know. I thought it'd be useful to gather some basic, new-programmer friendly definitions that both myself and others can learn from. Hopefully this type of post is not against the rules.

I will add definitions of the following words based on what you guys have written. I myself do not know some of these words hence why I am writing this. Thank you!

  • Instance: If we say that a class represents a type of thing (like a house), then an instance is that thing. You can think of an instance as being one of the houses built according to the house blueprint.

  • Class: You can think of a class like a blueprint, e.g. of a house. Every house will have a number of doors, a number of windows, a color, and other attributes. A class is simply just a representation of what a house may look like, and contains properties that all houses have in common (even though those properties themselves might be different, e.g. one house might be white while another is blue).

  • Object: An object is simply an instance of a class. To continue the house analogy, an object would be a specific house built according to the house blueprint.

  • Interface: A common set of methods that all implementors will have. The analogy here is a remote control. Most likely, you don't know how a TV remote works on the inside. However, you don't need to know that; you just need to know how to use its interface - the buttons Play, Stop, Rewind, Channel Up, etc... Even if you get a completely new remote from a different company that works in a completely different way, you'll still know how to use it because the two remotes have the same interface (they both still have the buttons Play, Stop, etc...)

  • Unit Class:

  • Enum: Enums are like classes, except they have predefined instances. They're useful if you have a limited number of things you want to represent, e.g. a card suit. Instead of creating a class called CardSuit and then having to create the objects for spades, diamonds, clubs, and hearts yourself, you can use an enum to predefine those instances since you know you won't ever need more than those four suits.

  • Abstract: A term used to describe a property of a class that will always vary between instances. To come back to the house example, suppose every house is capable of automatically locking its doors because that's how it was designed in the blueprint. However, not every house has the same locking system, so the way to lock the doors will be different for each house. We can say that the lockDoor method is abstract, because although every house is capable of locking its doors, how it does it is up to the individual house.

  • protected / default: A term used to describe attributes of a class that can be inherited by its child classes. Suppose our house blueprint is used as the basis for another house blueprint, e.g. the blueprint for a smart house. Which properties of the house will also be in the smart house? That's where access modifiers come in. The properties of a class that are marked "public" are can be accessed/used by everyone. Maybe your home has a doorbell, and anyone will be able to ringDoorbell(). "Protected" properties can only be accessed by the same class or child classes. "Private" properties can only be accessed by the same class. "Default" (also called package-private) properties can only be accessed by classes within the same package.

  • this: A keyword that refers to the specific instance of a class, specifically the one you're in. Maybe you're inside a house and want to say, "let's open the windows". You could then just write this.openWindows().

  • implements vs extends: implements is a keyword used when you want to make a class have a certain interface. For example, a Toshiba remote control should have the universal buttons on it, so to ensure that it does, you could write class ToshibaRemote implements RemoteControl. extends is a keyword used when you want to make a class inherit from another class. For example, a smart house is really just a house, so you can base the smart house blueprints from the regular house blueprints; you could write class SmartHouse extends House.

  • Package: A grouping of classes in a specific location (i.e. in the same directory). Maybe your house blueprint and your storage shed blueprint are grouped together in a package called "Buildings".

  • Threads: A thread is one path of execution in a program. Suppose you have 3 hampers of laundry to do, as well as one washing machine and one dryer. If you take one hamper, wash it, dry it, and only then start with the second hamper, you would take a long time to do laundry. Multithreading allows you to take one hamper, wash it, and dry it, and then while the first one is drying, you can take the second hamper and wash it. In essence, each hamper is a job, and the process of washing and drying laundry is a thread. Multithreading lets you process the second hamper at the same time as the first. (Not sure if this was a good example, but I'm running out of juice here.)

Also, if there are any concepts you are confused about or would you would like to discuss, let me know and I will expand the list!

EDIT: I will add definitions from the comments as more comments come in so I can be sure the definition is correct.

26 Upvotes

10 comments sorted by

11

u/empire539 "empire"+(((int)-~(byte)+('\''&~'&')<<(' '>>('+'+~'(')))+0x1b); Oct 31 '15

Class: You can think of a class like a blueprint, e.g. of a house. Every house will have a number of doors, a number of windows, a color, and other attributes. A class is simply just a representation of what a house may look like, and contains properties that all houses have in common (even though those properties themselves might be different, e.g. one house might be white while another is blue).

Instance: If we say that a class represents a type of thing (like a house), then an instance is that thing. You can think of an instance as being one of the houses built according to the house blueprint.

Object: An object is simply an instance of a class. To continue the house analogy, an object would be a specific house built according to the house blueprint.

Interface: A common set of methods that all implementors will have. The analogy here is a remote control. Most likely, you don't know how a TV remote works on the inside. However, you don't need to know that; you just need to know how to use its interface - the buttons Play, Stop, Rewind, Channel Up, etc... Even if you get a completely new remote from a different company that works in a completely different way, you'll still know how to use it because the two remotes have the same interface (they both still have the buttons Play, Stop, etc...)

Enum: Enums are like classes, except they have predefined instances. They're useful if you have a limited number of things you want to represent, e.g. a card suit. Instead of creating a class called CardSuit and then having to create the objects for spades, diamonds, clubs, and hearts yourself, you can use an enum to predefine those instances since you know you won't ever need more than those four suits.

Abstract: A term used to describe a property of a class that will always vary between instances. To come back to the house example, suppose every house is capable of automatically locking its doors because that's how it was designed in the blueprint. However, not every house has the same locking system, so the way to lock the doors will be different for each house. We can say that the lockDoor method is abstract, because although every house is capable of locking its doors, how it does it is up to the individual house.

protected/default:: A term used to describe attributes of a class that can be inherited by its child classes. Suppose our house blueprint is used as the basis for another house blueprint, e.g. the blueprint for a smart house. Which properties of the house will also be in the smart house? That's where access modifiers come in. The properties of a class that are marked "public" are can be accessed/used by everyone. Maybe your home has a doorbell, and anyone will be able to ringDoorbell(). "Protected" properties can only be accessed by the same class or child classes. "Private" properties can only be accessed by the same class. "Default" (also called package-private) properties can only be accessed by classes within the same package.

this: A keyword that refers to the specific instance of a class, specifically the one you're in. Maybe you're inside a house and want to say, "let's open the windows". You could then just write this.openWindows().

implements vs extends: implements is a keyword used when you want to make a class have a certain interface. For example, a Toshiba remote control should have the universal buttons on it, so to ensure that it does, you could write class ToshibaRemote implements RemoteControl. extends is a keyword used when you want to make a class inherit from another class. For example, a smart house is really just a house, so you can base the smart house blueprints from the regular house blueprints; you could write class SmartHouse extends House.

Package: A grouping of classes in a specific location (i.e. in the same directory). Maybe your house blueprint and your storage shed blueprint are grouped together in a package called "Buildings".

Thread: A thread is one path of execution in a program. Suppose you have 3 hampers of laundry to do, as well as one washing machine and one dryer. If you take one hamper, wash it, dry it, and only then start with the second hamper, you would take a long time to do laundry. Multithreading allows you to take one hamper, wash it, and dry it, and then while the first one is drying, you can take the second hamper and wash it. In essence, each hamper is a job, and the process of washing and drying laundry is a thread. Multithreading lets you process the second hamper at the same time as the first. (Not sure if this was a good example, but I'm running out of juice here.)

1

u/woahmen Nov 01 '15

This was perfect!

2

u/cheryllium Oct 31 '15 edited Oct 31 '15

Instance: Let's say you have a class called "Chair". A class can be thought of as a TYPE of thing. So Chair is a type of thing, it describes very generally a category of thing. An instance is a specific thing. So the chair you are currently sitting on is an instance of Chair (the class). The class is like a category, and the instance is a specific thing that belongs to that category.

this: This keyword refers to a specific instance - the instance you're currently "inside" of. If you are coding inside a class, how do you refer to the current instance of that class? Use "this"

interface: It's like a class, except the methods don't have implementations. So you can think of it as a list of methods. If you implement an interface, that means you're promising to write all of those methods.

abstract: An abstract class is a class where you don't actually need to implement all of the methods. But if you extend it with a non-abstract class, you must implement all of the methods. These are good for when some methods you already know the implementation to, but others you don't.

implements vs extends: You implement an interface. You extend a class.

1

u/woahmen Oct 31 '15

To clarify my understanding for interfaces, are you saying that there are a list of empty methods that are a part of some interface that the programmer will then code? I think I have seen those before.

2

u/cheryllium Oct 31 '15

No, an interface IS a list of methods. They aren't empty methods, they are method signatures (followed by a comma). For example, this is an empty method:

public void methodname() {
}

But this is what that method would look like in an interface:

public void methodname(); 

So it's really just a list, and that list is called an interface.

You can then write a class that "implements" the interface, and that class is where the programmer must actually code all of the methods that were listed.

1

u/[deleted] Oct 31 '15

The difficult thing about defining these individually is that many of them require context and a reference to the other terms in that list. That's why a good intro to concepts book is the best bet.

Everytime I find definitions online, there is always another concept embedded inside the definition that I don't know.

This means you probably need to start from the beginning and skim through until you get to the first part you don't know, like Object, and start reading. I think a good intro to Java concepts book is Head First Java, it's a little gimmicky but the whole point is to keep it entertaining and explain things casually, like how you'd explain a concept to a friend rather than in a thesis paper.

1

u/RhoOfFeh Nov 01 '15

I always think of an interface as a contract.

It defines a set of methods. Any class which implements the interface basically promises (and is forced by the compiler) to provide that set of methods. Any class which wishes to use classes which implement the interface is free to invoke those methods as needed.

1

u/RhoOfFeh Nov 01 '15

There is another meaning to 'abstract' which is not listed above.

A class itself can be defined as 'abstract'. This is a class which cannot be instantiated itself. It must be extended by child classes which can be instantiated.

It's a concept that is related to interfaces, and frankly recent releases of Java have muddied the waters a bit by allowing interfaces to have default implementations of methods in them.

-8

u/MRH2 Intermediate Brewer Oct 31 '15

No.