Practice questions on subclass

Level 1

Create a class with a method that prints "This is parent class" and its subclass with another method that prints "This is child class". Now, create an object for each of the class and call
1 - method of parent class by object of parent class
2 - method of child class by object of child class
3 - method of parent class by object of child class
class Pclass{
  public void pmethod(){
    System.out.println("This is parent class");

class Cclass extends Pclass{
  public void cmethod(){
    System.out.println("This is child class");

class Ans{
  public static void main(String[] args){
    Pclass m = new Pclass();
    Cclass n = new Cclass();

In the above example, declare the method of the parent class as private and then repeat the first two operations (You will get error in the third).

Create a class named 'Member' having the following members:
Data members
1 - Name
2 - Age
3 - Phone number
4 - Address
5 - Salary
It also has a method named 'printSalary' which prints the salary of the members.
Two classes 'Employee' and 'Manager' inherits the 'Member' class. The 'Employee' and 'Manager' classes have data members 'specialization' and 'department' respectively. Now, assign name, age, phone number, address and salary to an employee and a manager by making an object of both of these classes and print the same.
class Member{
  String name;
  int age;
  String number;
  String address;
  int salary;

  public void printSalary(){


class Employee extends Member{
  String specialization;

class Manager extends Member{
  String department;

class Ans{
  public static void main(String[] args){
    Employee e = new Employee(); = "xyz";
    e.age = 23;
    e.number = "986****";
    e.address = "xyzxyz";
    e.salary = 1231;
    e.specialization = "xyzxyz";

    Manager m = new Manager();
    //Same goes for Manager

Create a class named 'Rectangle' with two data members 'length' and 'breadth' and two methods to print the area and perimeter of the rectangle respectively. Its constructor having parameters for length and breadth is used to initialize length and breadth of the rectangle. Let class 'Square' inherit the 'Rectangle' class with its constructor having a parameter for its side (suppose s) calling the constructor of its parent class as 'super(s,s)'. Print the area and perimeter of a rectangle and a square.

Now repeat the above example to print the area of 10 squares.
Hint-Use array of objects
class Rectangle{
  int length;
  int breadth;

  public Rectangle(int l, int b){
    length = l;
    breadth = b;

  public void printArea(){

  public void printPerimeter(){

class Square extends Rectangle{
  int side;
  public Square(int s){

class Ans{
  public static void main(String[] args){
    Rectangle r = new Rectangle(4,5);

    Square s = new Square(4);

    Square[] a = new Square[10];
    int k = 5;
    for(int i = 0;i<10;i++){
      a[i] = new Square(k);

    for(int i = 0;i<10;i++){

Create a class named 'Shape' with a method to print "This is This is shape". Then create two other classes named 'Rectangle', 'Circle' inheriting the Shape class, both having a method to print "This is rectangular shape" and "This is circular shape" respectively. Create a subclass 'Square' of 'Rectangle' having a method to print "Square is a rectangle". Now call the method of 'Shape' and 'Rectangle' class by the object of 'Square' class.

Level 2

Level 3

Ask Yours
Post Yours