0% found this document useful (0 votes)
245 views2 pages

Implicit Method Visibility in Interfaces

An interface is a collection of public abstract methods and constants. A class implements an interface by providing implementations for the interface's abstract methods. Interfaces can extend other interfaces, inheriting their methods. The first concrete class that implements an interface must provide implementations for all inherited abstract methods. A class cannot extend or implement more than one interface that defines methods with the same signature but different return types.

Uploaded by

DARA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
245 views2 pages

Implicit Method Visibility in Interfaces

An interface is a collection of public abstract methods and constants. A class implements an interface by providing implementations for the interface's abstract methods. Interfaces can extend other interfaces, inheriting their methods. The first concrete class that implements an interface must provide implementations for all inherited abstract methods. A class cannot extend or implement more than one interface that defines methods with the same signature but different return types.

Uploaded by

DARA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

IT1908

INTERFACES The following interface definitions are equivalent:


//Interface 1
Fundamentals public interface Drinkable {
• An interface is a collection of related abstract public methods. int getNumGlasses();
It can also contain default methods, static methods, and }
constants declaration. //Interface 2
• An interface is created and defined using the interface public abstract interface Drinkable {
keyword. public abstract int getNumGlasses();
public interface Drinkable { }
public static final int MAX_GLASSES = 0;
public abstract int getNumGlasses(); Rules in Inheriting an Interface
} 1. An interface that extends another interface, as well as an
Explanation: Methods are implicitly public; there is no need abstract class that implements an interface, inherits all of the
to use this keyword in the method declaration. Constants are abstract methods as its own abstract methods.
implicitly public, static, and final. These keywords are Example 1:
not required in the declaration of a constant. //Interface 1
• A class can use an interface by adding an implements public interface Drinkable {
keyword in the class declaration. public int getNumGlasses();
public class Water implements Drinkable { }
public int getNumGlasses() { //Interface 2
return 8; public interface Potable {
} public String getContent();
} }
• A class can implement multiple interfaces. //An interface inheriting from the 2 interfaces
Example: public interface Sellable extends Drinkable,
public class Water implements Drinkable, Potable { } Potable { }
Explanation: Any class that implements the Sellable
Rules in Defining an Interface interface must provide an implementation for the methods in
1. An interface cannot be instantiated directly. the parent interfaces, getNumGlasses() and getContent().
Drinkable d = new Drinkable(); //does not compile Example 2:
2. It is not required for an interface to have a method. //Interface 1
public class Water implements Drinkable { } public interface Drinkable {
3. An interface cannot be marked as final. public int getNumGlasses();
public final interface Drinkable { } }
4. Interfaces are assumed to abstract. //Interface 2
public interface Drinkable { } is equivalent to public interface Potable {
public abstract interface Drinkable { } public String getContent();
5. All abstract, default, and static methods in an interface are }
implicitly public.

08 Handout 1 *Property of STI


[email protected] Page 1 of 2
IT1908

//An abstract class implementing the 2 interfaces public interface Drinkable { }


public abstract class Water implements Drinkable, public interface Potable implements Drinkable { }
Potable { } 2. A class can implement two (2) or more interfaces that contain
Explanation: The abstract class (Water) inherits the abstract the same method.
methods of the interfaces (Drinkable, Potable) but is not //Interface 1
required to implement them. public interface Drinkable {
2. The first concrete class that implements an interface, or public int getNumGlasses();
extends an abstract class that implements an interface, must }
provide an implementation for all of the inherited abstract //Interface 2
methods. public interface Potable {
//Interface 1 public int getNumGlasses();
public interface Drinkable { }
public int getNumGlasses(); public class Water implements Drinkable, Potable {
} public int getNumGlasses() {
//Interface 2 return 8;
public interface Potable { }
public String getContent(); }
} 3. A class can implement interfaces containing methods that
//An abstract class implementing the 2 interfaces overload.
public abstract class Water implements Drinkable, //Interface 1
Potable { } public interface Drinkable {
/*A non-abstract class implementing the 2 public int getNumGlasses();
interfaces*/ }
public class Juice extends Water implements //Interface 2
Drinkable, Potable { } public interface Potable {
Explanation: The first concrete class (Juice) to extend the public int getNumGlasses(int num);
abstract class (Water) must implement all the inherited }
interface methods within its definition. 4. A class cannot implement interfaces that contain methods
3. A class cannot extend an interface. with the same signature but with different return types.
public interface Drinkable { } //Interface 1
public class Water extends Drinkable { } public interface Drinkable {
//does not compile public int getNumGlasses();
4. An interface cannot extend a class. }
public class Water { } //Interface 2
public interface Drinkable extends Water { } public interface Potable {
//does not compile public void getNumGlasses();
}
Rules in Implementing an Interface Reference:
Oracle Docs (n.d.). Citing sources. Retrieved from
1. An interface cannot implement another interface. https://docs.oracle.com/javase/tutorial/java/javaOO/index.html

08 Handout 1 *Property of STI


[email protected] Page 2 of 2

You might also like