Lecture 6
Lecture 6
Programming
with Java II
Email: m_khamis@ci.suez.edu.eg
Lecture outcomes
• Abstract Classes
• Abstract class & abstract method
• Interfaces
• The Comparable Interface
• Classes become more specific and concrete with each new subclass.
• If you move from a subclass back up to a superclass, the classes become more general and less
specific.
• Class design should ensure that a superclass contains common features of its subclasses.
............
subclass.
Dr. Mohamed K. Hussein 6
Abstract Classes
• This is rare, but useful when the implementation of the method in the
superclass becomes invalid in the subclass.
• Therefore, the following statement, which creates an array whose elements are of
GeometricObject type, is correct.
• You can then create an instance of GeometricObject and assign its reference to the array like
this:
What is an interface?
• Like an abstract class, you cannot create an instance from an interface using the
new operator,
• In most cases you can use an interface more or less the same way you use an
abstract class.
• For example, you can use an interface as a data type for a variable.
• All data fields are public final static and all methods are public abstract in an
interface.
• In an interface, the data must be constants; an abstract class can have all types of data.
• Each method in an interface has only a signature without implementation; an abstract class can have concrete
methods.
• Write a test program that prompts the user to enter three sides of the triangle, a color, and a
Boolean value to indicate whether the triangle is filled.
• The program should create a Triangle object with these sides and set the color and filled
properties using the input.
• The program should display the area, perimeter, color, and true or false to indicate whether it
is filled or not.