Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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
}

More Related Content

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 }