Uploaded by Khant Si Thu

Chapter Four

advertisement
Bahir Dar University
ባሕር ዳር ዩኒቨርሲቲ
Bahir Dar Institute of Technology
ባሕር ዳር ቴክኖሎጂ ኢንስቲትዩት
Faculty of Computing
ኮምፒዩትንግ ፋኩሊት
Object Oriented Programming
Course Code: SEng2071
Target Group: 2nd Year Software Engineering
Instructor: Haileyesus D.
Object Oriented Programming
CHAPTER FOUR
POLYMORPHISM
&
ABSTRACTION
Chapter Outline
01
Polymorphism
02
Abstraction
Polymorphism using overridden methods, polymorphism using
overloading method, the final keyword: final class, final method,
final variable, constants, final argument
Abstract class, abstract method, interface, declaring interface
constants,
Polymorphism
Polymorphism
Polymorphism: means the ability to take more than one form.
It can be defined as the same thing being used in different forms.
Polymorphism is a OOPs concept where one name can have many
forms.
An operation may exhibit different behavior in different instances.
Compiled by : Haileyesus Demissie
Object Oriented Programming
5
Polymorphism
Example 1: Consider the operation of addition.
For two numbers, the operation will generate a sum
If the operation is strings, then the operation produce a third string
by concatenation.
Example 2:
Compiled by : Haileyesus Demissie
Object Oriented Programming
6
Polymorphism
Polymorphism is not a programming concept but it is one of the
principal of OOPs.
For many objects oriented programming language polymorphism
principle is common but whose implementations are varying from
one objects oriented programming language to another object
oriented programming language.
Compiled by : Haileyesus Demissie
Object Oriented Programming
7
Polymorphism using overridden methods
Method Overriding: is redefining a super class method in a sub class.
Rules for Method Overriding
The method signature i.e. method name, parameter list and return
type have to match exactly.
The overridden method can widen the accessibility but not narrow it,
i.e. if it is private in the base class, the child class can make it public
but not vice versa.
Compiled by : Haileyesus Demissie
Object Oriented Programming
8
Polymorphism using overridden methods
Example: The passing average grade for students is 2.0 and
above. The educational regulations for postgraduate students are
similar to undergraduate students. However, for postgraduate
students the passing grade is 3.00 and above. This indicates if we
have a class for postgraduate students, the class is slightly different
from the Student class. Hence, we can inherit or use the Student class
to PostGraduateStudent class, but we have to override some
methods of the Student class.
Compiled by : Haileyesus Demissie
Object Oriented Programming
9
Polymorphism using overridden methods
Any time you have a class that inherits a method from a superclass,
you have the opportunity to override the method (unless, the
method is marked final).
The key benefit of overriding is the ability to define behavior that's
specific to a particular subclass type.
Compiled by : Haileyesus Demissie
Object Oriented Programming
10
Polymorphism using overridden methods
Rules of method overriding
The new method definition must have the same method signature,
i.e., the method name, and the types and the number of parameters,
including their order, are the same as in the overridden method.
The new method definition cannot narrow the accessibility of the
method, but it can widen it.
You cannot override a method marked final.
Compiled by : Haileyesus Demissie
Object Oriented Programming
11
Polymorphism using overridden methods
Rules of method overriding
You cannot override a method marked static.
If a method can't be inherited, you cannot override it. Remember
that overriding implies that you're re implementing a method you
inherited.
Compiled by : Haileyesus Demissie
Object Oriented Programming
12
Polymorphism using overridden methods
Compiled by : Haileyesus Demissie
13
Polymorphism using overridden methods
14
Polymorphism using overridden methods
Output
15
Polymorphism using overloading methods
Overloaded methods: let you reuse the same method name in a
class, but with different arguments (and optionally, a different return
type).
Java permits you to reuse method name for more than one method.
The rules for making overloaded methods are:
Argument list must differ
Return types can be different
Compiled by : Haileyesus Demissie
Object Oriented Programming
16
Polymorphism using overloading methods
A method can be overloaded in the same class or in a subclass.
In other words, if class A defines a doStuff(int i) method, the subclass
B could define a doStuff(String s).
So two methods with the same name but in different classes can still
be considered overloaded, if the subclass inherits one version of the
method and then declares another overloaded version in its class
definition.
Compiled by : Haileyesus Demissie
Object Oriented Programming
17
Polymorphism using overloading methods
Possible legal way of writing overloaded method
Compiled by : Haileyesus Demissie
Object Oriented Programming
18
Polymorphism using overloading methods
Example:
Compiled by : Haileyesus Demissie
Object Oriented Programming
19
Polymorphism using overloading methods
Example:
avg1 has a value 3.25
avg2 has a value 2.8
avg3 has a value 3.33
avg4 has a value 2.85
Compiled by : Haileyesus Demissie
Object Oriented Programming
20
The final Keyword
Final Class
When used in a class declaration, the final keyword means the class
can't be sub classed.
In other words, no other class can ever extend (inherit from) a final
class, and any attempts to do so will give you a compiler error.
You should make a final class only if you need an absolute guarantee
that none of the methods in that class will ever be overridden.
Compiled by : Haileyesus Demissie
Object Oriented Programming
21
The final Keyword
Example:
public final class Grade {
public void importantMethod() { }
}
If you try to extend Grade class
class ExtendGrade extends Grade { }
We get an error something like
Can't subclass final classes: class
Grade class ExtendClass extends Grade{
1 error
Compiled by : Haileyesus Demissie
Object Oriented Programming
22
The final Keyword
Final Methods
The final keyword prevents a method from being overridden in a
subclass.
Preventing a subclass from overriding a method stifles many of the
benefits of OO including extensibility through polymorphism.
Compiled by : Haileyesus Demissie
Object Oriented Programming
23
The final Keyword
Example:
class SuperClass{
public final void showSample() {
System.out.println("One thing.");
}
}
It's legal to extend SuperClass, since the class isn't marked final, but we
can't override the final method showSample(), as the following code
attempts to do:
Compiled by : Haileyesus Demissie
Object Oriented Programming
24
The final Keyword
class SubClass extends SuperClass{
public void showSample() {
// Try to override the final
// superclass method
System.out.println("Another thing.");
}
Compiled by : Haileyesus Demissie
Object Oriented Programming
25
The final Keyword
Final Variable
Declaring a variable with the final keyword makes it impossible to
reinitialize that variable once it has been initialized with an explicit
value.
For primitives, this means that once the variable is assigned a value,
the value can't be altered.
For example, if you assign 10 to the int variable x, then x is going to
stay 10, forever.
Compiled by : Haileyesus Demissie
Object Oriented Programming
26
The final Keyword
Final object reference variable
A reference variable marked final can't ever be reassigned to refer to
a different object.
The data within the object can be modified, but the reference
variable cannot be changed.
In other words, a final reference still allows you to modify the state of
the object it refers to, but you can't modify the reference variable to
make it refer to a different object.
Burnthisin:therearenofinalobjects,onlyfinalreferences.
Compiled by : Haileyesus Demissie
Object Oriented Programming
27
The final Keyword
Constants: Java constants are created by marking variables static and
final. They should be named using uppercase letters with underscore
characters as separators:
Example: static final int MIN_HEIGHT;
Compiled by : Haileyesus Demissie
Object Oriented Programming
28
The final Keyword
Final Arguments
Method arguments are the variable declarations that appear in
between the parentheses in a method declaration.
public Record getRecord(int fileNumber, final int recNumber) {}
The variable recNumber is declared as final, which means it can't be modified
within the method.
Compiled by : Haileyesus Demissie
Object Oriented Programming
29
The final Keyword
Final Arguments
Method arguments are the variable declarations that appear in
between the parentheses in a method declaration.
public Record getRecord(int fileNumber, final int recNumber) {}
The variable recNumber is declared as final, which means it can't be modified
within the method.
Compiled by : Haileyesus Demissie
Object Oriented Programming
30
Abstraction
Abstraction
Abstraction: is a process of hiding the implementation
details from the user, only the functionality will be provided
to the user.
In other words, the user will have the information on what
the object does instead of how it does it.
In Java, abstraction is achieved using
Abstract classes and
Interfaces
Compiled by : Haileyesus Demissie
Object Oriented Programming
32
Abstract Class
A class which contains the abstract keyword in its
declaration is known as abstract class.
If a class is declared abstract, it cannot be instantiated.
Abstract classes may or may not contain abstract methods,
i.e., methods without body.
Example: public void getName();
But, if a class has at least one abstract method, then the
class must be declared abstract.
Compiled by : Haileyesus Demissie
Object Oriented Programming
33
Abstract Class
To use an abstract class, you have to inherit it from another
class, provide implementations to the abstract methods in it.
If you inherit an abstract class, you have to provide
implementations to all the abstract methods in it.
A concrete class is a regular class that can be instantiated.
An abstract class is different from a concrete class because it
cannot be instantiated and must be extended.
Compiled by : Haileyesus Demissie
Object Oriented Programming
34
Abstract Class
Example:
Public abstract class Person{ }
An abstract class can be sub classed
Public class Student extends Person{ }
An abstract class can not be instantiated
Person p1=new Person();
Compiled by : Haileyesus Demissie
Object Oriented Programming
35
Abstract Methods
An abstract class may contain abstract methods.
Abstract methods are methods that are not implemented.
They have a valid method signature but must be overridden
and implemented in the class that extends the abstract class.
An abstract method is one with keyword abstract in its
declaration.
Compiled by : Haileyesus Demissie
Object Oriented Programming
36
Abstract Methods
Example
Compiled by : Haileyesus Demissie
Object Oriented Programming
37
Abstract Methods
Generally abstract methods are:
Cannot have a method body
Must be declared in abstract class
Is overridden in subclass
When a child class inherits an abstract method, it is
inheriting a method signature but no implementation.
An abstract method is a way to guarantee that any child
class will contain a method with a matching signature.
Compiled by : Haileyesus Demissie
Object Oriented Programming
38
Abstract Methods
A class that contains any abstract methods must be declared
as an abstract class even if that class contains some concrete
(non-abstract) methods.
Constructors and static methods cannot be declared
abstract.
Constructors are not inherited, so an abstract constructor
could never be implemented.
Compiled by : Haileyesus Demissie
Object Oriented Programming
39
Abstract Methods
A class that contains any abstract methods must be declared
as an abstract class even if that class contains some concrete
(non-abstract) methods.
Constructors and static methods cannot be declared
abstract.
Constructors are not inherited, so an abstract constructor
could never be implemented.
Compiled by : Haileyesus Demissie
Object Oriented Programming
40
Interface
When you create an interface, you're defining a contract for
what a class can do, without saying anything about how the
class will do it.
An interface is a contract.
Interface: is a group of related methods with empty bodies.
For example you could write an interface Registration. Any
class type that implements this interface must agree to
write the code for the register() and withdrawal() methods.
Compiled by : Haileyesus Demissie
Object Oriented Programming
41
Interface
Interfaces can be implemented by any class, from any
inheritance tree.
To implement interface, you have to use the implements
keyword in the class declaration.
All interface methods must be implemented, and must be
marked public.
Compiled by : Haileyesus Demissie
Object Oriented Programming
42
Interface
Interfaces can be implemented by any class, from any
inheritance tree.
To implement interface, you have to use the implements
keyword in the class declaration.
All interface methods must be implemented, and must be
marked public.
Compiled by : Haileyesus Demissie
Object Oriented Programming
43
Interface
Compiled by : Haileyesus Demissie
Object Oriented Programming
44
Interface
Think of an interface as a 100-percent abstract class.
Like an abstract class, an interface defines abstract methods
that take the following form:
But while an abstract class can define both abstract and
non-abstract methods, an interface can have only abstract
methods
Compiled by : Haileyesus Demissie
Object Oriented Programming
45
Rules for Interface
All interface methods are implicitly public and abstract.
In other words, you do not need to actually type the public
or abstract modifiers in the method declaration, but the
method is still always public and abstract.
All variables defined in an interface must be public, static,
and final in other words, interfaces can declare only
constants, not instance variables.
Interface methods must not be static.
Compiled by : Haileyesus Demissie
Object Oriented Programming
46
Rules for Interface
The following is a legal interface declaration:
public abstract interface Registration { }
public interface Registration { }
The public modifier is required if you want the interface to
have public rather than default access.
Compiled by : Haileyesus Demissie
Object Oriented Programming
47
Rules for Interface
Methods within an interface:
public interface Registration {
public abstract void register();
public abstract void add(String course);
}
Typing public and abstract modifiers on the methods is
redundant, though, since all interface methods are implicitly
public and abstract. Given that rule, you can see that the
following code is exactly equivalent to the preceding interface:
Compiled by : Haileyesus Demissie
Object Oriented Programming
48
Rules for Interface
Methods within an interface:
public interface Registration {
void register(); // No modifiers
void add(String course); // No modifiers
}
Compiled by : Haileyesus Demissie
Object Oriented Programming
49
Rules for Interface
All interface methods are public and abstract regardless of
what you see in the interface definition.
The following five method declarations are legal and
identical
void register();
public void register();
abstract void register();
public abstract void register();
abstract public void register();
Compiled by : Haileyesus Demissie
Object Oriented Programming
50
Rules for Interface
The following interface method declarations won't compile:
final void register(); // final and abstract can never be used together
static void register (); // interfaces define instance methods
private void register (); // interface methods are always public
protected void register(); // (same as above)
Compiled by : Haileyesus Demissie
Object Oriented Programming
51
Declaring Interface Constant
You're allowed to put constants in an interface. By doing so,
you guarantee that any class implementing the interface will
have access to the same constant.
You need to remember one key rule for interface constants.
They must always be public static final
Compiled by : Haileyesus Demissie
Object Oriented Programming
52
Declaring Interface Constant
For example, the following are all identical:
public int x = 1; // Looks non-static and non-final, but isn't!
int x = 1; // Looks default, non-final, non-static, but isn't!
static int x = 1; // Doesn't show final or public
final int x = 1; // Doesn't show static or public
public static int x = 1; // Doesn't show final
public final int x = 1; // Doesn't show static
static final int x = 1; // Doesn't show public
public static final int x = 1; // what you get implicitly
Compiled by : Haileyesus Demissie
Object Oriented Programming
53
THANK YOU
“Any fool can write code that a
computer can understand.
Good programmers write code
that humans can understand. ”
Compiled by : Haileyesus Demissie
Object Oriented Programming
54
Download