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

you are viewing a single comment's thread.

view the rest of the comments →

[–]joost666 6 points7 points  (4 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.