6001104-3 Structured Programming L. Rafika
Description: 6001104-3 Structured Programming L. Rafika Maaroufi 1 Chapter 7: Endomorphism, Abstract Interface in Java 2 Outline Polymorphism Abstract Interface 3 Polymorphism in java Polymorphism comes from Greek meaning many forms. Polymorphism is
Related Topics
Download Presentation
"6001104-3 Structured Programming L. Rafika" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Presentation Transcript
slide1. 6001104-3 Structured Programming L. Rafika Maaroufi 1<br>
slide2. Chapter 7: Endomorphism, Abstract & Interface in Java 2<br>
slide3. Outline Polymorphism
Abstract
Interface 3<br>
slide4. Polymorphism in java Polymorphism comes from Greek meaning “many forms.”
Polymorphism is one of the OOPs feature that allows us to perform a single action in different ways.
In the context of object oriented design, polymorphism refers to the ability of a reference variable to take different forms.
In particular, polymorphism enables us to write programs that process objects that share the same superclass in a class hierarchy as if they are all objects of the superclass. 4<br>
slide5. Polymorphism in java polymorphism allows you define one interface and have multiple implementations.
There are two types of polymorphism in java:
Runtime polymorphism (Static Polymorphism)
Compile time polymorphism (Dynamic Polymorphism) 5<br>
slide6. Compile time Polymorphism Polymorphism that is resolved during compiler time is known as static polymorphism.
Example: Method overloading
Method overloading:
is a feature that allows a class to have more than one method having the same name, if their argument lists are different 6<br>
slide7. Example of Compile time Polymorphism T 7<br>
slide8. Runtime Polymorphism It is also known as Dynamic Polymorphism
is a process in which a call to an overridden method is resolved at runtime.
Example: Method Overriding
Method Overriding:
Declaring a method in sub class which is already present in parent class
Overriding is done so that a child class can give its own implementation to a method which is already provided by the parent class 8<br>
slide9. Runtime Polymorphism Rules for Java Method Overriding:
The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.
There must be an IS-A relationship (inheritance). 9<br>
slide10. Example of Runtime Polymorphism (1/3) Consider a scenario where Bank is a class that provides functionality to get the rate of interest. However, the rate of interest varies according to banks.
For example, SBI, ICICI and AXIS banks could provide 8%, 7%, and 9% rate of interest. 10<br>
slide11. Example of Runtime Polymorphism (2/3) T 11<br>
slide12. Example of Runtime Polymorphism (3/3) T 12<br>
slide13. Abstract Classes Abstract class: a class that is declared using “abstract” keyword.
It can have abstract methods(methods without body)
An abstract class can not be instantiated, which means you are not allowed to create an object of it.
A class has an abstract method, it must be declared class abstract.
A normal class(non-abstract class) cannot have abstract methods. 13<br>
slide14. Abstract class declaration //Declaration using abstract keyword
abstract class A{
//This is abstract method
abstract void myMethod();
//This is concrete method with body
void anotherMethod(){
//Does something
}
} 14<br>
slide15. Basics and example of abstract method. 1) Abstract method has no body.
2) Always end the declaration with a semicolon(;).
3) It must be overridden. An abstract class must be extended and in a same way abstract method must be overridden. 15 Note: The class which is extending abstract class must override all the abstract methods.<br>
slide16. Why we need an abstract class? Example:
We have a class Animal that has a method sound() and the subclasses of it like Dog, Lion, Horse, Cat etc.
Since the animal sound differs from one animal to another, there is no point to implement this method in parent class.
This is because every child class must override this method to give its own implementation details, like Lion class will say “Roar” in this method and Dog class will say “Woof”. 16<br>
slide17. Why we need an abstract class? (cont.) 17<br>
slide18. Interface in java Abstract class is used for achieving partial abstraction
An interface is used for full abstraction
Interface looks like a class but it is not a class.
An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body).
The variables declared in an interface are public, static & final by default 18<br>
slide19. Interface in java (cont.) Interface cannot be declared as private, protected or transient.
All the interface methods are by default abstract and public.
While providing implementation in class of any method of an interface, it needs to be mentioned as public.
implements keyword is used by classes to implement an interface. 19 Note: Class implements interface but an interface extends
another interface.<br>
slide20. The use of interface in Java The interface is used for full abstraction
Java programming language does not allow you to extend more than one class, However you can implement more than one interfaces in your class.
In java, multiple inheritance is not allowed, however you can use interface to make use of it as you can implement more than one interface.
The class that implements interface must implement all the methods of that interface 20<br>
slide21. Interface declaration in java (cont.) Interfaces are declared by specifying a keyword “interface”.
Syntax : 21 interface MyInterface {
//All the methods are public abstract by default
//As you see they have no body
public void method1();
public void method2();
} Note: Class implements interface but an interface extends
another interface.<br>
slide22. Example T 22<br>
slide23. References Java: How to Program, 9e, Dietel and Dietel, Pearson 0273759760
Data Structures and Algorithms in Java, 6th edition, by M.T. Goodrich and R. Tamassia. John Wiley and Sons, Inc., ISBN: 0-471-73884-0
Java The Complete Reference Ninth Edition 23<br>
slide2. Chapter 7: Endomorphism, Abstract & Interface in Java 2<br>
slide3. Outline Polymorphism
Abstract
Interface 3<br>
slide4. Polymorphism in java Polymorphism comes from Greek meaning “many forms.”
Polymorphism is one of the OOPs feature that allows us to perform a single action in different ways.
In the context of object oriented design, polymorphism refers to the ability of a reference variable to take different forms.
In particular, polymorphism enables us to write programs that process objects that share the same superclass in a class hierarchy as if they are all objects of the superclass. 4<br>
slide5. Polymorphism in java polymorphism allows you define one interface and have multiple implementations.
There are two types of polymorphism in java:
Runtime polymorphism (Static Polymorphism)
Compile time polymorphism (Dynamic Polymorphism) 5<br>
slide6. Compile time Polymorphism Polymorphism that is resolved during compiler time is known as static polymorphism.
Example: Method overloading
Method overloading:
is a feature that allows a class to have more than one method having the same name, if their argument lists are different 6<br>
slide7. Example of Compile time Polymorphism T 7<br>
slide8. Runtime Polymorphism It is also known as Dynamic Polymorphism
is a process in which a call to an overridden method is resolved at runtime.
Example: Method Overriding
Method Overriding:
Declaring a method in sub class which is already present in parent class
Overriding is done so that a child class can give its own implementation to a method which is already provided by the parent class 8<br>
slide9. Runtime Polymorphism Rules for Java Method Overriding:
The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.
There must be an IS-A relationship (inheritance). 9<br>
slide10. Example of Runtime Polymorphism (1/3) Consider a scenario where Bank is a class that provides functionality to get the rate of interest. However, the rate of interest varies according to banks.
For example, SBI, ICICI and AXIS banks could provide 8%, 7%, and 9% rate of interest. 10<br>
slide11. Example of Runtime Polymorphism (2/3) T 11<br>
slide12. Example of Runtime Polymorphism (3/3) T 12<br>
slide13. Abstract Classes Abstract class: a class that is declared using “abstract” keyword.
It can have abstract methods(methods without body)
An abstract class can not be instantiated, which means you are not allowed to create an object of it.
A class has an abstract method, it must be declared class abstract.
A normal class(non-abstract class) cannot have abstract methods. 13<br>
slide14. Abstract class declaration //Declaration using abstract keyword
abstract class A{
//This is abstract method
abstract void myMethod();
//This is concrete method with body
void anotherMethod(){
//Does something
}
} 14<br>
slide15. Basics and example of abstract method. 1) Abstract method has no body.
2) Always end the declaration with a semicolon(;).
3) It must be overridden. An abstract class must be extended and in a same way abstract method must be overridden. 15 Note: The class which is extending abstract class must override all the abstract methods.<br>
slide16. Why we need an abstract class? Example:
We have a class Animal that has a method sound() and the subclasses of it like Dog, Lion, Horse, Cat etc.
Since the animal sound differs from one animal to another, there is no point to implement this method in parent class.
This is because every child class must override this method to give its own implementation details, like Lion class will say “Roar” in this method and Dog class will say “Woof”. 16<br>
slide17. Why we need an abstract class? (cont.) 17<br>
slide18. Interface in java Abstract class is used for achieving partial abstraction
An interface is used for full abstraction
Interface looks like a class but it is not a class.
An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body).
The variables declared in an interface are public, static & final by default 18<br>
slide19. Interface in java (cont.) Interface cannot be declared as private, protected or transient.
All the interface methods are by default abstract and public.
While providing implementation in class of any method of an interface, it needs to be mentioned as public.
implements keyword is used by classes to implement an interface. 19 Note: Class implements interface but an interface extends
another interface.<br>
slide20. The use of interface in Java The interface is used for full abstraction
Java programming language does not allow you to extend more than one class, However you can implement more than one interfaces in your class.
In java, multiple inheritance is not allowed, however you can use interface to make use of it as you can implement more than one interface.
The class that implements interface must implement all the methods of that interface 20<br>
slide21. Interface declaration in java (cont.) Interfaces are declared by specifying a keyword “interface”.
Syntax : 21 interface MyInterface {
//All the methods are public abstract by default
//As you see they have no body
public void method1();
public void method2();
} Note: Class implements interface but an interface extends
another interface.<br>
slide22. Example T 22<br>
slide23. References Java: How to Program, 9e, Dietel and Dietel, Pearson 0273759760
Data Structures and Algorithms in Java, 6th edition, by M.T. Goodrich and R. Tamassia. John Wiley and Sons, Inc., ISBN: 0-471-73884-0
Java The Complete Reference Ninth Edition 23<br>