-6

So someone tell me if i'm correct or not.

Encapsulation is Data hiding, allowing yourself only to view the attributes and othering methods in a class privately, while you could you use these methods and abbritures in other classes,

Inheritance is extending a class, like taking some of the methods in the “super class” and pass it in “child class” and modify it or use it there.

Polymorphism is the same thing as inheritance but it's just formatted differently, like if i had an animal class, every animal has a different sound so, from there I would have something like this

Animal cat = new Cat();

overriding & overloading I’m not sure about this one

Abstract classes is taking methods or variables from the super class and pass those methods and variables as “Abstract” so that in the sub class you modify them and edit them.

Does that make sense? Or I misunderstood something?

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Here is some info on method overloading. The basic idea is same method name but different parameters. Useful for APIs and other projects where a user might need access to a wrapper method but then that wrapper passes more info to the private method that does the actual work. https://www.javatpoint.com/method-overloading-in-java – Chris May 07 '17 at 23:27
  • 2
    Your question is some what broad in nature, I'd focus on only asking one question at a time. Encapsulation is more than just data hiding, it's a means to "encapsulate" the functionality required to manage the data within a single class/object. The access control dictates how that functionality and data can be called – MadProgrammer May 07 '17 at 23:27
  • Inheritance is an extension mechanism, where you can add new functionality to a pre-existing class by create a new class which "inherits" the functionality of the parent and can add new functionality and/or change the existing functionality – MadProgrammer May 07 '17 at 23:28
  • Thank you guys for these valuable information. – david perily May 07 '17 at 23:29
  • *"Polymorphism is like the same thing as inheritance"* - No. While it's related to inheritance, it's not the same thing. Polymorphism allows a child class to appear as if it's the parent class. This is where the power starts to come into OO. Basically, you can take your child class and pass it to any other object which is expecting an instance of the parent class. While it won't be able to call any new functionality declared in the child class, calling any functionality which was overridden by the child class will be handled by the child class – MadProgrammer May 07 '17 at 23:30
  • Mhhm can u provide me an example please? I tried hard to find a good one didn't really understand it that well – david perily May 07 '17 at 23:31
  • Abstract classes is something like skeleton class, which describes functionality which implementing class will need to provide, but which provides additional functionality. It reduces the amount of work that child classes might need to provide, while indicating the desired functionality the abstract class wants implemented – MadProgrammer May 07 '17 at 23:32
  • *"Mhhm can u provide me an example please? I tried hard to find a good one didn't really understand it that well"* - You own example is correct. `Cat` is an instance of `Animal`, but `Animal` might not have the `climb` method which `Cat` implements. So you pass `Cat` to a method which expects an instance of `Animal`, the method will only be able to access the functionality of `Animal` and not the functionality that `Cat` defines itself – MadProgrammer May 07 '17 at 23:34

1 Answers1

0

These things all work together.

An object is something that is self-sufficient, it keeps track of its own state. Encapsulation enforces that separation, the object publishes methods that other objects call, but those methods are responsible for modifying the object's state.

In oo systems that use classes the class is a template for creating objects. Subclassing means creating a new class that is a more specific version of the subclassed class, where subclassed objects inherit the class definitions that specify the methods and fields of the superclasses.

Abstract classes defer some method implementations, leaving them to the subclasses to implement. If the superclass knows something has to happen at some particular point but wants to leave exactly what happens to the discretion of the specific objects, that's what abstract methods are for.

There's a pattern emerging here: objects taking responsibility for themselves, and a hierarchy of types from most abstract/general to most concrete/specific. Polymorphism is about objects' behavior being determined at the time the program runs based on what methods are overridden. Overriding means the subtype has a more specific version of a method that is substituted for the superclass version.

(Overloading otoh is a convenience for allowing a class to have methods with the same name but different parameters.)

The result of this can be a system that at a high level deals with abstract types and lets the objects themselves work out the exact details. The idea is that that way the details can be confined to the subclasses and the program can be modified by creating new subclasses without disrupting the rest of the program. In theory anyway, see Wadler's Expression Problem for where this all goes to hell.

And for examples: read the source that comes with the Jdk. The packages java.lang and java.util have a lot of classes that are examples of OO design.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276