OOP prefi
An abstract class - is created to ensure that subclasses will implement necessary methods.
• An abstract method has no body and is required to be implemented by subclasses. It is declared without braces but with a semicolon.
Example: abstract void draw();
Rules in Defining Abstract Classes
1. An abstract class cannot be instantiated directly.
2. An abstract class may include non-abstract methods
3. An abstract class cannot be marked as final because a final class cannot be extended by another class.
4. An abstract class that extends another abstract class inherits all its abstract methods as its own abstract methods.
5. The first concrete class (non-abstract class that extends an abstract class) is required to implement all inherited abstract methods.
Rules in Defining Abstract Methods
1. An abstract method may only be defined in an abstract class.
2. An abstract method cannot be marked as final because a final method cannot be overridden in a subclass.
3. An abstract method cannot be marked as private since that method is only accessible to the class where it belongs to.
4. To override an abstract method, declare a new method with the same name, parameter list, and return type as the method in the parent class. The method in the subclass must be at least as accessible as the method in the parent class.
An interface - is a collection of related abstract public methods. It can also contain default methods, static methods, and constants declaration.
• An interface is created and defined using the interface keyword.
Methods are implicitly public; there is no need to use this keyword in the method declaration.
Constants are implicitly public, static, and final. These keywords are
not required in the declaration of a constant.
• A class can use an interface by adding an implements keyword in the class declaration.
• A class can implement multiple interfaces.
Rules in Defining an Interface
1. An interface cannot be instantiated directly.
2. It is not required for an interface to have a method.
3. An interface cannot be marked as final.
4. Interfaces are assumed to abstract.
5. All abstract, default, and static methods in an interface are implicitly public.
Rules in Inheriting an Interface
1. An interface that extends another interface, as well as an abstract class that implements an interface, inherits all of the abstract methods as its own abstract methods.
2. The first concrete class that implements an interface, or extends an abstract class that implements an interface, must provide an implementation for all of the inherited abstract methods.
3. A class cannot extend an interface
4. An interface cannot extend a class.
Rules in Implementing an Interface
1. An interface cannot implement another interface.
2. A class can implement two (2) or more interfaces that contain the same method.
3. A class can implement interfaces containing methods that overload.
4. A class cannot implement interfaces that contain methods with the same signature but with different return types.