Salient Features of India constitution especially power and functions
Pi j3.2 polymorphism
1. Programming in Java
5-day workshop
OOP Polymorphism
Matt Collison
JP Morgan Chase 2021
PiJ3.2: OOP Polymorphism
2. Four principles of OOP:
1.Encapsulation – defining a class
2.Abstraction – modifying the objects
3.Inheritance – extending classes
4.Polymorphism – implementing interfaces
3. How do we achieve abstraction through OOP?
• Classes and objects: The first order of abstraction. class new
• There are lots of things that the same but with different attribute values.
• For example people – we all have a name, a height, we have interests and friends. Think of a
social media platform. Everyone has a profile but everyone’s profile is unique.
• Class inheritance: The second order of abstraction extends super instanceof
• The definitions for things can share common features.
• For example the administrator account and a user account both require a username,
password and contact details. Each of these account would extend a common ‘parent’
account definition.
• Polymorphism: The third order of abstraction interface implements @Override
• The definitions for things can share features but complete them in different ways to achieve
different behaviours. For example an e-commerce store must have a database binding, a
layout schema for the UI but the specific implementations are unique. These aren’t just
different values but different functions.
4. Interfaces
public interface ParentInterfaceIdentifier{
}
public class Identifier implements
ParentInterfaceIdentifier {
}
• An interface behaves like a programming ‘contract’
• Client: provides the interface
• Service provider: provides implementation(s)
6. Defining an interface
• An interface is a reference type, similar to a class, which variables can be declared
as (although not instantiated).
Contains:
• Constructors:
• No constructor(s)
• Attributes:
• Only static constants e.g. int A = 1; //implicitly public, static, and final
• Methods:
• Abstract methods e.g. double area(); //implicitly public and abstract
• Since Java 8, two types of non-abstract (i.e. concrete) methods are allowed.
• static void help(){...} //with body
• default void newFunction(){...} //with body
7. Rules of interfaces
• Interfaces cannot be instantiated.
• Interfaces cannot contain instance fields.
• If any non-abstract class implements an interface, it must implement
all the abstract methods in the interface. Otherwise, the class must
be declared as abstract.
8. Inheritance recap
• Superclass or base, parent class
• Subclass or derived, child, extended class
• Single inheritance only – there can only be one superclass for each
class. This creates a hierarchy or tree.
9. Example: the abstract Shape class
public abstract class Shape {
private String colour;
public Shape(String colour){ this.colour = colour; }
public Shape(){ colour = "black"; }
// Getters and Setters
public void setColour(String colour) { this.colour = colour; }
public String getColour() { return colour; }
// Two abstract methods
public abstract double area();
public abstract double perimeter();
}
10. Example: the Shape interface
/** * A Shape interface */
public interface Shape {
// Two abstract methods
double area();
double perimeter();
// A default method with body
default void printAreaPerimeter() {
System.out.println( area() );
System.out.println( perimeter() );
}
}
11. Example: the Shape interface
public class Rectangle implements Shape {
private double width, height;
private String colour;
public Rectangle(double width, double height, String colour) {
this.colour = colour; this.width = width; this.height = height;
}
@Override // informing compiler of intended override
public double area() { return width * height; }
@Override // informing compiler of intended override
public double perimeter() { return 2.0 * (width + height); }
// ... other methods
}
12. Example: the Shape interface
public class ShapeApp {
public static void main(String[] args) {
//declare them all shapes, but instantiate them
//with different implementation classes
Shape s1, s2, s3;
s1 = new Rectangle(3.0,4.0,"green");
System.out.println(s1);
s2 = new Rectangle(3.0,4.0,"green");
s2.printAreaPerimeter(); //call the default method
s3 = new Triangle(3.0,4.0,5.0,"green");
System.out.println(s3.toString());
System.out.println(s1.equals(s2));
System.out.println(s3.equals(s2));
}
}
//Rectangle@7b23ec81
//true
//false
13. Implementing multiple interfaces
• A class can implement multiple interfaces:
class PowerPoint implements Printable, Showable{...}
• A class can both extend one class and implement multiple interfaces:
class PowerPoint extends OfficeSoftware implements Printable,
Showable{...} //first extends, then implements
14. Implementing multiple interfaces
// A demo for implementing multiple interfaces
interface Printable { void print(); //implicitly public abstract }
interface Showable { void show(); //implicitly public abstract }
public class PowerPoint implements Printable, Showable {
public void print() { System.out.println("Print the PPT"); }
public void show() { System.out.println("Show the PPT"); }
public static void main(String args[]) {
PowerPoint obj = new PowerPoint();
obj.print();
obj.show();
}
}
15. Implementing multiple interfaces
Q: Java does not support multiple inheritance through classes, but
allows implementing multiple interfaces. Why?
A: Ambiguity may occur in the case of class for the concrete method to
use, but not for an interface.
• For example, there will be no conflicts if both the Printable interface
and Showable interface has the same method: print().
• A slight wrinkle when there are conflicting default methods...
16. Abstract classes vs. Interfaces
Similarity:
• Both can not be instantiated.
• Both can include abstract methods, i.e., only with method signature,
no implementation.
• Both are usually used as a general type parameter — when a variable
is declared — while the actual action of the method depends on the
subclasses, i.e., polymorphism
17. Abstract classes vs. Interfaces
Difference (recall the class members):
• Constructors ...
• Attributes ...
• Methods ...
18. Rule of thumb
• Program to an interface, not an implementation.
• That is, make references at the superclass/interface; substitute with
subclass instances; and invoke methods defined in the
superclass/interface only.
void someMethod(Shape shape){
double a = shape.area();
double b = shape.perimeter();
...
}
19. Rule of thumb
• Program to an interface, not an implementation.
• This is a powerful tool for abstraction.
• The separation of interface and implementation enables better
software design, and ease in expansion.
• Specialised implementors can be employed and instantiated
dynamically depending on context – no need to modify existing
codebase.
21. Learning resources
The workshop homepage
https://mcollison.github.io/JPMC-java-intro-2021/
The course materials
https://mcollison.github.io/java-programming-foundations/
• Session worksheets – updated each week
22. Additional resources
• Think Java: How to think like a computer scientist
• Allen B Downey (O’Reilly Press)
• Available under Creative Commons license
• https://greenteapress.com/wp/think-java-2e/
• Oracle central Java Documentation –
https://docs.oracle.com/javase/8/docs/api/
• Other sources:
• W3Schools Java - https://www.w3schools.com/java/
• stack overflow - https://stackoverflow.com/
• Coding bat - https://codingbat.com/java