Object-Oriented Programming CMPE 201
Object-Oriented Programming CMPE 201
Programming:
Polymorphism
LECTURE 7
Object-Oriented Programming
CMPE 201
10.1 Introduction
10.2 Polymorphism Examples
10.3 Demonstrating Polymorphic Behavior
10.4 Abstract Classes and Methods
10.6 Final Classes and Methods
10.7 Interfaces
2
Paul Deitel and Harvey Deitel, Java How to Program Early Objects,
10th Edition
✓ Chapter 10 (from page 395 to 440)
• Polymorphism
– Enables “programming in the general”
– The same invocation can produce “many forms” of results
• Interfaces
– Implemented by classes to assign common functionality to
possibly unrelated classes
4
• Polymorphism
– When a program invokes a method through a superclass variable,
the correct subclass version of the method is called, based on the
type of the reference stored in the superclass variable
– The same method name and signature can cause different actions
to occur, depending on the type of object on which the method is
invoked
– Facilitates adding new classes to a system with minimal
modifications to the system’s code
5
• Polymorphism enables programmers to deal in
generalities and let the execution-time environment
handle the specifics.
• Programmers can command objects to behave in
manners appropriate to those objects, without
knowing the types of the objects (as long as the
objects belong to the same inheritance hierarchy).
6
public class Person extends Object 7
{
private String name;
private int age;
public Person(String s)
{
name = s;
}
public Student(String n)
{
super(n);
}
public Teacher(String n)
{
super(n);
}
}
10
System.out.println(ob1);
System.out.println(ob2);
System.out.println(ob3);
System.out.println(ob4);
System.out.println(ob5); Persons name is Dara
}
Students name is Noora
}
Persons name is Ahmed
Students name is Yehya
Persons name is Zara
• Abstract classes
– Classes that are too general to create real objects
– Used only as abstract superclasses for concrete subclasses and to
declare reference variables
– Many inheritance hierarchies have abstract superclasses
occupying the top few levels
– Keyword abstract
• Use to declare a class abstract
• Also use to declare a method abstract
– Abstract classes normally contain one or more abstract
methods
– All concrete subclasses must override all inherited abstract
methods
11
• An abstract class typically contains one or more
abstract methods that subclasses must override if the
subclasses are to be concrete.
• The instance variables and concrete methods of an
abstract class are subject to the normal rules of
inheritance.
12
• Failure to implement a superclass’s abstract methods
in a subclass is a compilation error unless the subclass
is also declared abstract.
• Attempting to instantiate an object of an abstract
class is a compilation error.
13
14
Shape
Square Rectangle
15
public abstract class Shape
{
private String name;
public Rectangle()
{
this(1,1);
}
public Square()
{
this(1);
}
19
•final methods
– Cannot be overridden in a subclass
– private and static methods are implicitly final
– final methods are resolved at compile time, this is known as
static binding
•final classes
– Cannot be extended by a subclass
– All methods in a final class are implicitly final
20
• In the Java API, the vast majority of classes are not
declared final. This enables inheritance and
polymorphism—the fundamental capabilities of
object-oriented programming.
• However, in some cases, it is important to declare
classes final—typically for security reasons.
21
• Interfaces
– Keyword interface
– Contains only constants and abstract methods
• All fields(instance variables) are implicitly public, static and
final
• All methods are implicitly public abstract methods
– Classes can implement interfaces
• The class must declare each method in the interface using the same
signature or the class must be declared abstract
– Typically used when disparate classes need to share common
methods and constants
– Normally declared in their own files with the same names as the
interfaces and with the .java file-name extension
22
23
• Failing to implement any method of an interface in a
concrete class that implements the interface results in
a syntax error indicating that the class must be
declared abstract.
24
• A class can implement as many interfaces as it
needs
– Use a comma-separated list of interface names after
keyword implements
Example:
25
•Shape interface
– Contains method area()
– Is implemented by the Square and Rectangle classes
• UML representation of interfaces
– Interfaces are distinguished from classes by placing the word
“interface” in guillemets (« and ») above the interface name
– The relationship between a class and an interface is known as
realization
• A class “realizes” the methods of an interface
26
27
<<interface>>
Shape
Square Rectangle
28
public Rectangle()
{
this(1,1);
}
public Square()
{
this(1);
}
32