Download java lecture note for bachelor students and more Lecture notes Computer Science in PDF only on Docsity!
POLYMORPHISM
…
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 .”
Cont. …
(^) rules of method overriding
(^) new method definition must have the samemethod signature
(^) If a method can't be inherited , you can’toverride it
(^) Final and static method can’t overridden
Cont. …
(^) Example
(^) The passing average grade for students is 2.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
Ce.java
(^) …
Test.java
(^) …
Polymorphism using Overloading methods
(^) Overloading
(^) reuse the same method name in a class or subclass
(^) Different argument (number/type)
(^) Different return type [optional]
Example
(^) …
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
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. …
(^) 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