Professional Documents
Culture Documents
Oops Lab6
Oops Lab6
Create a class and determine if method overloading holds good for return type of methods or
not.
Code:-
class Main{
return result;
return result;
Output:-
Question 2:-
Overload the constructors for classes Area and Volume of a rectangular figure and also display
its area and volume. Area is the superclass and Volume is the subclass.
Code:-
class Box
{
double width, height, depth;
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Box()
{
width = height = depth = 0;
}
Box(double len)
{
width = height = depth = len;
}
double volume()
{
return width * height * depth;
}
}
public class Test
{
public static void main(String args[])
{
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);
vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
}
}
Output:-
Question 3:-
Create a class Employee is having instance variables name and id. Create its subclass named
Scientist which has 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 about the class and
from main() method create object of each class and print each object.
Code:-
import java.util.*;
return "the name is " + name + ". Id is " + id + ". Number of publication(s) " + no_of_publication + ".
Experience: " + experience ;
}
}
class DScientist extends Scientist{
Scanner sc=new Scanner(System.in);
String award;
DScientist(){
super();
System.out.println("enter award received");
this.award=sc.next();
}
public String toString()
{
return "the name is " + name + ". Id is " + id + ". No of publication(s) " + no_of_publication + ".
Experience: " + experience +". Awards received: " + award ;
}
}
Output:-
Question 4:-
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 overriding1 {
void show()
{
System.out.println("Parent's show()");
}
public static void main(String[] args)
{}
}
// Inherited class
class Child1 extends overriding1 {
void show()
{
System.out.println("Child's show(1)");
}
}
// Inherited class
class Child2 extends overriding1 {
void show()
{
System.out.println("Child's show(2)");
}
}
// Inherited class
class Child3 extends overriding1 {
void show()
{
System.out.println("Child's show(3)");
}
}
// Driver class
class Main {
public static void main(String[] args)
{
overriding1 obj = new overriding1();
obj.show();
Child1 obj1 = new Child1();
obj1.show();
Child2 obj2 = new Child2();
obj2.show();
Child3 obj3 = new Child3();
obj3.show();
}
}
Output:-
Question 5:-
Do the problem 4 using dynamic method dispatching.
Code:-
class A
{
void m1()
{
System.out.println("Inside A's m1 method");
}
}
class B extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside B's m1 method");
}
}
class C extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside C's m1 method");
}
}
// Driver class
class Dispatch
{
public static void main(String args[])
{
// object of type A
A a = new A();
// object of type B
B b = new B();
// object of type C
C c = new C();
// obtain a reference of type A
A ref;
Output:-
Question 6:-
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 main method.
Code:-
abstract class abstractc {
public void display() {
System.out.println("This is a method of abstract class");
}
}
public class AbstractClassExample extends abstractc{
public static void main(String args[]) {
new AbstractClassExample().display();
}
}
Output:-
Question 7:-
Create an abstract class with three abstract methods check whether you can we override only
few methods (not all methods) in subclass or not.
Code:-
abstract class Shape
{