0% found this document useful (0 votes)
18 views

Lab 07

The document discusses abstract classes and interfaces in object oriented programming. It provides examples of an abstract class with abstract and non-abstract methods, and a subclass that implements the abstract methods. It also provides an example of an interface across packages and its implementation in another package.

Uploaded by

awaishussainjatt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab 07

The document discusses abstract classes and interfaces in object oriented programming. It provides examples of an abstract class with abstract and non-abstract methods, and a subclass that implements the abstract methods. It also provides an example of an interface across packages and its implementation in another package.

Uploaded by

awaishussainjatt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Department of: Subject of:

Computer Systems Engineering Object Oriented Programming


Mehran University of Engineering Year 1st Semester 2nd
&Technology
Batch 23CS Duration 03
Hours
Jamshoro

Practical 7
To practice programs using abstract classes and interfaces

Objective
 To practice programs using abstract classes and interfaces.

Tools
 Notepad++/IntelliJ IDE/Eclipse IDE.

Keywords: Method Overriding, Abstract Classes, Abstract


Duration:03 hours
Methods, Interfaces.

Write an abstract class that has following members


a. Abstract methods
i. A Method
b. Non abstract methods
i. To display This is a non-abstract method
Write another sub class that extends the above abstract class. Now create the object of
subclass and call abstract and non-abstract methods.

abstract class AbstractClass {


public abstract void abstractMethod();

public void displayMessage() {


System.out.println("This is a non-abstract method");
}
}

class Subclass extends AbstractClass {


@Override
public void abstractMethod() {
System.out.println("This is the implementation of abstract method");
}
}

public class Main {


public static void main(String[] args) {
Subclass obj = new Subclass();
obj.abstractMethod();
obj.displayMessage();
}
}

OUTPUT

Create an interface containing three methods, in its own package. Implement the interface
in a different package. Prove that all the methods in an interface are automatically public.

package interfacepackage;

public interface InterfaceExample {


void method1();
void method2();
void method3();
}

package implementationpackage;

import interfacepackage.*;

public class ImplementInterface implements InterfaceExample {


@Override
public void method1() {
System.out.println("Implementing method1");
}

@Override
public void method2() {
System.out.println("Implementing method2");
}

@Override
public void method3() {
System.out.println("Implementing method3");
}
}
package mainpackage;
import implementationpackage.*;

public class Main {


public static void main(String[] args) {
ImplementInterface obj = new ImplementInterface();
obj.method1();
obj.method2();
obj.method3();
}
}

Rubrics Marks Obtained


0 1
Completeness & Accuracy

Timelines

Student ID: 23CS016

Subject Teacher: Dr. Irfan Ali Bhacho

Date: _________________

You might also like