












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of java api, focusing on abstract classes and interfaces. Learn about abstract methods, using the 'final' keyword, and the difference between abstract classes and interfaces. Discover how to implement interfaces and make use of constants defined in an interface.
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













public abstract class Animal { public abstract void sound(); // Abstract method public Animal(String aType) { type = new String(aType); } public String toString() { return “This is a “ + type; } private String type; }
An abstract method cannot be private since a private method cannot be inherited and therefore cannot be redefined in a subclass. You cannot instantiate an object of an abstract class, but you can declare a variable of an abstract class type.With the new abstract version of the class Animal, you can still write: Animal thePet = null; // Declare a variable of type Animal
To make use of an interface, you implement the interface in a class—that is, you declare that the class implements the interface and you write the code for each of the methods that the interface declares as part of the class definition. When a class implements an interface, any constants that were defined in the interface definition are available directly in the class, just as though they were inherited from a base class
public interface Conversions { double inchesToMillimeters (double inches); double ouncesToGrams(double ounces); double poundsToGrams(double pounds); double hpToWatts(double hp); double wattsToHP(double watts); } public class MyClass implements Conversions { // Implementations for the methods in the Conversions interface // Definitions for the other class members... }
public class MyClass implements Conversions, Definitions, Detections { // Definition of the class including implementation of interface methods }
public class MyOtherClass implements ConversionFactors { public static double poundsToGrams(double pounds) { return pounds*POUND_TO_GRAM; } // Plus the rest of the class definition... } public class MyClass { public static double poundsToGrams(double pounds) { return pounds ***** ConversionFactors.POUND_TO_GRAM ; } // Plus the rest of the class definition... }
You can omit the implementation of one or more of the methods from an interface in a class that implements the interface, but in this case you would need to declare the class itself as abstract: public interface Convert { public void myMethod1(); public void myMethod2(); } abstract public class interfaceExample implements Convert{ public void myMethod1(){ } }
public interface MyInterface extends HisInterface, HerInterface { // Interface members – constants and abstract methods... }