Subtyping-lecture-13

Sub typing

In programming , subtyping (likewise subtype polymorphism or consideration polymorphism) is a type of sort polymorphism in which a subtype is a datatype that is identified with another datatype.


Explanation 

Subtyping is a strategy for substitution and code reuse utilized as a part of protest situated programming dialects to anticipate superfluous replicating of to a great extent comparable code and advance code lucidness and avert bugs. A subtype is essentially a substitute for a supertype that can satisfy the greater part of the last's determinations, to say the very least. So if B is a subtype of An, at that point B can simply be utilized to substitute for An and any property that is ensured by An absolute necessity additionally be ensured by B. 

The subtype is permitted to fortify and even include properties that are not the supertype, which implies that it permits the supertype to be expanded. In this way, rather than making another supertype each time something new is required and after that duplicating properties and conditions from another supertype, a subtype could basically be made that expands the supertype with extra properties or highlights without changing the supertype. At that point, for this situation, everything that is provable to the supertype will likewise be provable to the subtype and that's only the tip of the iceberg. In the event that a subtype is dealt with absolutely an indistinguishable path from the supertype by utilizing and questioning just supertype techniques and fields, at that point the outcomes would be reliable with objects of the supertype.

Example

public class vehicle
{
        public void Run()
        {
                System.out.println("vehicle class");
        }
}

public class car extends vehicle
{
        public void Run()
        {
                System.out.println("car class");
        }
}

public class bus extends vehicle
{
        public void Run()
        {
                System.out.println("bus class");
        }
}

public class truck extends vehicle
{
        public void Run()
        {
                System.out.println("truck class");
        }
}

                                                                                                                                       Previous Lecture