48024 Lecture Notes - Lecture 7: Code Reuse, Multiple Inheritance
Study 7 – System Design
Interfaces
• An interface declares a set of methods common to multiple classes.
• Each class provides its own implementation of these methods
• To implement an interface, use the implements keyword, and override and interface
method with the @Override annotation
• Methods from an interface must be public.
• Iterfaes use a is a relatioship. Thus, if the lass is a type of the iterfae, the
interface is a valid option.
• A class can implement multiple interfaces
Polymorphism
• Polymorphism allows for a single object to have many types.
• It is a payoff for the use of interfaces.
• This can allow you to store objects with different types (given they share at least one
type) in the same list.
Super Classes
• Similar to interfaces, superclasses define methods common to multiple classes.
• However, dissimilar, they provide implementations for the common methods, they
define the common fields, and they also define non-public members.
• Each subclass of a superclass inherits the common methods and fields
• Methods which must be implemented in the subclasses are declared abstract.
• A class containing abstract methods must also be declared abstract.
• In superclasses, multiple inheritance is not supported as it is in interfaces, thus a
subclass cannot extend more than one superclass.
• Subclasses implement the abstract methods and inherit everything else.
• Fields declared protected can be accessed by subclasses
• Inheritance is a form of code reuse. You should avoid repeated code across classes,
and instead put it in a superclass and inherit it.
• Non-abstract methods can also be overridden.
• The superlass’s ersio of the ethod a e alled ith super.
• The subclass constructor must call the superclass constructor first.
find more resources at oneclass.com
find more resources at oneclass.com