[go: up one dir, main page]

0% found this document useful (0 votes)
95 views6 pages

Lab Session 4: Java Inheritance Objective

The lab session covers Java inheritance concepts like superclasses, subclasses, overriding, overloading, and using the this and super keywords. Students will create class hierarchies to represent shapes like rectangles and squares, with subclasses inheriting and extending the functionality of parent classes. They will also explore access modifiers, constructors, and preventing inheritance. The exercises provide hands-on practice with these inheritance concepts through creating classes, objects, and calling methods of parent and child classes.
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)
95 views6 pages

Lab Session 4: Java Inheritance Objective

The lab session covers Java inheritance concepts like superclasses, subclasses, overriding, overloading, and using the this and super keywords. Students will create class hierarchies to represent shapes like rectangles and squares, with subclasses inheriting and extending the functionality of parent classes. They will also explore access modifiers, constructors, and preventing inheritance. The exercises provide hands-on practice with these inheritance concepts through creating classes, objects, and calling methods of parent and child classes.
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/ 6

Lab session 4: Java Inheritance

Objective

The objective of lab session 4 is


 To understand the use of inheritance
 To differentiate super classes and sub-classes
 To differentiate shadowing, overloading and overriding
 To use static and final modifier with inheritance implementation
 To understand the use of this and super keywords
 To implement super class constructor inside sub-classes

Pre-lab Exercise

1. ___________ is a form of software reusability in which new classes acquire the members of
existing classes and enhance those classes with new capabilities.
A. Encapsulation C. Polymorphism
B. Inheritance D. Abstraction
2. Which of these is correct way of inheriting class A by class B?
A. class B + class A {} C. class B inherits class A {}
B. class B extends A {} D. class B extends class A {}
3. Which of these keywords is used to refer to member of base class from a sub class?
A. upper C. super
B. this D. None of the mentioned
4. When an object of a subclass is instantiated, a superclass ___________ is called implicitly
or explicitly.
A. Static variable C. Non-static method
B. Constructor D. main method
5. Which of the following statement is false about inheritance?
A. Superclass constructors are not inherited by subclasses
B. A has-a relationship is implemented via inheritance
C. When a subclass redefines a superclass method by using the same signature, the
subclass is said to overload that superclass method
D. We can override a private method defined in a superclass
E. We can override a static method defined in a superclass.

1|Page
6. Multiple inheritance means,
A. One class inheriting from more super classes
B. More classes inheriting from one super class
C. More classes inheriting from more super classes
D. None of the above
E. (A) and (B) above.
7. To prevent any method from overriding, we declare the method as,
A. static C. const
B. final D. abstract
8. Consider the following code fragment
Rectangle r1 = new Rectangle();
r1.setColor(Color.blue);
Rectangle r2 = r1;
r2.setColor(Color.red);
After the above piece of code is executed, what are the colors of r1 and
r2 (in this order)?
A. Color.blue & Color.red C. Color.blue & Color.blue
B. Color.red & Color.red D. Color.red & Color.blue
9. A protected member can be accessed in, which option is false?
A. A subclass of the same package
B. A non-subclass of the same package
C. A non-subclass of different package
D. A subclass of different package
E. The same class.
10. When an overridden method is called from within a subclass, it will always refer to the
version of that method defined by the
A. Super class C. Compiler will choose randomly
B. Sub class D. Interpreter will choose randomly
11. What best describes the purpose of a class’s constructor?
A. Initialize the fields in the object.
B. Determines the amount of space needed for an object and creates the object.
C. Names the new object.
D. None of the listed

2|Page
12. What will be the output of the following Java code?

class A {
int i;
}
class B extends A {
int j;
void display() {
super.i = j + 1;
System.out.println(j + " " + i);
}
}
class inheritance {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}

13. What will be the output of the following Java program?

public class Test {


public static void main(String[] args) {
A a = new A(3);
}
}
class A extends B {
public A(int t) {
System.out.println("A's constructor is invoked");
}
}

class B {

3|Page
public B() {
System.out.println("B's constructor is invoked");
}
}

14. Identify an error?

class X
{
public X(int i)
{
System.out.println(1);
}
}

class Y extends X
{
public Y()
{
System.out.println(2);
}
}

class Z extends X, Y
{
//Class Z Members
}

15. Below code is showing compile time error. Can you suggest the corrections?

class X
{
//Class X Members
}

class Y
{
//Class Y Members
}

class Z extends X, Y
{
//Class Z Members
}

4|Page
In-lab Exercise

16. Create a class Parent with a method ‘display1’ that prints "This is parent class" and its
subclass Child with another method ‘display2’ that prints "This is child class". Now, create
an object for each of the class and call
 method of parent class by object of parent class
 method of child class by object of child class
 method of parent class by object of child class
17. In the above exercise [16], declare the method of the parent class as private and then
repeat the first two operations. What is the output?
18. Create a class named 'Member' having the following members: Data members
 Name
 Age
 Phone number
 Address
 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.
19. 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.
20. Now repeat the above exercise [19] to print the area of 10 squares. Hint-Use array of
objects
21. Create a class named 'Shape' with a method to print "This is a 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.

5|Page
Post-lab Exercise: Explain with example

22. Can a class be extended by more than one classes in java?


23. What happens if both super class and sub class have a field with same name?
24. How do you prevent a field or a method of any class from inheriting to sub classes?
25. Does java support multiple inheritance?

6|Page

You might also like