0% found this document useful (0 votes)
5 views10 pages

Java Inheritance

Uploaded by

muhammad shahbaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Java Inheritance

Uploaded by

muhammad shahbaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

JAVA INHERITANCE

Lab-09

Submitted by: Syed Muhammad Ali ( INFT231102007)


Ali Haider ( INFT231102013)
Ather Nazeer ( INFT231102027)
Ateeb Nazeer ( INFT231102028)
Hussain Ali Gill ( INFT231102044)
Daniyal Shahzad ( INFT231102058)
GROUP NO: 06
LAB MANUAL: 09
LAB 09 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.
clas
s 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.

TASK 3: Continuing with TASK 2. Display some statements in the


constructors of both class A and class B and now try to create object of class B
to observe which constructor was called first.
TASK 4: Imagine a publishing company that markets both book and
audiocassette versions of its works. Create a class publication that stores the
title (a string) and price (type float) of a publication. From this class derive
two classes: book, which adds a page count (type int), and tape, which adds a
playing time in minutes (type float). Each of these three classes should have a
getdata() function to get its data from the user at the keyboard, and a putdata()
function to display its data.
Write a main() program to test the book and tape classes by creating instances
of them, asking the user to fill in data with getdata(), and then displaying the
data with putdata().
TASK 5: 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.
QUESTIONS
Please Fill the blank space with respective answers to following questions:
Question 1: What are different types of inheritance?
ANS: Different types of inheritance in Java include:

1. Single Inheritance: A subclass inherits from only one superclass.


2. Multilevel Inheritance: A subclass inherits from another subclass, forming
a chain of inheritance.
3. Hierarchical Inheritance: Multiple subclasses inherit from the same
superclass.
4. Multiple Inheritance (not directly supported in Java): A subclass inherits
from multiple superclasses. In Java, this is achieved through interfaces and
interface implementation.
5. Hybrid Inheritance (not directly supported in Java): Combination of
multiple types of inheritance.

These types of inheritance allow for code reuse and the creation of
hierarchical relationships between classes.

Question 2: Can child class inherit private data of parent class?


ANS: No, a child class cannot directly inherit private data members of the
parent class in Java. Private members of a class are not accessible to its
subclasses, including child classes. They are only accessible within the class in
which they are declared.
However, child classes can indirectly access private members of the parent
class through public or protected methods defined in the parent class. These
methods act as bridges for accessing the private members. By providing
public or protected methods to access private members, the parent class can
enforce encapsulation while still allowing access to its data when necessary.

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.
ANS: In multi-level inheritance, where class A is the parent of class B and
class B is the parent of class C, the sequence of constructor calling is as follows:

1. Constructor of class A is called first when an object of class C is created.


2. Then, constructor of class B is called.
3. Finally, constructor of class C is called.
This sequence ensures that the constructors of all parent classes are called in
the order of their hierarchy before the constructor of the child class is called.
Each constructor initializes its corresponding class members and performs
any necessary initialization tasks.
Question 4: Define the required classes such that the program compiles
without any errors.

class DefineClassHierarchy
{ public static void main(String s[])
{
C c = new C();
M m = new M();
K k;
O o = new C();
H h = new H();
k = o;
m = h;
c = m;
System.out.println("All compilation errors resolved");
}
ANS: To define the required classes so that the program compiles without any errors, we need to
create the classes C, M, K, O, and H. Based on the provided code snippet, here's how we can
define these classes:

java
class C {}

class M {}

class K {}
class O extends C {}

class H extends M {}

With these class definitions, the program should compile without any errors. Each class has been
defined as per the requirements mentioned in the code snippet, and the assignments k = o, m = h,
and c = m are valid since the types of variables k, m, and c match the types of objects being
assigned to them.
THE END

You might also like