javauser-experienceknowledge-management

I'm a self-taught programmer so did i understand these concepts? can someone include example too


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?


Solution

  • 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.