Unit05 - Advanced OOP in Java
Unit05 - Advanced OOP in Java
Instructor:
◊ Abstraction
P Abstract class
P Interfaces
!"#$%&'()*+&&,*",-).$#)
/)0,"",#)',/#%1%& /%-)
+%-,#*"/%-1%& $.)"21*)
Study and understand Study and understand
all the artifacts 3$+#*,4 the self study topics
POLYMORPHISM
You can use the same name for several different things
P the compiler automatically figures out which version you wanted.
There are several forms of polymorphism supported in Java,
shadowing, overriding, and overloading.
<<Abstract>>
Shap
- String Color
Shap(Color)
+ String getColor()
+ void setColor()
+ abstract String draw()
Circle Rectangle
@Override
public String draw() {
return "I'm a " + this.getColor() + " circle.";
}
}
@Override
public String draw() {
return "I'm a " + this.getColor() + " rectangle.";
}
}
v In a subclass,
P you can overload the methods inherited from the superclass.
P such overloaded methods neither hide nor override the superclass
methods
P they are new methods, unique to the subclass.
Note: When I say method signature I am not talking about return type of the method, for
example if two methods have same name, same parameters and have different return type,
then this is not a valid method overloading example. This will throw compilation error.
System.out.println(simpleCalculator.add(10, 20));
System.out.println(simpleCalculator.add(10, 20, 30));
System.out.println(simpleCalculator.add(12.5, 20.5));
}
}
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 16
Method Overloading and Type Promotion
Shape.testClassMethod();
myShape.testInstanceMethod();
}
}
Output:
The class method in Shape.
The instance method in Circle.
class Human {
public static void walk() {
System.out.println("Human walks"); Output:
} Human walks
} Human walks
class Human {
public void walk() {
System.out.println("Human walks"); Output:
Boy walks
} Human walks
}
class B extends A{
B get(a){return this;}
void message(){System.out.println("welcome to covariant return type");
}
class Animal {
}
ABSTRACTION
§ Example:
ü Sending sms, you just type the text and send the message.
You don't know the internal processing about the message
delivery
It shows only important things to the user and
hides the internal details
Rectangle
• It cannot be instantiated.
3
ü An interface is a definition of
method prototypes and possibly
some constants (static final fields).
ü An interface does not include the
implementation of any methods.
v Example:
class Automatic extends GearBox implements Forward, Stop,
Speed
{
public void drive() {
System.out.println("drive()");
}
public void park() {
System.out.println("park()");
}
public void turbo() {
System.out.println("turbo()");
}
public void move() {
System.out.println("move()");
}
}
interface Showable {
void print();
}
1 Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods.
2 Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3 Abstract class can have final, non-final, static and non-static Interface has only static and final variables.
variables.
4 Abstract class can have static methods, main method and Interface can't have static methods, main method or constructor.
constructor.
5 Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
6 The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
7 Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }