CS515 Module 2 - Classes Abstraction & Interface
CS515 Module 2 - Classes Abstraction & Interface
Polymorphism, Abstract
Classes & Interfaces
By;
Dr. Y. B. Mohammed
Department of Computer Science, Faculty of Computing, ATBU
Bauchi
The Object Class
• Because every class is directly or indirectly derived
from the Object class:
• every class inherits the Object class’s members.
• example: toString and equals.
• In the Object class, the toString method returns a
string containing the object’s class name and a hash of
its memory address.
• The equals method accepts the address of an object
as its argument and returns true if it is the same as the
calling object’s address.
• Example: ObjectMethods.java
11-2
Polymorphism
• A reference variable can reference objects of classes
that are derived from the variable’s class.
GradedActivity exam;
• We can use the exam variable to reference a
GradedActivity object.
exam = new GradedActivity();
• The GradedActivity class is also used as the
superclass for the FinalExam class.
• An object of the FinalExam class is a
GradedActivity object.
11-3
Polymorphism
• A GradedActivity variable can be used to reference a
FinalExam object.
GradedActivity exam = new FinalExam(50, 7);
• This statement creates a FinalExam object and stores the
object’s address in the exam variable.
• This is an example of polymorphism.
• The term polymorphism means the ability to take many
forms.
• In Java, a reference variable is polymorphic because it can
reference objects of types different from its own, as long as
those types are subclasses of its type.
11-4
Polymorphism
• Other legal polymorphic references:
GradedActivity exam1 = new FinalExam(50, 7);
GradedActivity exam2 = new PassFailActivity(70);
GradedActivity exam3 = new PassFailExam(100, 10, 70);
• The GradedActivity class has three methods:
setScore, getScore, and getGrade.
• A GradedActivity variable can be used to call
only those three methods.
GradedActivity exam = new PassFailExam(100, 10, 70);
System.out.println(exam.getScore()); // This works.
System.out.println(exam.getGrade()); // This works.
System.out.println(exam.getPointsEach()); // ERROR!
11-5
Polymorphism and Dynamic
Binding
• If the object of the subclass has overridden a
method in the superclass:
• If the variable makes a call to that method the
subclass’s version of the method will be run.
GradedActivity exam = new PassFailActivity(60);
exam.setScore(70);
System.out.println(exam.getGrade());
• Java performs dynamic binding or late binding when a
variable contains a polymorphic reference.
• The Java Virtual Machine determines at runtime which
method to call, depending on the type of object that the
variable references.
11-6
Polymorphism
• It is the object’s type, rather than the reference type, that
determines which method is called.
• Example:
• Polymorphic.java
• You cannot assign a superclass object to a subclass reference
variable.
11-7
Abstract Classes
• An abstract class cannot be instantiated, but
other classes are derived from it.
• An Abstract class serves as a superclass for other
classes.
• The abstract class represents the generic or
abstract form of all the classes that are derived
from it.
• A class becomes abstract when you place the
abstract key word in the class definition.
public abstract class ClassName
11-8
Abstract Methods
• An abstract method has no body and must be
overridden in a subclass.
• An abstract method is a method that appears in a
superclass, but expects to be overridden in a subclass.
• An abstract method has only a header and no body.
AccessSpecifier abstract ReturnType MethodName(ParameterList);
• Example:
• Student.java, CompSciStudent.java, CompSciStudentDemo.java
11-9
Abstract Methods
• Notice that the key word abstract appears in
the header, and that the header ends with a
semicolon.
public abstract void setValue(int value);
• Any class that contains an abstract method is
automatically abstract.
• If a subclass fails to override an abstract method,
a compiler error will result.
• Abstract methods are used to ensure that a
subclass implements the method.
11-10
Interfaces
• An interface is similar to an abstract class that has
all abstract methods.
• It cannot be instantiated, and
• all of the methods listed in an interface must be written
elsewhere.
• The purpose of an interface is to specify behavior
for other classes.
• An interface looks similar to a class, except:
• the keyword interface is used instead of the keyword
class, and
• the methods that are specified in an interface have no
bodies, only headers that are terminated by semicolons.
11-11
Interfaces
• The general format of an interface definition:
public interface InterfaceName
{
(Method headers...)
}
• All methods specified by an interface are public by default.
• A class can implement one or more interfaces.
11-12
Interfaces
• If a class implements an interface, it uses the implements
keyword in the class header.
public class FinalExam3 extends
GradedActivity implements Relatable
• Example:
• GradedActivity.java
• Relatable.java
• FinalExam3.java
• InterfaceDemo.java
11-13
Fields in Interfaces
• An interface can contain field declarations:
• all fields in an interface are treated as final and static.
• Because they automatically become final, you must provide an
initialization value.
public interface Doable
{
int FIELD1 = 1, FIELD2 = 2;
(Method headers...)
}
• In this interface, FIELD1 and FIELD2 are final static
int variables.
• Any class that implements this interface has access to these
variables.
11-14
Implementing Multiple Interfaces
• A class can be derived from only one superclass.
• Java allows a class to implement multiple interfaces.
• When a class implements multiple interfaces, it must
provide the methods specified by all of them.
• To specify multiple interfaces in a class definition, simply
list the names of the interfaces, separated by commas,
after the implements key word.
public class MyClass implements Interface1,
Interface2,
Interface3
11-15
Interfaces in UML
FinalExam3 Relatable
11-16
Polymorphism with Interfaces
• Java allows you to create reference variables of an
interface type.
• An interface reference variable can reference any
object that implements that interface, regardless of
its class type.
• This is another example of polymorphism.
• Example:
• RetailItem.java
• CompactDisc.java
• DvdMovie.java
• PolymorphicInterfaceDemo.java
11-17
Polymorphism with Interfaces
• In the example code, two RetailItem
reference variables, item1 and item2, are
declared.
• The item1 variable references a
CompactDisc object and the item2 variable
references a DvdMovie object.
• When a class implements an interface, an
inheritance relationship known as interface
inheritance is established.
• a CompactDisc object is a RetailItem, and
• a DvdMovie object is a RetailItem.
11-18
Polymorphism with Interfaces
• A reference to an interface can point to any class that
implements that interface.
• You cannot create an instance of an interface.
RetailItem item = new RetailItem(); // ERROR!
• When an interface variable references an object:
• only the methods declared in the interface are
available,
• explicit type casting is required to access the other
methods of an object referenced by an interface
reference.
11-19
Encapsulation
• Encapsulation
• Encapsulation allows the programmer to group data and the subroutines
that operate on them together in one place, and to hide irrelevant details
from the user.
• Information Hiding
• Making objects and algorithms invisible to portions of the system that do not
need them.
20
Modules
• If a module M exports a type T, the rest of the program can only
pass T to subroutines exported from M.
• T is said to be an opaque type.
• Body is Changed
22
Classes can limit visibility
• Private
• Protected
• Public
23
Derived class can restrict visibility
• Private
• Protected and public members of base class are private in derived
class.
• Protected
• Protected and public members of base class are protected in derived
class.
• Public
• Protected and public members of base class are protected and public
in derived class.
• Private members of base class aren’t visible in derived class.
24