48024 Lecture Notes - Lecture 7: Code Reuse, Multiple Inheritance

56 views2 pages
5 Jun 2018
School
Department
Course
Professor
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.
Iterfaes use a is a relatioship. Thus, if the lass is a type of the iterfae, 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 superlass’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
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents