Skip to content

KYehor/oop-example

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

oop-example

Polymorphism


Polymorphism is the ability of an object to take on many forms.
The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Any Java object that can pass more than one IS-A test is considered to be polymorphic.
In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
It is important to know that the only possible way to access an object is through a reference variable.
A reference variable can be of only one type.
Once declared, the type of a reference variable cannot be changed.
The reference variable can be reassigned to other objects provided that it is not declared final.
The type of the reference variable would determine the methods that it can invoke on the object.
A reference variable can refer to any object of its declared type or any subtype of its declared type.
A reference variable can be declared as a class or interface type.

Compile time polymorphism: It is also known as static polymorphism. This type of polymorphism is achieved by
function overloading or operator overloading.

  • Method Overloading: When there are multiple functions with same name but different parameters then these functions
    are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.
  • Operator Overloading: Java also provide option to overload operators. For example, we can make the operator (‘+’) for
    string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single
    operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them.
    Runtime polymorphism: It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden
    method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.
  • Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class.
    That base function is said to be overridden.

    ##Aggregation and Composition

    Aggregation and Composition are subsets of association meaning they are specific cases of association.
    In both aggregation and composition object of one class "owns" object of another class.
    But there is a subtle difference. In Composition the object of class that is owned by the object of it's owning class cannot live on it's own
    (Also called "death relationship"). It will always live as a part of it's owning object where as in Aggregation the dependent object is standalone
    and can exist even if the object of owning class is dead.
    So in composition if owning object is garbage collected the owned object will also be which is not the case in aggregation.

    Composition Example: Consider the example of a Car and an engine that is very specific to that car (meaning it cannot be used in any other car).
    This type of relationship between Car and SpecificEngine class is called Composition. An object of the Car class cannot exist without an object
    of SpecificEngine class and object of SpecificEngine has no significance without Car class. To put in simple words Car class solely "owns" the
    SpecificEngine class.

    Aggregation Example: Now consider class Car and class Wheel. Car needs a Wheel object to function. Meaning the Car object owns the Wheel object
    but we cannot say the Wheel object has no significance without the Car Object. It can very well be used in a Bike, Truck or different Cars Object.

    ##Generics in Java

    Generics in Java is similar to templates in C++. The idea is to allow type (Integer, String, … etc and user defined types) to be a parameter to
    methods, classes and interfaces
    Advantages of Generics:

  • Code Reuse: We can write a method/class/interface once and use for any type we want.
  • Type Safety : Generics make errors to appear compile time than at run time (It’s always better to know problems in your code at compile time
    rather than making your code fail at run time). Suppose you want to create an ArrayList that store name of students and if by mistake programmer
    adds an integer object instead of string, compiler allows it. But, when we retrieve this data from ArrayList, it causes problems at runtime.
  • Individual Type Casting is not needed: If we do not use generics, then, in the above example every-time we retrieve data from ArrayList,
    then we need not to typecast it every time.
  • Implementing generic algorithms: By using generics, we can implement algorithms that work on different types of objects and at the same
    they are type safe too.
  • About

    No description, website, or topics provided.

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

     
     
     

    Contributors

    Languages

    • Java 100.0%