0% found this document useful (0 votes)
83 views32 pages

Object-Oriented Programming CMPE 201

This document discusses the object-oriented programming concept of polymorphism. It covers polymorphism examples, demonstrating polymorphic behavior, abstract classes and methods, final classes and methods, and interfaces. Key points include that polymorphism enables "programming in the general" and the same method invocation can produce different results depending on the object type. Abstract classes are too general to create objects from directly but are used as superclasses. Interfaces contain only constants and abstract method signatures that classes implement.

Uploaded by

Omar Zardasht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views32 pages

Object-Oriented Programming CMPE 201

This document discusses the object-oriented programming concept of polymorphism. It covers polymorphism examples, demonstrating polymorphic behavior, abstract classes and methods, final classes and methods, and interfaces. Key points include that polymorphism enables "programming in the general" and the same method invocation can produce different results depending on the object type. Abstract classes are too general to create objects from directly but are used as superclasses. Interfaces contain only constants and abstract method signatures that classes implement.

Uploaded by

Omar Zardasht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Object-Oriented

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 void setName(String s)


{
name = s;
}

public String getName()


{
return name;
}

public void setAge(int a)


{
age = a;
}

public int getAge()


{
return age;
}

public String toString()


{
return "Persons name is " + getName();
}
}
public class Student extends Person 8
{
private double gpa;

public Student(String n)
{
super(n);
}

public void setGpa(double g)


{
gpa = g;
}

public double getGpa()


{
return gpa;
}

public String toString()


{
return "Students name is " + getName();
}
}
9
public class Teacher extends Person
{
private double salary;

public Teacher(String n)
{
super(n);
}

public void setSalary(double s)


{
salary = s;
}

public double getSalary()


{
return salary;
}

}
10

public class InheritanceTest


{
public static void main(String[] args)
{
Person ob1 = new Person("Dara");

Person ob2 = new Student("Noora");


Person ob3 = new Teacher("Ahmed");

Student ob4 = new Student("Yehya");


Teacher ob5 = new Teacher("Zara");

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 void setName(String s)


{
name = s;
}

public String getName()


{
return name;
}

public abstract double area();


}
16
public class Rectangle extends Shape
{
private double width;
private double length;

public Rectangle()
{
this(1,1);
}

public Rectangle(double width, double length)


{
this.width = width;
this.length = length;
}

public double area()


{
return width * length;
}
}
17

public class Square extends Shape


{
private double side;

public Square()
{
this(1);
}

public Square(double side)


{
this.side = side;
}

public double area()


{
return side * side;
}
}
18

public class AbstractTest


{
public static void main(String[] args)
{
Square sqr = new Square(5);
Rectangle rec = new Rectangle(4,5);

System.out.println("Area of square is: " + sqr.area());


System.out.println("Area of rectangle is: " + rec.area());
}
}

Area of square is 25.0


Area of rectangle is 20.0
• Superclass and subclass assignment rules
– Assigning a superclass reference to a superclass variable is
straightforward
– Assigning a subclass reference to a subclass variable is
straightforward
– Assigning a subclass reference to a superclass variable is safe
because of the is-a relationship
– Assigning a superclass reference to a subclass variable is a
compilation error

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:

public class ClassName extends SuperclassName implements FirstInterface, SecondInterface,..

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 interface Shape


{
double area();
}

We can write the above interface in another way as


following:

public interface Shape


{
public abstract double area();
}
29
public class Rectangle implements Shape
{
private double width;
private double length;

public Rectangle()
{
this(1,1);
}

public Rectangle(double width, double length)


{
this.width = width;
this.length = length;
}

public double area()


{
return width * length;
}
}
30

public class Square implements Shape


{
private double side;

public Square()
{
this(1);
}

public Square(double side)


{
this.side = side;
}

public double area()


{
return side * side;
}
}
31

public class AbstractTest


{
public static void main(String[] args)
{
Square sqr = new Square(5);
Rectangle rec = new Rectangle(4,5);

System.out.println("Area of square is: " + sqr.area());


System.out.println("Area of rectangle is: " + rec.area());
}
}

Area of square is: 25.0


Area of rectangle is: 20.0
• Inheritance and interfaces are similar in their
implementation of the “is-a” relationship.
• An object of a class that implements an interface may
be thought of as an object of that interface type.
• An object of any subclasses of a class that implements
an interface also can be thought of as an object of the
interface type.

32

You might also like