




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Lab task of Inheritance and overriding OOP
Typology: Exercises
1 / 8
This page cannot be seen from the preview
Don't miss anything!





In object-oriented programming, inheritance enables new objects to take on the proper�es of
exis�ng objects. A class that is used as the basis for inheritance is called a superclass or base
class. A class that inherits from a superclass is called a subclass or derived class.
The syntax for crea�ng a subclass is simple. At the beginning of your class declara�on, use the
“extends” keyword, followed by the name of the class to inherit from:
class MountainBike extends Bicycle
{
// new fields and methods de fi ning a mountain bike would go here }
Code Example public class A { A() { System.out.println("Inside A"); }
}
public class B extends A {
B() { System.out.println("Inside B"); }
}
public class C extends B { C() {
System.out.println("Inside C"); }
}
public class Lab { public static void main(String[] args) {
Overriding
C c=new C();//object of type C } }
Output Inside A Inside B Inside C
Member Access and Inheritance
Although a subclass includes all of the members of its superclass, it cannot access those
members of the superclass that have been declared as private. A class member that has been
declared as private will remain private to its class. It is not accessible by any code outside its
class, including subclasses. To avoid this we use protected access specifier. Protected enforces
the type or member can only be accessed by code in the same class, or in a derived class.
Using super
Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword super.
super has two general forms.
Super is a special keyword which has two-fold purpose
Overriding
}
Output
**1. Inside A
Coding Example public class A { protected int i;
public A() { System.out.println("Inside A"); }
public A(int i) { this.i=i; System.out.println("A's 1 arg Constructor"); }
void callMe() { System.out.println("A's implementation of callMe."); }
} public class B extends A{
B() { System.out.println("Inside B"); }
B(int input) { super(input); System.out.println("B's one arg Constructor"); }
@Override void callMe() { super.callMe(); System.out.println("B's implementation of callMe."); } } public class Lab {
public static void main(String[] args) {
Overriding
B b=new B(5); A a=new A(6); a.callMe(); b.callMe();
}
} Output
**1. A's 1 arg Constructor
Ques � on 1: You are going to develop a reserva�on system for a train or flight related journeys. Reserva�on can be of two types; train reserva�on and flight reserva�on. Generally when we speak about reserva�on, one thing that’s common in all types of reserva�ons is Time (including day and �me of that day). We might need to specify some extra features for train or flight reserva�on like carriage number or flight number etc. In this exercise create a Reserva � on class and inherit this class in Train reserva � on and Flight reserva � on classes.
Overriding
Person class fields are: Name and Gender.
Make a class Employee, derive it from Person class.
Employee has:
Make a class Visi � ng Employee, derive it from Employee class.
Visi � ng Employee class:
Overriding
EmployeeVisiting Employee
Create Main Class and a main func � on:
Overriding