Interfaces for Java

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is another way to think of Interfaces?

- interface as an abstract base class with all methods abstract

Calls of comparing object a and b

-A value <0 if 'a' comes "before" 'b' in the ordering. -A value >0 if 'a' comes "after" 'b' in the ordering. - or 0 if both are considered "Equal" in the ordering. Notice that there are quotations in location terms because logically they may be wrong but in the instance their being compared it may be right.

Differences of an abstract class.

-An abstract class is a mix of abstract methods and non-abstract methods, when implemented that means the class has a method through default, so some default implementation. -An abstract class can also have static methods, private and protected methods, etc.

How does an interface differ from inheritance?

-An interface specifies what an object is capable of and does not share code. -Only method stubs in the interface. -Objects can-act-as any interface it implements. -"A Rectangle does what you expect from a Shape as long as it implements the interface."

Example of interface participants being used in an array(polymorphism)

-Each element of the array executes the appropriate behavior for its object when it is passed to the printInfo method, or when area or perimeter is called on it. Example: Circle circ = new Circle(12.0); Rectangle rect = new Rectangle(4, 7); Triangle tri = new Triangle(5, 12, 13); Shape[] shapes = {circ, tri, rect}; for (int i = 0; i < shapes.length; i++) { printInfo(shapes[i]); }

What do interfaces allow us to do?

-Getting around the Java limitation of no multiple inheritance, a class can implement several interfaces. -A class can implement an interface while also extending to another class.

What does inheritance encode?

-Inheritance encodes an is- relationship and provides code sharing. -An Executive object can be treated as a StaffMember, and Executive inherits StaffMember's code.

Polymorphism

-This possible with interfaces -Any object that implements the interface may be passed a parameter methods such as: public static void printInfo(Shape s) { System.out.println("The shape: " + s); System.out.println("area : " + s.area()); System.out.println("perim: " + s.perimeter()); System.out.println(); }

Interface Polymorphism (Arrays)

-We can create an array of an interface type, and store any object implementing that interface as an element.

Uses of Interfaces in java

-comparable: allows us to order the elements of an arbitrary class -Serializable(in java.io)- for saving objects to a file. -List, Set, Map, Iterator( in Java.util): Describe data structures for storing collections of objects.

Interface

A list of methods that a class promises to implement.

What field do methods have in an interface?

All methods are public!: -public <type> <name>(<type> <name>, ..., <type> <name>);

What do interfaces also define theory wise

Defines a contract for how you interact with an object, independent from the underlying implementation.

"Writing an interface requires you to specify the methods a class implementing the interface does not need to address.?

IC Question: False any signatures not implemented cause a compile error.

"We can create an array of an interface type, and store any object implementing that interface as"

IC question: An element.

It will not compile!

If we write a class that claims act like a Shape but doesn't implement the area and perimeter methods. What happens?

Delegation trick(compare to)

If your object's attributes are comparable (such as strings), you can use their compareTo: // sort by employee name public int compareTo(StaffMember other) { return name.compareTo(other.getName()); }

Inheritance and interfaces

Inheritance can be applied to interfaces - an interface can be derived from another interface.

What is an interface Code Wise?

Interface is a type

What type of methods does an interface posses?

Methods of an interface are abstract.

This is what happens when a class claims to implement an interface but does not implement the methods belonging to the interface.

Syntax: public class Banana implements Shape { //without implementing area or perimeter } -error message: Banana.java:1: Banana is not abstract and does not override abstract method area() in Shape public class Banana implements Shape {

What does the term 'Interface' also refer to?

The set of public methods through which we can interact with objects of a class.

Code sharing and Intefaces

There is no code sharing when using an interface

What does it mean when a class implements an interface?

This means that the class must contain an implementation for each method in the Interface that it's implementing. -Syntax: public class <name> implements <interface name> { ... }

A Java comparable interface

What sort of interface would you need to define the ordering of a group of objects?

Implementing an interface

You have to have the class declare that it implements an interface.

Example of comparable

public class Employee implements Comparable<Employee> { ... }

Example of what a comparable interface

public interface Comparable<E> { public int compareTo(E other); }


Ensembles d'études connexes

Texas Govt. Exam 3 Review Questions Ch. 6,7

View Set

Cell Division in Eukaryotic Cells Quiz

View Set

Chapter 11 water and major minerals

View Set

NCLEX PRACTICE QUESTIONS-ONCOLOGY UNITY 1

View Set

Peds Musculoskeletal or Articular Dysfunction

View Set