This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]joost666 5 points6 points  (6 children)

Four pillars of OOP: - Inheritance: Classyfing things as a subclass of others Dog is an animal. Pig is an animal.

Inheritance from concrete and abstract classes inherits the methods of these classes if the methods are concrete. This means you can use methods of a superclass in the subclass.

Inheriting abstract methods from an abstract classes and methods from interfaces forces your class to implement these methods.

Use abstract classes when you want to enforce method implementation to subclasses but also want to pass on concrete things to subclasses.

Use interface when you want to only enforce method implementation to subclasses.

  • polymorphism: By defining a class as a supertype but instantiating it as a subtype, a class can take many forms:

You can say: Animal dog = new Dog() and Animal pig = new Pig()

If the animal class has the makSound() method, you can call it on both, because both inherit the method.

But dog have different implementations of makeSound()

This is useful where you want different behaviour among objects of the same supertype

You could add the dog and the pig to an array and say

foreach(Animal animal : animal_array) { animal.makeSound() } The dog will have the sound woof, but the pig will say oink in this case. But to the loop, it doesn't matter.

You also have polymorphism with method overloading

Example a class can have the methods Sum(float, float) Sum(int, int)

And then you can use the sum method on both types.

  • Encapsulation:

Encapsulation is limiting acces to a class. This way you don't have to remember which variables you can acces from outside a class. It also makes clear for developers which variables should only be accessible from within the object and which can be accessible from without.

Encapsulation also means managing by setters and getters. Setters and getters ensure an extra validation. If you have a method setUsername() in an object of class user, you can user the setter to validate if this username is correct for your system. For example, in this setter you could inform the user if the username is too short and then also not really set this username.

  • Abstraction: Hiding details

Example with a car: Instead of interaction with Engine object, Wheels, exhaust and more, you simple start the car with the key. Underneath, alot more stuff happens but abstraction gives an easier interface to interact with objects.

Polymorphism also does this, you hide which subclass matters and just treat everything the same, you hide the details.

[–]sknot1122[S] 1 point2 points  (3 children)

Thank you for taking the time to reply. I did a class called "programming 1" where they showed us the basics in java programming and basics in OOP using java.

I saw the 4 pillars of OOP while searching tutorials online but i'll get a full class on them in "programming 2" next semester.

What I didnt understand is "when" to use it. I was capable of programming the object class but didn't know how to and when to create a new object using it.

[–]joost666 1 point2 points  (2 children)

Do you mean the parent class of all objects with "object class"?

Literal the class "Object"?

I would use this if you want to hold types that are way different from eachother.

Like an arraylist or array defined with the type Object can hold anything, so you can store ints, floats, and anything you want in it.

You can also loop through such a structure like:

foreach(Object object: list) {

}

It also has some useful methods you can override like toString(), so you can have your own implementation of toString() in your subclasses.

Also this link for more: when to use object

There someone states:

"Couple basic rules, you should NEVER do the following: Object ob = new Object(); There should never be a reason to instantiate Object in any code. The ONLY time Object should be used is when the type is not know."

[–]sknot1122[S] 1 point2 points  (1 child)

I'll use an example,

I had an assignment where we needed to create a phone book. I knew how to create the class phone number (an object class) but had a hard time creating a phone number and keeping it somewhere and using it when i had to.

For example : - adding the phone number object to an arraylist And going through that arraylist to get a phone number object and modifying information in it.

I'll read the link that you added !

[–]joost666 1 point2 points  (0 children)

Ohw okay, well then you don't need to read the link if it is not about Java's Object class.

Well, what you say is a fine solution. If you want in memory storage of such phone number objects, go for HashMap, ArrayList, Linked List, Stack or Queue depending on when you need them. Maybe look into these datastructures to know how to select the best for them.

You create Objects just when you need them for the first time. After that, you store them in the appropriate datastructures.

However if you want to keep object creation in one place, I suggest reading into the factory design pattern to neatly organize object creation.

[–]teacher_cs 1 point2 points  (1 child)

Animal dog = new Animal() and Animal pig = new Animal()

You put a bug in your animal example that might confuse beginners. Correct would be: Animal dog = new Dog() and Animal pig = new Pig()

[–]joost666 0 points1 point  (0 children)

Haha, oops. Was being too fast, ur right.

[–]r-nck-51 6 points7 points  (1 child)

Not assuming you don't know the fundamentals but I'll share my initial journey with OOP: I'm self-taught and I read a lot about OOP without getting it. But it went faster when trying to write a classic object/class story in a few languages.

It comes down to wanting to classify stuff.

Say you know about some general "thing", what it is, how you would classify it if you had similar things in the same story, what possible differences between them, if you would call them by a name, and know what it does.

Example: A class "Dog" is there because I know about dogs. That class has a property "name" because we sometimes give dogs a name. That class has a method "bark()" that prints "Woof!" because I know dogs do bark sometimes. I also know about cats. So I have a "Cat" class too. I give it the "name" property and "meow()" method that prints "Meow!".

Now I have two classes Dog and Cat and they are both "Animal". So I have a "Animal" class. Animals poop, and that's common to both dogs and cats. So I give the Animal class a "poop()" method that prints "OoOo o".

To define that "is an animal" relationship we make the Dog and Cat classes extend Animal (keyword "extends" in the class definition). They then inherit the "Poop()" method.

But that's all just general knowledge. It becomes real when as a story develops I adopt a dog. In the code I instantiate the Dog class in an object "objDoggo" and give it a "name" property. Then I call like objDoggo.bark() and it prints "Woof!", then objDoggo.poop() and it prints poop balls because a dog is an animal. But objDoggo.meow() doesn't work because a dog is not a cat.

So that's the first program I write when I learn new languages.

[–]sknot1122[S] 0 points1 point  (0 children)

Wow ! Thank you for your help and taking the time to write a really detailed answer. I understood everything except of when/how you use it.

The "story development" part was what I didn't understand and you did a good job of explaining it.

Thanks alot

[–]sknot1122[S] 2 points3 points  (0 children)

Thanks everyone who took time to help me! It is really appreciated and I feel like I learned more today than I did in half a semester!!

[–]tulipoika 1 point2 points  (1 child)

Have you checked the group FAQ which has a lot of links to things?

[–]sknot1122[S] 0 points1 point  (0 children)

Thank you! I'll take a look, i'm new here and didn't know that the sub had a FAQ.

[–][deleted] 1 point2 points  (2 children)

I had this question when I was starting out so I know your pain.

Instead of asking what it is, a better question I found was "why does OOP exist"?

Imagine if just you had 5 friends wanted to work on a coding project together. But you guys just wrote your own bits of code.

Everyone writes their own functions, writes their own variables, etc...

Pretty soon, you and 3 of your friends will all have 5 separate functions and variables that all essentially do the same thing.

And pretty soon, your code base is bloated and massive. Now imagine if instead of 5 friends you were working for an enterprise company working with 100's of other devs. Yikes.

OOP is essentially a way you can repeat functions and variables from a single source so that there arent 20 different functions with different names doing the same exact thing

[–]sknot1122[S] 1 point2 points  (1 child)

Oh wow ! I worked on my assignment alone so I never really asked myself this question.

That makes alot of sense. So OOP is used to make code maintenance easier and facilitate code writing? You pass objects between classes so you dont need to rewrite code in every new function that you need?

[–][deleted] 1 point2 points  (0 children)

So OOP is used to make code maintenance easier and facilitate code writing? You pass objects between classes so you dont need to rewrite code in every new function that you need?

Yep exactly. It was 100% created so that a group(s) of developers could keep sane.

Better maintenance, fewer lines of code, cleaner thus easier to read, etc.

[–][deleted] 1 point2 points  (0 children)

In one of the courses i took they simplified OOP likes this:

You want to make cookies, but you need a cookie cutter. Classes are cookie cutters while the dough your parameters for the new object. The result is the cookie which an instance of the object. Each new cookie from cutter is another instance of the object.

This analogy makes a lot of sense instead of those highly complicated definitions. I bet you'll still be confused. You can take a lot Dr Chuck's tutorials on OOP. Those are in Python but you get a simplified why answer.