0% found this document useful (0 votes)
5 views

java lec 3

Uploaded by

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

java lec 3

Uploaded by

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

POLYMORPHISM

interpreted by Asaminew G. 12/03/2024 1


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.”
interpreted by Asaminew G. 12/03/2024 2
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
interpreted by Asaminew G. 12/03/2024 3
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
interpreted by Asaminew G. 12/03/2024 4
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
interpreted by Asaminew G. 12/03/2024 5
Student.java

•…

interpreted by Asaminew G. 12/03/2024 6


Ce.java

•…

interpreted by Asaminew G. 12/03/2024 7


Test.java

•…

interpreted by Asaminew G. 12/03/2024 8


Output
•…

interpreted by Asaminew G. 12/03/2024 9


Polymorphism using Overloading
methods
• Overloading
• reuse the same method name in a
class or subclass
• Different argument (number/type)
• Different return type [optional]

interpreted by Asaminew G. 12/03/2024 10


Example
•…

interpreted by Asaminew G. 12/03/2024 11


Example
Output

•…

interpreted by Asaminew G. 12/03/2024 12


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)
interpreted by Asaminew G. 12/03/2024 13
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
interpreted by Asaminew G. 12/03/2024 14
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
interpreted by Asaminew G. 12/03/2024 15
Abstract keyword
• Abstract class
• cannot be instantiated and must be
extended
• used only as super classes
• Are incomplete
• may contain abstract methods
interpreted by Asaminew G. 12/03/2024 16
Cont. …
• Example :
• public abstract class MemberOfBiT {}
• public class Student extends
MemberOfBiT { // must be extendes }
• MemberOfBiT me = new MemberOfBiT();
// cannot be instantiated

interpreted by Asaminew G. 12/03/2024 17


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
interpreted by Asaminew G. 12/03/2024 18
Cont. …
• Example

interpreted by Asaminew G. 12/03/2024 19


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
interpreted by Asaminew G. 12/03/2024 20
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
interpreted by Asaminew G. 12/03/2024 21
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
interpreted by Asaminew G. 12/03/2024 22
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{}
interpreted by Asaminew G. 12/03/2024 23
Implementation of interface
•…

interpreted by Asaminew G. 12/03/2024 24


Cont. …

•…

“Not
registered”

interpreted by Asaminew G. 12/03/2024 25


Cont. …

•…

“Not
registered”

interpreted by Asaminew G. 12/03/2024 26


Cont. …

•…

interpreted by Asaminew G. 12/03/2024 27


Cont. …

• Output

interpreted by Asaminew G. 12/03/2024 28

You might also like