Lec 3
Lec 3
Lec 3
…
polymorphism
• the ability to take more than one form
• particular operation may behave differently
for different sub-classes of the same class
• the ability to refer to an object using either to
actual form or a parent form
• enables us to “program in the general” rather
than “program in the specific.”
Polymorphism using overridden method
• Overriding :
• allows the subclass to provide its own
implementation of the method, which its
signature is in the super class
• re implementing a method you inherited
• overridden method in the superclass is not
inherited by the subclass
Cont. …
• rules of method overriding
• new method definition must have the same
method signature
• If a method can't be inherited, you can’t
override it
• Final and static method can’t overridden
Cont. …
• Example
• The passing average grade for students is 2.0
and above. However, for CE students the passing
grade is 2.5 and above. if we have a class for CE
students, the class is slightly different from the
Student class. Hence, we can inherit the Student
class to CE class, but we have to override some
methods of the Student class
Student.java
•…
Ce.java
•…
Test.java
•…
Output
•…
Polymorphism using Overloading methods
• Overloading
• reuse the same method name in a class or
subclass
• Different argument (number/type)
• Different return type [optional]
Example
•…
Example
Output
•…
Final keyword
• Final class
• can't be sub classed
• can't ever be improved by another
programmer
• none of the methods in that class will ever
be overridden
• Provide absolute guarantee (security)
Cont. …
• Final method
• Can’t overridden in the subclass
• Provide safety and security
• Class can contain non final method
• You can subclass a class contain final
method
Cont. …
• Final variable
• Once value assigned stay for ever
• impossible to reinitialize
• A final reference variable can't ever be reassigned to
refer to a different object, but, data within the
object can be modified
• there are no final objects, only final references
• Final argument can't be modified within the method
Abstract keyword
• Abstract class
• cannot be instantiated and must be
extended
• used only as super classes
• Are incomplete
• may contain abstract methods
Cont. …
• Example :
• public abstract class MemberOfBiT {}
• public class Student extends
MemberOfBiT { // must be extendes }
• MemberOfBiT me = new MemberOfBiT();
// cannot be instantiated
Cont. …
• abstract methods :
• 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
Cont. …
• Example
Cont. …
• A class that contains any abstract methods must
be declared as an abstract class even if that class
contains some concrete (non abstract) methods.
• Each concrete subclass of an abstract superclass
also must provide concrete implementations of
each of the superclass’s abstract methods
Interface
• Is a contract for what a class can do, not how to do
• is a group of related methods with empty bodies
• can be implemented by any class
• use the implements keyword in the class declaration to implement
interface
• all interface methods must be implemented, and must be marked
public
• can defines abstract methods not both concrete and abstract method
• It is abstract by default
Cont. …
• Rules :
• interface methods are by default public and abstract
• All variables defined in an interface must be public, static, and
final (constant)
• methods must not be static
• An interface can extend one or more other interfaces
• An interface cannot implement another interface or class.
• An interface must be declared with the keyword interface.
• A class that implement an interface must be provide legal
implementations for every method defined in the interface
Cont. …
• An implementation class can itself be abstract
• You can extend only one class, but implement many
interfaces
• Example
• public class Class implements Interface1, Interface2,
Interface3 { ... }
• public interface Interface1 extends Interface2,
Interface3{}
Implementation of interface
•…
Cont. …
•…
“Not registered”
Cont. …
•…
Cont. …
•…
Cont. …
• Output