Abstract Classes:
Q1) Create an abstract class Compartment to represent a rail coach. Provide an
abstract function notice in this class.
public abstract String notice();
Derive FirstClass, Ladies, General, Luggage classes from the compartment class.
Override the notice function in each of them to print notice message that is
suitable to the specific type of compartment.
Create a class TestCompartment.Write main function to do the following:
Declare an array of Compartment of size 10.
Create a compartment of a type as decided by a randomly generated integer in the
range 1 to 4.
Check the polymorphic behavior of the notice method.
[i.e based on the random number genererated, the first compartment can be
Luggage, the second one could be Ladies and so on..]
Code:
abstract class Compartment {
public abstract String notice();
} class FirstClass extends Compartment {
@Override
public String notice() {
return "This is First Class Compartment.";
}class General extends Compartment {
@Override
public String notice() {
return "This is general department.";
} class Ladies extends Compartment {
@Override
public String notice() {
return "This is ladies Compartment."; }
} class Luggage extends Compartment {
@Override
public String notice() {
return "This is Luggage Compartment.";
public class TestCompartment {
public static void main(String[] args) {
Compartment comp[] = new Compartment[10];
double randomDouble = Math.random();
randomDouble = randomDouble * 4 + 1;
int randomInt = (int) randomDouble;
if(randomInt == 1) {
comp[randomInt] = new FirstClass();
else if(randomInt == 2) {
comp[randomInt] = new General();
else if(randomInt == 3) {
comp[randomInt] = new Ladies();
else {
comp[randomInt] = new Luggage();
System.out.print("Notice is :-" + comp[randomInt].notice());
Output: Notice is :-This is Luggage Compartment.
PACKAGES:
Q1) Create a package called test package.
Define a class called foundation inside the test package.
Inside the class, you need to define 4 integer variables:
var1 with private access modifier
var2 with default access modifier
var3 with protected access modifier
var4 with public access modifier
Import this class and packages in another class.
Try to access all 4 variables of the foundation class and see what variables are
accessible and what are not accessible.
Code: Solution.java
package test;
class Foundation {
private int var1;
int var2;
protected int var3;
public int var4;
public class Solution {
public static void main(String[] args) {
Foundation foundation = new Foundation();
//foundation.var1 = 1; //error - not accessible
(private variable)
foundation.var2 = 2;
foundation.var3 = 3;
foundation.var4 = 4;
//System.out.println(foundation.var1);
System.out.println(foundation.var2);
System.out.println(foundation.var3);
System.out.println(foundation.var4); }
}
Output:
Q2) Create a class called compartment which represents the ship compartments with
attributes like height, width and breadth.
Take care it should not conflict with the compartment class you have created in
Abstract class exercise 2.
To avoid conflict create this class in a new package called
com.wipro.automobile.ship
Code:
package com.wipro.automobile.ship;
class Compartment {
private double height;
private double width;
private double breadth;
public Compartment(double height, double width, double breadth) {
this.height = height;
this.width = width;
this.breadth = breadth;
@Override
public String toString() {
return "Compartment [height=" + height + ", width=" + width + ", breadth=" +
breadth + "]";
public class Solution1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Compartment compartment = new Compartment(1005.15, 400.5, 588.2);
System.out.println(compartment);
Output: Compartment [height=1005.15, width=400.5, breadth=588.2]
Q3) Create a package called com.automobile. Define an abstract class called
Vehicle.
Vehicle class has the following abstract methods:
public String getModelName()
public String getRegistrationNumber()
public String getOwnerName()
Create twowheeler subpackage under automobile package
Hero class extends automobile.vehicle class with the following methods
public int getSpeed()
– returns the current speed of the vehicle.
public void radio()
– provides facility to control the radio device
Honda class extends com.automobile.vehicle class with the following methods
public int getSpeed()
– Returns the current speed of the vehicle.
public void cdplayer()
– provides facility to control the cd player device which is available in the car.
Create a test class to test the methods available in all these child class.