Programming Lab Lab8
StudentName: M.Sarmad Iqbal RegistrationNo: Cosc222102008
LAB 08 Inheritance in Java
Lab Objectives:
1. Understanding the concept of inheritance
2. Implementation of Inheritance in java
Software Required:
Netbeans IDE
Inheritance in Java
TASK 1: Run and understand the following code
// A simple example of inheritance.
// Create a superclass.
class A
{ int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A
{ int k; void
showk() {
System.out.println("k: " + k);
} void sum()
{
System.out.println("i+j+k: " + (i+j+k));
}
}
class SimpleInheritance { public static void
main(String args []) {
A superOb = new A();
B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10; superOb.j
= 20;
System.out.println("Contents of superOb: "); superOb.showij();
System.out.println();
/* The subclass has access to all public members of
its superclass. */ subOb.i = 7; subOb.j = 8; subOb.k
= 9;
System.out.println("Contents of subOb: ");
subOb.showij(); subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:"); subOb.sum();
}
}
TASK 2: Create a class A which has two data members one is public and other
is private. Create another class B which extends class A. Try to display values of
both data members of class A in class B.(take protacted AS)
public class A {
public String name= "Ali";
protected String id="34521";
private String Adress ="ryk";
}
class B extends A {
public static void main(String[] args) {
B sc =new B();
System.out.println(sc.name);
System.out.println(sc.id);
System.out.println(sc.Adress);
} }
TASK 3: Drive a class called RegularEmployee and HourlyEmploee from the
Employee class. RegularEmployee should add a type double data item called
basicSalary. HourlyEmploee should add a type double data item called
hourlyRate and another double data type item called noOfHours to calculate
salary for hours worked. In both classes add appropriate functions to take their
information from user and display it as well.
import java.util.Scanner;
public class Employe {
}
class Regular_Employee extends Employe{
public double basic_salary;
public void showSalary(){
System.out.println(basic_salary);
}
}
class Hourly_Employee extends Employe{
public double hourly_Rate;
public double noOfHours;
public void showHourlySalary(){
double salary= hourly_Rate*noOfHours;
System.out.println(salary);
}
public static void main(String[] args) {
Regular_Employee ds =new Regular_Employee();
Scanner sc=new Scanner(System.in);
System.out.println("Enter basic salary:");
ds.basic_salary=sc.nextDouble();
ds.showSalary();
Hourly_Employee obj =new Hourly_Employee();
System.out.println("Enter hourly rate and no. of hours");
obj.hourly_Rate=sc.nextDouble();
obj.noOfHours= sc.nextDouble();
obj.showHourlySalary();
}
}
Please Fill the blank space with respective answers to following questions:
Question 1: What are different types of inheritance?
Single-level inheritance.
Multi-level Inheritance.
Hierarchical Inheritance.
Multiple Inheritance.
Hybrid Inheritance.
Question 2: Can child class inherit private data of parent class?
Child class cannot inherit private data of parent class.
Question 3: What is the sequence of constructor calling in multi level
inheritance? Suppose A is parent of B and B is parent of class C.
If we inherit a class from another class and create an object of the derived class, it is clear
that the default constructor of the derived class will be invoked but before that the default
constructor of all of the base classes will be invoke, i.e the order of invocation is that the
base class's default constructor .
THE END