SlideShare a Scribd company logo
Polymorphism in java
Elizabeth Alexander
Hindustan University
POLYMORPHISM
● Polymorphism is the ability of an object to take on many forms.
● A concept by which we can perform a single action by different ways.
● Polymorphism is derived from 2 greek words: poly and morphs. The word
"poly" means many and "morphs" means forms. So polymorphism means
many forms.
Method Overloading
● Method Overloading is a feature that allows a class to have two or more
methods having same name, if their argument lists are different
● Argument lists could differ in –
■ 1. Number of parameters.
■ 2. Data type of parameters.
■ 3. Sequence of Data type of parameters.
METHOD OVERLOADING
● Method overloading is also known as Static Polymorphism.
1. Static Polymorphism is also known as compile time binding or early
binding.
2. Static binding happens at compile time. Method overloading is an example
of static binding where binding of method call to its definition happens at Compile
time.
● Overloading – Different Number of parameters in argument list
Eg: void disp(char c)
void disp(char c, int num)
● Overloading – Difference in data type of arguments
Eg: void disp(char c)
void disp(int c)
METHOD OVERLOADING Cont...
● Overloading – Sequence of data type of arguments
Eg: void disp(char c, int num)
void disp(int num, char c)
Valid/invalid cases of method overloading
● Case 1: int mymethod(int a, int b, float c)
int mymethod(int var1, int var2, float var3)
Result: Compile time error. Argument lists are exactly same. Both methods are
having same number, data types and same sequence of data types in arguments.
● Case 2: int mymethod(int a, int b)
int mymethod(float var1, float var2)
Result: Perfectly fine. Valid case for overloading. Here data types of arguments
are different.
METHOD OVERLOADING Cont...
● Case 3: int mymethod(int a, int b)
int mymethod(int num)
Result: Perfectly fine. Valid case for overloading. Here number of arguments are
different.
● Case 4: float mymethod(int a, float b)
float mymethod(float var1, int var2)
Result: Perfectly fine. Valid case for overloading. Sequence of the data types are
different, first method is having (int, float) and second is having (float, int).
● Case 5: int mymethod(int a, int b)
float mymethod(int var1, int var2)
Result: Compile time error. Argument lists are exactly same. Even though return
type of methods are different, it is not a valid case. Since return type of method
doesn’t matter while overloading a method.
METHOD OVERRIDING
● Overriding is a feature that allows a subclass or child class to provide a specific
implementation of a method that is already provided by one of its super-classes
or parent classes.
● When a method in a subclass has the same name, same parameters or
signature and same return type(or sub-type) as a method in its super-class,
then the method in the subclass is said to override the method in the super-
class.
● In such cases child class overrides the parent class method without even
touching the source code of the base class. This feature is known as method
overriding.
Usage of Java Method Overriding
● Method overriding is used to provide specific implementation of a method that is
already provided by its super class.
METHOD OVERRIDING Cont...
● The version of a method that is executed will be determined by the object that is
used to invoke it.
● If an object of a parent class is used to invoke the method, then the version in the
parent class will be executed, but if an object of the subclass is used to invoke the
method, then the version in the child class will be executed.
● In other words, it is the type of the object being referred to (not the type of the
reference variable) that determines which version of an overridden method will be
executed.
METHOD OVERRIDING Cont...
● When reference variable of Parent class refers to the object of Child class, it is
known as upcasting.
Eg: class A{}
class B extends A{}
A a=new B();//upcasting
RULES FOR METHOD OVERRIDING
● The argument list should be exactly the same as that of the overridden method.
● The return type should be the same or a subtype of the return type declared in the
original overridden method in the superclass.
● The access level cannot be more restrictive than the overridden method's access
level. For example: If the superclass method is declared public then the overriding
method in the sub class cannot be either private or protected.
● Instance methods can be overridden only if they are inherited by the subclass.
RULES FOR METHOD OVERRIDING Cont...
● A method declared static cannot be overridden but can be re-declared.
● If a method cannot be inherited, then it cannot be overridden.
● A subclass within the same package as the instance's superclass can override
any superclass method that is not declared private or final.
● A subclass in a different package can only override the non-final methods
declared public or protected.
● An overriding method can throw any uncheck exceptions, regardless of whether
the overridden method throws exceptions or not. However, the overriding method
should not throw checked exceptions that are new or broader than the ones
declared by the overridden method. The overriding method can throw narrower or
fewer exceptions than the overridden method.
● Constructors cannot be overridden.
Using the super Keyword
DIFFERENCE BETWEEN METHOD OVERLOADING AND
OVERRIDING
NOTES
● Static method cannot be overridden because static method is bound with class
whereas instance method is bound with object. Static belongs to class area and
instance belongs to heap area.
● java main method cannot be overridden because main is a static method.
JAVA-ABSTRACTION
● In Object-oriented programming, 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.
Abstract Class
● A class which contains the abstract keyword in its declaration is known as
abstract class.
● Abstract classes may or may not contain abstract methods, i.e., methods without
body ( public void get(); )
● But, if a class has at least one abstract method, then the class must be declared
abstract.
ABSTRACT CLASS
● If a class is declared abstract, it cannot be instantiated.
● Abstract classes cannot be instantiated, means we can't create an object to
Abstract class.
● We can create Subclasses to Abstract classes. 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.
ABSTRACT METHODS
● If you want a class to contain a particular method but you want the actual
implementation of that method to be determined by child classes, you can declare
the method in the parent class as an abstract.
ABSTRACT METHODS Cont...
● abstract keyword is used to declare the method as abstract.
● You have to place the abstract keyword before the method name in the method
declaration.
● An abstract method contains a method signature, but no method body.
● Instead of curly braces, an abstract method will have a semoi colon (;) at the end.
● Declaring a method as abstract has two consequences −
■ The class containing it must be declared as abstract.
■ Any class inheriting the current class must either override the abstract
method or declare itself as abstract.
● Note − Eventually, a descendant class has to implement the abstract method;
otherwise, you would have a hierarchy of abstract classes that cannot be
instantiated.
JAVA MODIFIERS
● Modifiers are keywords that you add to those definitions to change their
meanings. Java language has a wide variety of modifiers, including the following
■ Java Access Modifiers
■ Non Access Modifiers
Access Control Modifiers
● Java provides a number of access modifiers to set access levels for classes,
variables, methods and constructors. The four access levels are −
■ Visible to the package, the default. No modifiers are needed.
■ Visible to the class only (private).
■ Visible to the world (public).
■ Visible to the package and all subclasses (protected).
JAVA MODIFIERS Cont...
DEFAULT ACCESS MODIFIER - No Keyword
● Default access modifier means we do not explicitly declare an access modifier for
a class, field, method, etc.
● A variable or method declared without any access control modifier is available to
any other class in the same package. The fields in an interface are implicitly
public static final and the methods in an interface are by default public.
PRIVATE ACCESS MODIFIER - Private
● Methods, variables, and constructors that are declared private can only be
accessed within the declared class itself.
● Private access modifier is the most restrictive access level. Class and interfaces
cannot be private.
● Variables that are declared private can be accessed outside the class, if public
getter methods are present in the class.
● Using the private modifier is the main way that an object encapsulates itself and
hides data from the outside world.
JAVA MODIFIERS Cont...
PUBLIC ACCESS MODIFIER-Public
● A class, method, constructor, interface, etc. declared public can be accessed from
any other class.
PROTECTED ACCESS MODIFIER- Protected
● Variables, methods, and constructors, which are declared protected in a
superclass can be accessed only by the subclasses in other package or any class
within the package of the protected members' class.
● The protected access modifier cannot be applied to class and interfaces.
Methods, fields can be declared protected, however methods and fields in a
interface cannot be declared protected.
ACCESS CONTROL AND INHERITANCE
● The following rules for inherited methods are enforced −
● Methods declared public in a superclass also must be public in all subclasses.
● Methods declared protected in a superclass must either be protected or public
in subclasses; they cannot be private.
● Methods declared private are not inherited at all, so there is no rule for them.
NON-ACCESS MODIFIERS
● Java provides a number of non-access modifiers to achieve many other
functionality.
● The static modifier for creating class methods and variables.
● The final modifier for finalizing the implementations of classes, methods, and
variables.
● The abstract modifier for creating abstract classes and methods.
● The synchronized and volatile modifiers, which are used for threads.
THE FINAL MODIFIER
Final Variable
● A final variable can be explicitly initialized only once. A reference variable
declared final can never be reassigned to refer to an different object.
● The data within the object can be changed. So, the state of the object can be
changed but not the reference.
● With variables, the final modifier often is used with static to make the
constant a class variable.
Final Methods
● A final method cannot be overridden by any subclasses. As mentioned
previously, the final modifier prevents a method from being modified in a
subclass.
● The main intention of making a method final would be that the content of the
method should not be changed by any outsider.
FINAL METHODS
Example:
public class Test {
public final void changeName() {
// body of method
}
}
FINAL CLASSES
● The main purpose of using a class being declared as final is to prevent the class
from being subclassed. If a class is marked as final then no class can inherit any
feature from the final class.
Example
public final class Test {
// body of class
}
Ad

More Related Content

What's hot (20)

This keyword in java
This keyword in javaThis keyword in java
This keyword in java
Hitesh Kumar
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
Kongu Engineering College, Perundurai, Erode
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
Data types in java
Data types in javaData types in java
Data types in java
HarshitaAshwani
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
Ahsan Raja
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
Lovely Professional University
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
Ankita Totala
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
Madishetty Prathibha
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Static Members-Java.pptx
Static Members-Java.pptxStatic Members-Java.pptx
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Java interface
Java interfaceJava interface
Java interface
BHUVIJAYAVELU
 

Similar to Polymorphism in java (20)

Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.doc
Joyce Thomas
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
Arun Vasanth
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
homeworkping9
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
Shiva Cse
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
Vinay Kumar
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
Thakur Amit Tomer
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
Rosie Jane Enomar
 
Chapter 5 declaring classes & oop
Chapter 5 declaring classes  & oopChapter 5 declaring classes  & oop
Chapter 5 declaring classes & oop
sshhzap
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
Shashwat Shriparv
 
5- Overriding and Abstraction In Java
5- Overriding and Abstraction In Java5- Overriding and Abstraction In Java
5- Overriding and Abstraction In Java
Ghadeer AlHasan
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
KartikKapgate
 
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
SAJITHABANUS
 
Java session2
Java session2Java session2
Java session2
Rajeev Kumar
 
Object Oriented Principles
Object Oriented PrinciplesObject Oriented Principles
Object Oriented Principles
Sujit Majety
 
object oriented programming unit two ppt
object oriented programming unit two pptobject oriented programming unit two ppt
object oriented programming unit two ppt
isiagnel2
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
talha ijaz
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
Madhavendra Dutt
 
Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)
Nuzhat Memon
 
polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdf
BapanKar2
 
javainheritance
javainheritancejavainheritance
javainheritance
Arjun Shanka
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.doc
Joyce Thomas
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
Arun Vasanth
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
homeworkping9
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
Shiva Cse
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
Vinay Kumar
 
Chapter 5 declaring classes & oop
Chapter 5 declaring classes  & oopChapter 5 declaring classes  & oop
Chapter 5 declaring classes & oop
sshhzap
 
5- Overriding and Abstraction In Java
5- Overriding and Abstraction In Java5- Overriding and Abstraction In Java
5- Overriding and Abstraction In Java
Ghadeer AlHasan
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
KartikKapgate
 
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
SAJITHABANUS
 
Object Oriented Principles
Object Oriented PrinciplesObject Oriented Principles
Object Oriented Principles
Sujit Majety
 
object oriented programming unit two ppt
object oriented programming unit two pptobject oriented programming unit two ppt
object oriented programming unit two ppt
isiagnel2
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
Madhavendra Dutt
 
Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)
Nuzhat Memon
 
polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdf
BapanKar2
 
Ad

More from Elizabeth alexander (11)

Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
Java applet
Java appletJava applet
Java applet
Elizabeth alexander
 
Io streams
Io streamsIo streams
Io streams
Elizabeth alexander
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
Elizabeth alexander
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
Elizabeth alexander
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
Elizabeth alexander
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
Elizabeth alexander
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Quantitative Aptitude- Number System
Quantitative Aptitude- Number SystemQuantitative Aptitude- Number System
Quantitative Aptitude- Number System
Elizabeth alexander
 
Java Programming
Java ProgrammingJava Programming
Java Programming
Elizabeth alexander
 
Ad

Recently uploaded (20)

DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Basic Principles for Electronics Students
Basic Principles for Electronics StudentsBasic Principles for Electronics Students
Basic Principles for Electronics Students
cbdbizdev04
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
vlsi digital circuits full power point presentation
vlsi digital circuits full power point presentationvlsi digital circuits full power point presentation
vlsi digital circuits full power point presentation
DrSunitaPatilUgaleKK
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.
Kamal Acharya
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Dust Suppressants: A Sustainable Approach to Dust Pollution Control
Dust Suppressants: A Sustainable Approach to Dust Pollution ControlDust Suppressants: A Sustainable Approach to Dust Pollution Control
Dust Suppressants: A Sustainable Approach to Dust Pollution Control
Janapriya Roy
 
BCS401 ADA Second IA Test Question Bank.pdf
BCS401 ADA Second IA Test Question Bank.pdfBCS401 ADA Second IA Test Question Bank.pdf
BCS401 ADA Second IA Test Question Bank.pdf
VENKATESHBHAT25
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Lecture 13 (Air and Noise Pollution and their Control) (1).pptx
Lecture 13 (Air and Noise Pollution and their Control) (1).pptxLecture 13 (Air and Noise Pollution and their Control) (1).pptx
Lecture 13 (Air and Noise Pollution and their Control) (1).pptx
huzaifabilalshams
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Upstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptxUpstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptx
KshitijJayswal2
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Taking AI Welfare Seriously, In this report, we argue that there is a realist...
Taking AI Welfare Seriously, In this report, we argue that there is a realist...Taking AI Welfare Seriously, In this report, we argue that there is a realist...
Taking AI Welfare Seriously, In this report, we argue that there is a realist...
MiguelMarques372250
 
Building Security Systems in Architecture.pdf
Building Security Systems in Architecture.pdfBuilding Security Systems in Architecture.pdf
Building Security Systems in Architecture.pdf
rabiaatif2
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Basic Principles for Electronics Students
Basic Principles for Electronics StudentsBasic Principles for Electronics Students
Basic Principles for Electronics Students
cbdbizdev04
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
vlsi digital circuits full power point presentation
vlsi digital circuits full power point presentationvlsi digital circuits full power point presentation
vlsi digital circuits full power point presentation
DrSunitaPatilUgaleKK
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.
Kamal Acharya
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Dust Suppressants: A Sustainable Approach to Dust Pollution Control
Dust Suppressants: A Sustainable Approach to Dust Pollution ControlDust Suppressants: A Sustainable Approach to Dust Pollution Control
Dust Suppressants: A Sustainable Approach to Dust Pollution Control
Janapriya Roy
 
BCS401 ADA Second IA Test Question Bank.pdf
BCS401 ADA Second IA Test Question Bank.pdfBCS401 ADA Second IA Test Question Bank.pdf
BCS401 ADA Second IA Test Question Bank.pdf
VENKATESHBHAT25
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Lecture 13 (Air and Noise Pollution and their Control) (1).pptx
Lecture 13 (Air and Noise Pollution and their Control) (1).pptxLecture 13 (Air and Noise Pollution and their Control) (1).pptx
Lecture 13 (Air and Noise Pollution and their Control) (1).pptx
huzaifabilalshams
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Upstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptxUpstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptx
KshitijJayswal2
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Taking AI Welfare Seriously, In this report, we argue that there is a realist...
Taking AI Welfare Seriously, In this report, we argue that there is a realist...Taking AI Welfare Seriously, In this report, we argue that there is a realist...
Taking AI Welfare Seriously, In this report, we argue that there is a realist...
MiguelMarques372250
 
Building Security Systems in Architecture.pdf
Building Security Systems in Architecture.pdfBuilding Security Systems in Architecture.pdf
Building Security Systems in Architecture.pdf
rabiaatif2
 

Polymorphism in java

  • 1. Polymorphism in java Elizabeth Alexander Hindustan University
  • 2. POLYMORPHISM ● Polymorphism is the ability of an object to take on many forms. ● A concept by which we can perform a single action by different ways. ● Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms. Method Overloading ● Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different ● Argument lists could differ in – ■ 1. Number of parameters. ■ 2. Data type of parameters. ■ 3. Sequence of Data type of parameters.
  • 3. METHOD OVERLOADING ● Method overloading is also known as Static Polymorphism. 1. Static Polymorphism is also known as compile time binding or early binding. 2. Static binding happens at compile time. Method overloading is an example of static binding where binding of method call to its definition happens at Compile time. ● Overloading – Different Number of parameters in argument list Eg: void disp(char c) void disp(char c, int num) ● Overloading – Difference in data type of arguments Eg: void disp(char c) void disp(int c)
  • 4. METHOD OVERLOADING Cont... ● Overloading – Sequence of data type of arguments Eg: void disp(char c, int num) void disp(int num, char c) Valid/invalid cases of method overloading ● Case 1: int mymethod(int a, int b, float c) int mymethod(int var1, int var2, float var3) Result: Compile time error. Argument lists are exactly same. Both methods are having same number, data types and same sequence of data types in arguments. ● Case 2: int mymethod(int a, int b) int mymethod(float var1, float var2) Result: Perfectly fine. Valid case for overloading. Here data types of arguments are different.
  • 5. METHOD OVERLOADING Cont... ● Case 3: int mymethod(int a, int b) int mymethod(int num) Result: Perfectly fine. Valid case for overloading. Here number of arguments are different. ● Case 4: float mymethod(int a, float b) float mymethod(float var1, int var2) Result: Perfectly fine. Valid case for overloading. Sequence of the data types are different, first method is having (int, float) and second is having (float, int). ● Case 5: int mymethod(int a, int b) float mymethod(int var1, int var2) Result: Compile time error. Argument lists are exactly same. Even though return type of methods are different, it is not a valid case. Since return type of method doesn’t matter while overloading a method.
  • 6. METHOD OVERRIDING ● Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. ● When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super- class. ● In such cases child class overrides the parent class method without even touching the source code of the base class. This feature is known as method overriding. Usage of Java Method Overriding ● Method overriding is used to provide specific implementation of a method that is already provided by its super class.
  • 7. METHOD OVERRIDING Cont... ● The version of a method that is executed will be determined by the object that is used to invoke it. ● If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed. ● In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed.
  • 8. METHOD OVERRIDING Cont... ● When reference variable of Parent class refers to the object of Child class, it is known as upcasting. Eg: class A{} class B extends A{} A a=new B();//upcasting RULES FOR METHOD OVERRIDING ● The argument list should be exactly the same as that of the overridden method. ● The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass. ● The access level cannot be more restrictive than the overridden method's access level. For example: If the superclass method is declared public then the overriding method in the sub class cannot be either private or protected. ● Instance methods can be overridden only if they are inherited by the subclass.
  • 9. RULES FOR METHOD OVERRIDING Cont... ● A method declared static cannot be overridden but can be re-declared. ● If a method cannot be inherited, then it cannot be overridden. ● A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. ● A subclass in a different package can only override the non-final methods declared public or protected. ● An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method. ● Constructors cannot be overridden. Using the super Keyword
  • 10. DIFFERENCE BETWEEN METHOD OVERLOADING AND OVERRIDING
  • 11. NOTES ● Static method cannot be overridden because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area. ● java main method cannot be overridden because main is a static method.
  • 12. JAVA-ABSTRACTION ● In Object-oriented programming, 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. Abstract Class ● A class which contains the abstract keyword in its declaration is known as abstract class. ● Abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); ) ● But, if a class has at least one abstract method, then the class must be declared abstract.
  • 13. ABSTRACT CLASS ● If a class is declared abstract, it cannot be instantiated. ● Abstract classes cannot be instantiated, means we can't create an object to Abstract class. ● We can create Subclasses to Abstract classes. 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. ABSTRACT METHODS ● If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as an abstract.
  • 14. ABSTRACT METHODS Cont... ● abstract keyword is used to declare the method as abstract. ● You have to place the abstract keyword before the method name in the method declaration. ● An abstract method contains a method signature, but no method body. ● Instead of curly braces, an abstract method will have a semoi colon (;) at the end. ● Declaring a method as abstract has two consequences − ■ The class containing it must be declared as abstract. ■ Any class inheriting the current class must either override the abstract method or declare itself as abstract. ● Note − Eventually, a descendant class has to implement the abstract method; otherwise, you would have a hierarchy of abstract classes that cannot be instantiated.
  • 15. JAVA MODIFIERS ● Modifiers are keywords that you add to those definitions to change their meanings. Java language has a wide variety of modifiers, including the following ■ Java Access Modifiers ■ Non Access Modifiers Access Control Modifiers ● Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are − ■ Visible to the package, the default. No modifiers are needed. ■ Visible to the class only (private). ■ Visible to the world (public). ■ Visible to the package and all subclasses (protected).
  • 16. JAVA MODIFIERS Cont... DEFAULT ACCESS MODIFIER - No Keyword ● Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. ● A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public. PRIVATE ACCESS MODIFIER - Private ● Methods, variables, and constructors that are declared private can only be accessed within the declared class itself. ● Private access modifier is the most restrictive access level. Class and interfaces cannot be private. ● Variables that are declared private can be accessed outside the class, if public getter methods are present in the class. ● Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.
  • 17. JAVA MODIFIERS Cont... PUBLIC ACCESS MODIFIER-Public ● A class, method, constructor, interface, etc. declared public can be accessed from any other class. PROTECTED ACCESS MODIFIER- Protected ● Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. ● The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.
  • 18. ACCESS CONTROL AND INHERITANCE ● The following rules for inherited methods are enforced − ● Methods declared public in a superclass also must be public in all subclasses. ● Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private. ● Methods declared private are not inherited at all, so there is no rule for them. NON-ACCESS MODIFIERS ● Java provides a number of non-access modifiers to achieve many other functionality. ● The static modifier for creating class methods and variables. ● The final modifier for finalizing the implementations of classes, methods, and variables. ● The abstract modifier for creating abstract classes and methods. ● The synchronized and volatile modifiers, which are used for threads.
  • 19. THE FINAL MODIFIER Final Variable ● A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to an different object. ● The data within the object can be changed. So, the state of the object can be changed but not the reference. ● With variables, the final modifier often is used with static to make the constant a class variable. Final Methods ● A final method cannot be overridden by any subclasses. As mentioned previously, the final modifier prevents a method from being modified in a subclass. ● The main intention of making a method final would be that the content of the method should not be changed by any outsider.
  • 20. FINAL METHODS Example: public class Test { public final void changeName() { // body of method } } FINAL CLASSES ● The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class. Example public final class Test { // body of class }