You are on page 1of 15

Question 1:-

Create a class and determine if method overloading holds good for return type of methods or
not.

Code:-

class Main{

public int division(int a, int b){

int result = a/b;

return result;

public double division (int a, int b){

double result = a/b;

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.*;

public class Employee {


Scanner sc=new Scanner(System.in);
String name;
int id;
Employee(){
System.out.println("enter name");
this.name=sc.next();
System.out.println("enter id");
this.id=sc.nextInt();
}
public String toString()
{
return ("the name is " + name + ". Id is " + id);
}
public static void main(String[] args) {
Employee e=new Employee();
System.out.println(e);
Scientist s=new Scientist();
System.out.println(s);
DScientist d=new DScientist();
System.out.println(d);
}
}
class Scientist extends Employee{
Scanner sc=new Scanner(System.in);
int no_of_publication;
float experience;
Scientist(){
super();
System.out.println("enter no of publications");
this.no_of_publication=sc.nextInt();
System.out.println("enter experience");
this.experience=sc.nextFloat();
}
public String toString()
{

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;

// ref refers to an A object


ref = a;

// calling A's version of m1()


ref.m1();

// now ref refers to a B object


ref = b;

// calling B's version of m1()


ref.m1();

// now ref refers to a C object


ref = c;

// calling C's version of m1()


ref.m1();
}
}

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
{

abstract void RectangleArea(float length , float breadth);


abstract void SquareArea(float radius);
abstract void CircleArea(float side);
}
class Area extends Shape {
double Area = 0;
void RectangleArea(float length, float breadth) {
Area = length * breadth;
System.out.println("Area of rectangle is: " + Area);
}

void SquareArea(float Side) {


Area = Side * Side;
System.out.println("Area of Square is: " + Area);
}

void CircleArea(float radius) {


Area = (radius * radius) * 3.14;
System.out.println("Area of Circle is: " + Area);
}
}
public class objArea
{
public static void main(String[] args) {
Area a = new Area();
a.RectangleArea(5.5f, 7f);
a.SquareArea(5f);
a.CircleArea(4);
}
}
Output:-
Question 8:-
Create a class Parent having instance variables id, name and address. Create a class ChildOne
having instance variables id, name, address and marks. Also create another class ChildTwo with
instance variables id, name, address, qualification and salary. Within each class define your own
method to display values of these variables. Design the program using super call with proper
parameter and use object of each class from main() to display their properties.
Code:-
import java.util.*;
public class Parent {
Scanner sc=new Scanner(System.in);
int id;
String name;
String address;
Parent(){
System.out.println("enter name");
this.name=sc.next();
System.out.println("enter address");
this.address=sc.next();
System.out.println("enter id");
this.id=sc.nextInt();}
void display() {
System.out.println("name is "+name);
System.out.println("the address is "+address);
System.out.println("the id is "+id);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("---parent---");
Parent p=new Parent();
p.display();
System.out.println("---ChildOne---");
ChildOne c1=new ChildOne(80);
c1.display();
System.out.println("---ChildTwo---");
ChildTwo c2=new ChildTwo();
c2.display();
}
}
class ChildOne extends Parent{
Scanner sc=new Scanner(System.in);
float marks;
ChildOne(float m){
super();
System.out.println("enter marks");
this.marks=m;
}
void display() {
super.display();
System.out.println("the marks is "+marks);
}
}
class ChildTwo extends Parent{
Scanner sc=new Scanner(System.in);
String qualification;
double salary;
ChildTwo(){
super();
System.out.println("enter qualification");
this.qualification=sc.next();
System.out.println("enter salary");
this.salary=sc.nextDouble();
}
void display() {
super.display();
System.out.println("qualification is "+qualification);
System.out.println("salary is "+salary);
}
}
Output:-

You might also like