Question

From what I understand, does it mean. making methods to build up different components of a program. e.g. if i was to make a program that adds and subtracts numbers then I would have something like;

        public void addnum(int addnum){
        addnum= addnum+1;
        system.out.println(addnum);
    }

        public void subtractnum int subtractnum){
        subtractnum = subtractnum-1;
        system.out.println(addnum);
    }
        public static void main(String args[]){
        int num = 21;
        addnum(num);
        subtractnum(num);

    }

Am I correct, or does it mean something else?

Was it helpful?

Solution

In the Java and .NET frameworks, among others, having a class X inherit from Y has two benefits:

  1. Instances of class X encapsulate the values of all of Y's fields, and can use any of Y's protected members on themselves as if those members belonged to X; additionally, the definition of class X may use Y's static members as though they were its own.

  2. Variables of type Y may hold references to instances of type X.

Allowing a class object to regard as its own the contents of multiple other classes makes it impossible to have upcasts and downcasts preserve identity; since identity-preserving upcasts and downcasts are useful, Java and .NET allow each class to regard members of only one parent as its own (members of the parent's parent are also members of the parent, and get incorporated as such). The limitation of incorporating members from only one parent class is generally not overly restrictive.

On the other hand, if each type could only be stored in references of its own type or its ancestors' types, that would be restrictive. To allow for the possibility that it may be helpful to store references to an object in multiple independent types, Java and .NET both make it possible to define interface types. A reference to an object which implements an interface may be stored in a variable of that interface type (achieving the second benefit of inheritance) but unlike class inheritance which is restricted to a single parent, interface implementation is relatively unrestricted. A class may implement an arbitrary number of independent interfaces, and references to such a class may be stored in variables of any of those interfaces' types.

In short, interfaces provide the most important benefit of inheritance (reference substitutability), but give up some features in exchange for giving up a significant restriction (the inability to inherit from multiple classes).

OTHER TIPS

You´re confusing different methods with different parameter types.
Maybe this example will help:

public interface GeometricalObject {
    double getArea();
    double getPerimeter();
}

...

public class Circle implements GeometricalObject {
    public double r;

    public double getArea() {
        return 3.14 * r * r;
    }
    public double getPerimeter()
    {
        return 3.14 * 2 * r;
    }
}

...

public class Square implements GeometricalObject {
    public double s;

    public double getArea() {
        return s * s;
    }
    public double getPerimeter()
    {
        return 4 * s;
    }
}

...

public void printGeomObject(GeometricalObject g) {
    System.out.println("Area is " + g.getArea());
    System.out.println("Perimeter is " + g.getPerimeter());
}

Interface provides us the way of multilevel inheritance.

Interface can be extended to any class

Common properties of any class can be define in interface and can be inherited to many classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top