0% found this document useful (0 votes)
12 views8 pages

Oop Lab 9 Practice Tasks

Uploaded by

Tube 360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Oop Lab 9 Practice Tasks

Uploaded by

Tube 360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Riphah International University I-14

Main Campus
Faculty of Computing

Class Fall-2022 Subject OOP


Course Code CS2104 Class/Lab Instructor Saad Wazir

-------------------- LAB 09 --------------------


Learning Objective:
Abstraction Interfaces

• Abstract methods • Uses of Interfaces


• Abstract classes • Multiple inheritance
• Pure Abstract classes • Constant interfaces
• Loose coupling

Practice Task # 1 - Abstract methods & Abstract classes


The abstract keyword is a non-access modifier, used for classes and methods:

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must
be inherited from another class).

Abstract method: can only be used in an abstract class, and it does not have a body. The body
is provided by the subclass (inherited from).

An abstract class can have both abstract and regular methods:

abstract class Shape{


int side;
public void print() {
System.out.println("printing shape");
}
abstract void draw();

class Rectangle extends Shape{


void draw(){
System.out.println("drawing rectangle");
}
}
class Circle extends Shape{

void print_circle(){
System.out.println("printing circle");
}

void draw(){
System.out.println("drawing circle");
}
}

class abstract_class_main{
public static void main(String args[]){

Shape s_1 = new Circle();


s_1.draw();
s_1.side = 30;
System.out.println(s_1.side);
//s_1.print_circle();

Shape s_2 = new Rectangle();


s_2.draw();
s_2.side = 20;
System.out.println(s_2.side);

Circle s_3 = new Circle();


s_3.draw();
s_3.side = 10;
System.out.println(s_3.side);
s_3.print_circle();
}
}

Practice Task # 2 – interface in java


• Like abstract classes, interfaces cannot be used to create objects
• Interface methods do not have a body - the body is provided by the "implement" class
• On implementation of an interface, you must override all of its methods
• Interface methods are by default abstract and public
• Interface attributes are by default public, static and final
• An interface cannot contain a constructor (as it cannot be used to create objects)
interface Animal {

public abstract void animalSound();


void sleep();

class Cat implements Animal {


public void animalSound() {
System.out.println("The cat says: meow moew");
}
public void sleep() {
System.out.println("zzz");
}
public void catRun() {
System.out.println("The cat is running");
}
}

class main {
public static void main(String[] args) {
Cat myCat = new Cat();
myCat.animalSound();
myCat.sleep();
myCat.catRun();

Animal urCat = new Cat();


urCat.animalSound();
urCat.sleep();
//urCat.catRun();
}
}

Practice Task # 3 – Constant Interface

interface OlympicMedal {
String GOLD = "Gold";
String SILVER = "Silver";
String BRONZE = "Bronze";
}
public class interface_constants implements OlympicMedal {
public static void main(String args[]){
String medal = GOLD;
System.out.println(medal);
}
}

Practice Task # 4 - Multiple Interfaces


Java does not support "multiple inheritance" (a class can only inherit from one superclass).
However, it can be achieved with interfaces, because the class can implement multiple
interfaces. Note: To implement multiple interfaces, separate them with a comma.
interface FirstInterface {
public void myMethod(); // interface method
}

interface SecondInterface {
public void myOtherMethod(); // interface method
}

class DemoClass implements FirstInterface, SecondInterface {


public void myMethod() {
System.out.println("Some text..");
}
public void myOtherMethod() {
System.out.println("Some other text...");
}
}

class multiple_inheritance {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}

Practice Task # 5 - Loose coupling

interface Keyboard
{
String display();
}

class DellKeyboard implements Keyboard


{

String info;

public String display()


{
this.info = "the Dell keyboard.";
return this.info;
}
}

class LenovoKeyboard implements Keyboard


{

String info;

public String display()


{
this.info = "the Lenovo keyboard.";
return this.info;
}
}

class Computer
{
// the method only handles keyboard made by the company Dell
public void keyboardUsed(DellKeyboard dk)
{
System.out.println("The computer is using " + dk.display());
}

// the method only handles keyboard made by the company Lenovo


public void keyboardUsed(LenovoKeyboard dk)
{
System.out.println("The computer is using " + dk.display());
}

public class CouplingExample5


{

// main method
public static void main(String argvs[])
{
// creating an object of the class Computer
Computer obj = new Computer();

// creating an object of the class LenovoKeyboard


LenovoKeyboard lk = new LenovoKeyboard();

obj.keyboardUsed(lk);

}
}
Now, the computer works for the Lenovo keyboard. However, the keyboardUsed() method is
tightly coupled with Lenovo as well as the Dell keyboard. Thus, any other variety of keyboard
is not considered by the keyboardUsed() method. Hence, we ended up with the same
problem. Also, it is not good to add a method for a specific keyboard in the class Computer.

Think what will happen if we have 50 varieties of keyboards? Handling 50 methods is


certainly a tedious task. Also, one has to write a lot of code to add 50 methods. Also, for
testing purposes, we have to test all of the added 50 methods, that will be time-consuming.

To avoid such problems, we have to make the method keyboardUsed() loosely coupled with
the keyboard. To achieve the same, our keyboardUsed() method should depend upon the
interface (remember! Abstraction is the key).
interface Keyboard
{
String display();
}

class DellKeyboard implements Keyboard


{

String info;
public String display()
{
this.info = "the Dell keyboard.";
return this.info;
}
}

class LenovoKeyboard implements Keyboard


{

String info;
public String display()
{
this.info = "the Lenovo keyboard.";
return this.info;
}
}

class Computer
{
// the method is now dependent on the interface Keyboard
public void keyboardUsed(Keyboard k)
{
System.out.println("The computer is using " + k.display());
}

public class CouplingExample6


{

// main method
public static void main(String argvs[])
{
// creating an object of the class Computer
Computer obj = new Computer();

// creating an object of the class LenovoKeyboard


LenovoKeyboard lk = new LenovoKeyboard();

obj.keyboardUsed(lk);

// creating an object of the class DellKeyboard


DellKeyboard dk = new DellKeyboard();

obj.keyboardUsed(dk);

}
}
We see that one method is handling keyboards manufactured by the company Dell or
Lenovo. It is because the only one method keyboardUsed() of the class Computer is not
tightly attached with the class DellKeyboard or LenovoKeyboard. The keyboardUsed()
method is dependent on the abstraction (interface Keyboard). Therefore, the method is able
to handle any kind of keyboard.

Thus, even if we add 50 more types of keyboard, the keyboardUsed() method is able to
handle it. Hence, we have to write a lesser amount of code as compared to the previous
code.

Testing the above code is also easier as we have only one method in the class Computer.
Previously, we have to test all the methods of class Computer for every type of keyboard.

Also, if in the future the Dell company stops the manufacturing of the keyboard, then we can
remove the DellKeboard class. Nothing has to be changed in the class Computer. However, in
the previous example, we have to remove the method keyboardUsed(DellKeyboard dk) from
the class Computer.

We can observe that if we add something or remove something, we have to make more
changes in the previous code and lesser changes to the above code. Thus, it is evident that
why one should strive for loosely coupled structures.

You might also like