Interfaces are a list of method signatures. All of the methods are abstract and have no implementation (they have no code in the body), just a semicolon at the end. All of the methods are automatically public, so no need to include public. An interface doesn't have any instance fields.
A class implements an interface if it implements all of the methods of the interfaces (it provides code for the methods). For a class to implement an interface, it must be included when declaring the class.
public class Triangle implements Shape
A class can also implement multiple interfaces.
public class ClassName implements myInterface, myInterface2
A class must implement (provide code for) all of the methods in the interface(s) that it implements. Sometimes, you can just write garbage code for one of the methods, but all of the methods of the interface must be declared in the class. Note: you need to declare the methods as public in the class. Additionally, classes that implement an interface can have other methods that aren't part of the interface.
Therefore, create an interface when you suspect that multiple classes will have the same method, but different implementations of the method. Also, you can think about creating an interface when you would like to create a formal contract for a class that says a class must implement certain methods.
Converting between class types and interface types
In Java, it's legal to convert an object from a class type to an interface type if the class implements the interface. For example, Shape x = triangleObj is ok if the class of triangleObj implements Shape. Or passing an object into a method with a parameter that is an interface variable is fine if the class of that object implements the interface.
To convert from an interface type back to the class type, you must cast it back. For example, if Triangle implements Shape:
Shape x = triangleObj; // convert from class type to interface type
Triangle t = (Shape)x;//cast x to Shape object called t
Polymorphism
Let's say that both the Triangle and Rectangle classes implement the interface Shape, and we have Shape x, which is a Shape variable. Both of the following statements are ok, since both Triangle and Rectangle implement Shape:
x = new Triangle(3,4);
x = new Rectangle(5,8);
Now, let's say Shape has a method getArea(). What happens when we say x.getArea()? Well, it depends on the contents of x. If the Java Virtual Machine recognizes x as a Triangle object, then the getArea() from the Triangle class will be called. Likewise, if the Java Virtual Machine recognizes x as a Rectangle, then the getArea() from the Rectangle class will be called. The Java Virtual Machine recognizes this at run-time, not when the program is being compiled.
Note: Never construct an interface variable.
Shape x = new Shape() is not legal, since interfaces don't have a constructor.
Another note: Shape x is an interface variable, so it can only call methods in the Shape interface.
Subscribe to:
Post Comments (Atom)
Searching Algorithms
Linear/Sequential Search A linear/sequential search algorithm looks at each element in the array until it finds what it's looking for. ...
-
A recursive function is one that calls itself in the method body. A recursive function breaks down a problem into smaller iterative pieces...
-
Linear/Sequential Search A linear/sequential search algorithm looks at each element in the array until it finds what it's looking for. ...
-
Cohesion - a class is cohesive if it has a single, well-defined purpose. If the class has methods and instance fields that don't exactly...
No comments:
Post a Comment