Lab Oopsjava (1) Maaz
Lab Oopsjava (1) Maaz
class Main {
import java.util.Scanner;
interface Walkable {
void walk();
}
interface Running {
void run();
}
// Implement the interfaces in a class
class Dog implements Walkable, Running {
@Override
public void walk() {
System.out.println("Dog is walking.");
}
@Override
public void run() {
System.out.println("Dog is running.");
}
}
// Use the class to call the methods from the interfaces
public class Main {
dog.walk();
dog.run();
}
}
Program No. 5: To Implement Abstract Class
// Main class
public class Base {
public static void main(String[] args) {
// Creating an instance of Employee with a reference of type Sunstar
Sunstar s = new Employee();
// Calling the printInfo method, which is implemented in Employee class
s.printInfo();
}
}
Program no 6: Program to Implement Inheritance concept in Java.
import java.io.*;
import java.lang.*;
import java.util.*;
// Parent class
class One {
public void print_geek()
{
System.out.println("Geeks");
}
}
// Driver class
public class Main {
// Main function
public static void main(String[] args)
{
Two g = new Two();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Program no 7:Program to implement multilevel inheritence
import java.io.*;
import java.lang.*;
import java.util.*;
class One {
public void print_geek()
{
System.out.println("Hello");
}
}
// Drived class
public class Main {
public static void main(String[] args)
{
Three g = new Three();
g.print_geek();
g.print_for();
g.print_greek();
}
}
Program no 8: Demonstrate the use of polymorphism using method overridding
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}
public class Main { // Ensure this class is public and named Main
public static void main(String[] args) {
Shape s1 = new Rectangle();
Shape s2 = new Triangle();
import java.io.*;
class GFG1 {
public static void main (String[] args) {
int a=5;
int b=0;
try{
System.out.println(a/b);
}
catch(ArithmeticException e){
System.out.println(e.getMessage());
}
}
}
Program No. 10: Program for Multi-Threading
}
}
new A().start();
new B().start();
new C().start();