Swapnanil Dutta 12 OOP Lab
Swapnanil Dutta 12 OOP Lab
Question: Create a class Employee is having instance variables name and id. Create its
subclass named Scientist which has an instance variables no_of_publication and experience.
Now create its subclass, say DScientist which has instance variable award. Put a method like:
public String toString(){ }
in every class where you describe the class and from the main() method create an object of
each class and print each object.
Code:
class Employee {
int id;
String name;
Employee(int id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return "Id = " + this.id + "\nName = " + this.name + "\n";
}
}
System.out.println("----------------------Details-------------------------
-");
System.out.println(em+"\n");
System.out.println(st+"\n");
System.out.println(ds+"\n");
}
}
Output:
Question: Create a class with a method void show()and make three subclasses of it and
all subclasses have this show() method overridden and call those methods using their
corresponding object references.
Code:
class Grandparent {
void show() {
System.out.println("Class GrandParent");
}
}
Code:
class Grandparent {
void show() {
System.out.println("Class GrandParent");
}
}
Output:
Question: Check without having any abstract method/s whether a class can be abstract; if so,
then use that concrete method(s) from another class having the main method.
Code:
abstract class Base {
public static void show() {
System.out.println("Base abstract class");
}
}
Output:
Question: Create an abstract class with three abstract methods check whether you can we
override only a few methods (not all methods) in subclass or not.
Code:
abstract class Base {
abstract String method();
}
Output: