Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.

Similar presentations


Presentation on theme: "UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in."— Presentation transcript:

1 UNIT-3 Interfaces & Packages

2 What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in an interface are abstract. Interfaces can contain no implementation Interfaces cannot contain instance variables. However, they can contain public static final variables (ie. constant class variables) Interfaces are declared using the "interface" keyword If an interface is public, it must be contained in a file which has the same name. Interfaces are more abstract than abstract classes Interfaces are implemented by classes using the "implements" keyword.

3 Declaring an Interface public interface Steerable { public void turnLeft(int degrees); public void turnRight(int degrees); } In Steerable.java: public class Car extends Vehicle implements Steerable { public int turnLeft(int degrees) { [...] } public int turnRight(int degrees) { [...] } In Car.java: When a class "implements" an interface, the compiler ensures that it provides an implementation for all methods defined within the interface.

4 Implementing Interfaces A Class can only inherit from one superclass. However, a class may implement several Interfaces The interfaces that a class implements are separated by commas Any class which implements an interface must provide an implementation for all methods defined within the interface. NOTE: if an abstract class implements an interface, it NEED NOT implement all methods defined in the interface. HOWEVER, each concrete subclass MUST implement the methods defined in the interface. Interfaces can inherit method signatures from other interfaces.

5 Declaring an Interface public class Car extends Vehicle implements Steerable, Driveable { public int turnLeft(int degrees) { [...] } public int turnRight(int degrees) { [...] } // implement methods defined within the Driveable interface In Car.java:

6 Inheriting Interfaces If a superclass implements an interface, it's subclasses also implement the interface public abstract class Vehicle implements Steerable { private String make; [...] public class Car extends Vehicle { private int trunkCapacity; [...] Vehicle - make: String - model: String - tireCount: int Car - trunkCapacity: int Truck - bedCapacity: int public class Truck extends Vehicle { private int bedCapacity; [...]

7 Multiple Inheritance? Some people (and textbooks) have said that allowing classes to implement multiple interfaces is the same thing as multiple inheritance This is NOT true. When you implement an interface: The implementing class does not inherit instance variables The implementing class does not inherit methods (none are defined) The Implementing class does not inherit associations Implementation of interfaces is not inheritance. An interface defines a list of methods which must be implemented.

8 Interfaces as Types When a class is defined, the compiler views the class as a new type. The same thing is true of interfaces. The compiler regards an interface as a type. It can be used to declare variables or method parameters int i; Car myFleet[]; Steerable anotherFleet[]; [...] myFleet[i].start(); anotherFleet[i].turnLeft(100); anotherFleet[i+1].turnRight(45);

9 Abstract Classes Versus Interfaces When should one use an Abstract class instead of an interface? If the subclass-superclass relationship is genuinely an "is a" relationship. If the abstract class can provide an implementation at the appropriate level of abstraction When should one use an interface in place of an Abstract Class? When the methods defined represent a small portion of a class When the subclass needs to inherit from another class When you cannot reasonably implement any of the methods

10 © 2006 Pearson Addison- Wesley. All rights reserved 5-10 Packages and Import Statements Java uses packages to form libraries of classes A package is a group of classes that have been placed in a directory or folder, and that can be used in any program that includes an import statement that names the package – The import statement must be located at the beginning of the program file: Only blank lines, comments, and package statements may precede it – The program can be in a different directory from the package

11 © 2006 Pearson Addison- Wesley. All rights reserved 5-11 Import Statements We have already used import statements to include some predefined packages in Java, such as Scanner from the java.util package import java.util.Scanner; It is possible to make all the classes in a package available instead of just one class: import java.util.*; – Note that there is no additional overhead for importing the entire package

12 © 2006 Pearson Addison- Wesley. All rights reserved 5-12 The package Statement To make a package, group all the classes together into a single directory (folder), and add the following package statement to the beginning of each class file: package package_name; – Only the.class files must be in the directory or folder, the.java files are optional – Only blank lines and comments may precede the package statement – If there are both import and package statements, the package statement must precede any import statements

13 © 2006 Pearson Addison- Wesley. All rights reserved 5-13 The Package java.lang The package java.lang contains the classes that are fundamental to Java programming – It is imported automatically, so no import statement is needed – Classes made available by java.lang include Math, String, and the wrapper classes

14 © 2006 Pearson Addison- Wesley. All rights reserved 5-14 Package Names and Directories A package name is the path name for the directory or subdirectories that contain the package classes Java needs two things to find the directory for a package: the name of the package and the value of the CLASSPATH variable – The CLASSPATH environment variable is similar to the PATH variable, and is set in the same way for a given operating system – The CLASSPATH variable is set equal to the list of directories (including the current directory, ". ") in which Java will look for packages on a particular computer – Java searches this list of directories in order, and uses the first directory on the list in which the package is found

15 © 2006 Pearson Addison- Wesley. All rights reserved 5-15 A Package Name

16 © 2006 Pearson Addison- Wesley. All rights reserved 5-16 Pitfall: Subdirectories Are Not Automatically Imported When a package is stored in a subdirectory of the directory containing another package, importing the enclosing package does not import the subdirectory package The import statement: import utilities.numericstuff.*; imports the utilities.numericstuff package only The import statements: import utilities.numericstuff.*; import utilities.numericstuff.statistical.*; import both the utilities.numericstuff and utilities.numericstuff.statistical packages

17 © 2006 Pearson Addison- Wesley. All rights reserved 5-17 The Default Package All the classes in the current directory belong to an unnamed package called the default package As long as the current directory (. ) is part of the CLASSPATH variable, all the classes in the default package are automatically available to a program


Download ppt "UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in."

Similar presentations


Ads by Google