OBJECT ORIENTED PROGRAMMING USING JAVA Lab 6 PROJECTS

OBJECT ORIENTED PROGRAMMING USING JAVA Lab 6

PROJECT’S IDEA SUBMISSION Kindly Note that the Project registration form will be opened this Wednesday , 16/12/2020, at 10: 00 pm, and it will be closed on Friday 18/12/2020 , at 11: 59 pm. 2

CONTENT ABSTRACT METHODS Polymorphism Dynamic binding Type Casting Instance. Of keyword 3

ABSTRACT METHODS Is a method where only the signature is provided with no implementation. Abstract Methods must be implemented (Overridden) in the subclasses. A class with an abstract method must be an abstract class. Abstract classes may have both concrete (non-abstract) and abstract methods. 4

EXAMPLE OF ABSTRACT CLASS THAT HAS AN ABSTRACT METHOD abstract class Bike{ abstract void run(); } class Honda 4 extends Bike { void run(){ System. out. println(“Running safely"); } } public static void main(String args[]){ Bike obj = new Honda 4(); obj. run(); } Output Running safely 5

EFFECT OF ABSTRACT METHODS If a class has an abstract method, then that class must be defined as an abstract class Bike 12 { abstract void run(); } Compile time error 6

SOME NOTES Abstract classes may define a constructor, but trying to call the constructor will generate a compiler error. You don't have to implement all methods of an abstract class. But you must implement all abstract methods of it 7

QUESTIONS 1 Static methods cannot be declared abstract - Why? An abstract method is defined only so that it must be overridden in a subclass. However, static methods can not be overridden because static methods belongs to a particular class and not to its instance. Other words, a static method in an abstract class would belong to that class, and not the overriding class, so couldn't be used anyway. 8

QUESTIONS 2 Constructors cannot be declared abstract - Why? First, the constructor (according to the Java Language Specification) can't be overridden. Also, when you set a method as abstract it means: "The method doesn't have a body and it should be implemented in a child class. " But the constructor is called implicitly when the new keyword is used so it can't lack a body. Even if we didn’t use new keyword, as we mentioned before, the constructor of the parent class must be called from the child’s constructor before executing any code line, so it can’t be empty. 9

POLYMORPHISM Polymorphism comes from Greek meaning “many forms”. There are two types of polymorphism: � Runtime polymorphism. � Compile-time polymorphism. 10

COMPILE TIME AND RUNTIME POLYMORPHISM Compile-time polymorphism Run-time polymorphism It is implemented through method overloading It is implemented through method overriding Is executed at the compile time Is executed at the run time The compiler knows which method to execute depending on the parameter list. The compiler doesn’t know which implementation will be used before runtime (The implementation in the parent class or the implementation in the child class). Static binding Dynamic binding 11

Compile-time polymorphism Run-time polymorphism COMPILE TIME AND RUNTIME POLYMORPHISM Class Parent Class Test { int add (int a, int b){ System. out. println(a+b) } { void math_operation (int a, int b){ System. out. println(a+b) } } Float add (float a, float b) { System. out. println(a+b) } } Class Child extends Parent { void math_operation (int a, int b){ System. out. println(a*b) } } Public static void main() { Test T= new Test(); T. add(3, 4); //7 T. add(3. 4, 5. 2); //8. 6 } Public static void main() { Parent P 1= new Parent(); Parent P 2= new Child(); P 1. math_operation(3, 4); //7 P 2. math_operation(3, 4); //12 } 12

Compile-time polymorphism Run-time polymorphism COMPILE TIME AND RUNTIME POLYMORPHISM Class Parent Class Test { int add (int a, int b){ System. out. println(a+b) } { void math_operation (int a, int b){ System. out. println(a+b) } } Float add (float a, float b) { System. out. println(a+b) } } Class Child extends Parent { } } Public static void main() { Test T= new Test(); T. add(3, 4); //7 T. add(3. 4, 5. 2); //8. 6 } Public static void main() { Parent P 1= new Parent(); Parent P 2= new Child(); P 1. math_operation(3, 4); //7 P 2. math_operation(3, 4); //7 } 13

DYNAMIC BINDING Achieved at runtime � When the class of an object cannot be determined at compile time � Means the JVM (not the compiler) must bind a method call to its implementation Instances of a sub-class can be treated as if they were an instance of the parent class � Therefore the compiler doesn’t know its type, just its base type. 14

DYNAMIC BINDING EXAMPLE Abstract class Vehicle { public void start() { System. out. println("Vehicle"); } } class Bike extends Vehicle { public void start() { System. out. println(“Bike"); } } class Car extends Vehicle { public void start() { System. out. println("Car"); } } public static void main(String[] args) { Vehicle vehicle 1 = new Car(); vehicle 1. start(); Vehicle vehicle 2 = new Bike(); vehicle 2. start(); Vehicle vehicle 3 = new Vehicle(); vehicle 3. start(); Car Bike Compile time error 15

TYPE CASTING Upcasting is casting to a supertype, while downcasting is casting to a subtype. Upcasting is always allowed, but downcasting involves a type check. 16

TYPE CASTING: UPCASTING class parent { void display() { System. out. println("Parent"); } } class child extends parent { @Override void display() { System. out. println("Child"); } void display_child() public class Main { { System. out. println("Display child"); public static void main(String[] args) { parent up. Casting=new child(); up. Casting. display(); Child up. Casting. display_child(); } } 17

TYPE CASTING: DOWNCASTING class parent class child extends parent { { void display() @Override { void display() System. out. println("Parent"); { } System. out. println("Child"); } } void display_child() public class Main { { System. out. println("Display child"); public static void main(String[] args) { } parent upcasting=new child(); child down. Casting=(child)upcasting; } down. Casting. display(); down. Casting. display_child(); upcasting. display_child(); } } Output: Child Display_child Child 18

TYPE CASTING: DOWNCASTING Abstract class Vehicle { public void start() { System. out. println("Vehicle"); } } class Bike extends Vehicle { public void start() { System. out. println(“Bike"); } } class Car extends Vehicle { public void start() { System. out. println("Car"); } } public static void main(String[] args) { Vehicle vehicle 1 = new Car(); Vehicle vehicle 2 = new Bike(); Car C 1 = (Car) vehicle 1; Car C 2 = (Car) vehicle 2; Runtime error } 19

INSTANCEOF KEYWORD It determines whether a reference variable that points to an object is a particular class type. 20

INSTANCEOF KEYWORD Abstract class Vehicle { public void start() { System. out. println("Vehicle"); } } class Bike extends Vehicle { public void start() { System. out. println(“Bike"); } } class Car extends Vehicle { public void start() { System. out. println("Car"); } } public static void main(String[] args) { Vehicle vehicle 1 = new Car(); Car C 1 = new Car(); Vehicle vehicle 2 = new Bike(); vehicle 1 instanceof Car vehicle 2 instanceof Car vehicle 1 instanceof Vehicle vehicle 2 instanceof Vehicle C 1 instanceof Vehicle Output True False True 21

TYPE CASTING: DOWNCASTING Abstract class Vehicle { public void start() { System. out. println("Vehicle"); } } class Bike extends Vehicle { public void start() { System. out. println(“Bike"); } } How would you fix this class Car extends Vehicle code ? { public void start() { System. out. println("Car"); } } public static void main(String[] args) { Vehicle vehicle 1 = new Car(); Vehicle vehicle 2 = new Bike(); Car C 1 = (Car) vehicle 1; Car C 2 = (Car) vehicle 2; Runtime error } 22

INSTANCEOF KEYWORD Abstract class Vehicle { public void start() { System. out. println("Vehicle"); } } class Bike extends Vehicle { public void start() { System. out. println(“Bike"); } } class Car extends Vehicle { public void start() { System. out. println("Car"); } } public static void main(String[] args) { Vehicle vehicle 1 = new Car(); Vehicle vehicle 2 = new Bike(); Car C 1 = (Car) vehicle 1; Car C 2 = (Car) vehicle 2; } Car C 1; If ( vehicle 1 instanceof Car ) { C 1 = (Car)vehicle 1 } And so on… 23

QUESTIONS 24
- Slides: 24