--- title: Computer Programming II Pre-Lab 7 --- <h1 style='border: none'><center>Computer Programming II Pre-Lab 7</center></h1> <h2 style='border: none'><center>Abstract Classes & Interfaces</center></h2> <h5><center>The Islamic University of Gaza<br>Engineering Faculty<br>Department of Computer Engineering</center></h5> <h6>Authors: Usama R. Al Zayan<span style="float:right">2023/03/24</span></h6> <h6>Parts of this Lab were adapted from work done by Mohammed Nafiz ALMadhoun and Mohammed AlBanna</h6> --- # Abstract class in Java A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body). ## Points to Remember * An abstract class must be declared with an abstract keyword. * It can have abstract and non-abstract methods. * It cannot be instantiated. * It can have constructors and static methods also. * It can have final methods which will force the subclass not to change the body of the method. ```java= abstract class Car { abstract void run(); } class BMW extends Car { public static void main(String[] args) { Car obj = new BMW(); obj.run(); } @Override void run() { System.out.println("running"); } } ``` Output: :::success running ::: ## Understanding the real scenario of Abstract class In this example, Shape is the abstract class, and its implementation is provided by the Rectangle and Circle classes. ```java= abstract class Shape { abstract void draw(); } class Rectangle extends Shape { @Override void draw() { System.out.println("drawing rectangle"); } } class Circle extends Shape { @Override void draw() { System.out.println("drawing circle"); } } class TestAbstraction1 { public static void main(String[] args) { Shape s = new Circle(); s.draw(); } } ``` Output: :::success drawing circle ::: ## Another example of Abstract class in java ```java= abstract class Bank { abstract int getRateOfInterest(); } class SBI extends Bank { @Override int getRateOfInterest() { return 7; } } class PNB extends Bank { @Override int getRateOfInterest() { return 8; } } class TestBank { public static void main(String[] args) { Bank b; b = new SBI(); System.out.println("Rate of Interest is: " + b.getRateOfInterest() + " %"); b = new PNB(); System.out.println("Rate of Interest is: " + b.getRateOfInterest() + " %"); } } ``` Output: :::success Rate of Interest is: 7 % Rate of Interest is: 8 % ::: ## Abstract class having constructor, data member and methods ```java= abstract class Bike { Bike() { System.out.println("bike is created"); } abstract void run(); void changeGear() { System.out.println("gear changed"); } } class Honda extends Bike { @Override void run() { System.out.println("running safely.."); } } class TestAbstraction2 { public static void main(String[] args) { Bike obj = new Honda(); obj.run(); obj.changeGear(); } } ``` Output: :::success bike is created running safely.. gear changed ::: ## Abstract class in Java Important Points 1. abstract keyword is used to create an abstract class in java. 2. Abstract class in java can’t be instantiated. 3. We can use abstract keyword to create an abstract method, an abstract method doesn’t have body. 4. If a class have abstract methods, then the class should also be abstract using abstract keyword, else it will not compile. 5. It’s not necessary to have abstract class to have abstract method. We can mark a class as abstract even if it doesn’t declare any abstract methods. 6. If abstract class doesn’t have any method implementation, its better to use interface because java doesn’t support multiple class inheritance. 7. The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class. # Interface in Java An **interface** in java is a blueprint of a class. It has static constants and abstract methods. The **interface** in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java. As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface. <center> ![](https://i.imgur.com/C8MHJK2.png) </center> ```java= interface Printable { void print(); } class Print1 implements Printable { public static void main(String[] args) { Print1 obj = new Print1(); obj.print(); } @Override public void print() { System.out.println("Hello"); } } ``` Output: :::success Hello ::: ## Another example of Interface in java ```java= interface Drawable { void draw(); } class Rectangle implements Drawable { @Override public void draw() { System.out.println("drawing rectangle"); } } class Circle implements Drawable { @Override public void draw() { System.out.println("drawing circle"); } } class TestInterface1 { public static void main(String[] args) { Drawable d = new Circle(); d.draw(); } } ``` Output: :::success drawing circle ::: ## Multiple inheritance in Java by interface ```java= interface Printable { void print(); } interface Showable { void show(); } class Multi implements Printable, Showable { public static void main(String[] args) { Multi obj = new Multi(); obj.print(); obj.show(); } @Override public void print() { System.out.println("Hello"); } @Override public void show() { System.out.println("Welcome"); } } ``` Output: :::success Hello Welcome ::: ## Interface inheritance ```java= interface Printable { void print(); } interface Showable extends Printable { void show(); } class TestInterface4 implements Showable { public static void main(String[] args) { TestInterface4 obj = new TestInterface4(); obj.print(); obj.show(); } @Override public void print() { System.out.println("Hello"); } @Override public void show() { System.out.println("Welcome"); } } ``` Output: :::success Hello Welcome ::: ## Static Method in Interface ```java= interface Drawable { static int cube(int x) { return x * x * x; } void draw(); default void print() { System.out.println("Print"); } } class Rectangle implements Drawable { @Override public void draw() { System.out.println("drawing rectangle"); } } class TestInterfaceStatic { public static void main(String[] args) { Drawable d = new Rectangle(); d.draw(); System.out.println(Drawable.cube(3)); } } ``` Output: :::success drawing rectangle 27 ::: # Eamples ## Example 1 ```java= abstract class Shape { protected abstract double getArea(double length); protected abstract double getPerimeter(double length); } class Square extends Shape { public double getArea(double side) { return side * side; } public abstract double getPerimeter(double side); } class TestProgram { public static void main(String[] args) { Square s = new Square(); System.out.print(s.getArea(4)); } } ``` :::danger :negative_squared_cross_mark: **Compilation Error**: Class 'Square' must either be declared abstract or implement abstract method 'getPerimeter(double)' in 'Square' :::info Why? A subclass of an abstract class must be declared abstract if it has one or more abstract methods. This code is wrong, because you can’t instantiate the Square class. ::: ## Example 2 ```java= abstract class Base { int x = 2; public Base() { System.out.println("x=" + x); x++; } abstract int calculate(); abstract int calculate(int i); } class MySuper extends Base { public MySuper() { System.out.println("x=" + calculate(2)); } @Override int calculate() { return x; } @Override int calculate(int i) { return i * x; } } class Sub extends MySuper { public static void main(String[] args) { Sub sub = new Sub(); System.out.println("x=" + sub.calculate()); } @Override int calculate() { return x + 2; } } ``` Output: :::success x=2 x=6 x=5 ::: ## Example 3 ```java= abstract class Base { int z = 6; public Base() { z -= 2; System.out.println("z=" + method(3, 8)); } abstract int method(int i); abstract int method(int i, int i2); } class Sub extends Base { int z = 2; public Sub() { System.out.println("z=" + method(4)); } public static void main(String[] args) { Sub sub = new Sub(); } @Override int method(int i) { return (z * i + super.z); } @Override int method(int i, int i2) { return (method(i) + i + i2); } } ``` Output: :::success z=15 z=12 ::: ## Example 4 ```java= abstract class Base { int x = 3; public Base() { x += 2; System.out.println("x=" + x); x++; } abstract int calculate(); abstract int calculate(int i); } class SuperA extends Base { int x = 1; public SuperA() { System.out.println("x=" + calculate(2)); } @Override int calculate() { return x; } @Override int calculate(int i) { return (calculate() + i); } } class Sub extends SuperA { Sub() { x += 3; } public static void main(String[] args) { Sub sub = new Sub(); System.out.println("x=" + sub.calculate()); } @Override int calculate() { return x + 6; } } ``` Output: :::success x=5 x=9 x=10 ::: ## Example 5 ```java= abstract class Base { int y = 5; public Base() { y -= 2; System.out.println("y=" + method(4, 7)); } abstract int method(int i); abstract int method(int i, int i2); } class Super extends Base { int y = 2; public Super() { System.out.println("y=" + method(4)); } @Override int method(int i) { return (y * i + super.y); } @Override int method(int i, int i2) { return (method(i) + i + i2); } } class Sub extends Super { public static void main(String[] args) { Sub sub = new Sub(); } @Override int method(int i) { return y * 2 + i; } } ``` Output: :::success y=15 y=8 ::: <center>Good luck with your mid-term exams 😄</center> ###### tags: `Computer Programming II` `Pre-Lab` `IUG` `Computer Engineering` <center>End Of Pre-Lab 7</center>