Inheritance and overriding OOP, Exercises of Object Oriented Programming

Lab task of Inheritance and overriding OOP

Typology: Exercises

2018/2019

Uploaded on 07/07/2019

mukhtar-khawaja
mukhtar-khawaja 🇵🇰

2 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Inheritance:
In object-oriented programming, inheritance enables new objects to take on the properes of
exisng 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 creang a subclass is simple. At the beginning of your class declaraon, use the
extends” keyword, followed by the name of the class to inherit from:
class MountainBike extends Bicycle
{
// new elds and methods dening 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)
{
Course Code: CS2301L Object Oriented Programming. Lab 06: Inheritance and
Overriding
Centre for Advance Studies in Engineering Page 4
pf3
pf4
pf5
pf8

Partial preview of the text

Download Inheritance and overriding OOP and more Exercises Object Oriented Programming in PDF only on Docsity!

Inheritance:

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.

  1. The fi rst calls the superclass constructor.
  2. The second is used to access members (protected or public data members and methods) of the superclass. Super use cases from an Inherited Class super(parameter-list); // constructor call super.member; // some member call of the super class super.method(); // some method call of the super class

Super is a special keyword which has two-fold purpose

  1. To call the constructors of base class. 1.a.In case of calling constructor of base a. Only a constructor of a child class can call constructor of the base class using “super(…)". b. Call to “super(…)” must be the very first line in the child’s constructor c. If you don't call super(...) explicitly as the first line in a constructor, Java will automa�cally insert a call to the default no-argument superclass constructor for you super();
  2. To access member variables and methods inherited from

Overriding

}

Output

**1. Inside A

  1. B's one arg Constructor
  2. A's 1 arg Constructor
  3. A's Implementation of callMe
  4. A's Implementation of callMe
  5. B's Implementation of callMe**

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

  1. B's one arg Constructor
  2. A's 1 arg Constructor
  3. A's Implementation of callMe
  4. A's Implementation of callMe
  5. B's Implementation of callMe**

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.

  • Create a class named Reserva on
    • Data members: date(Date)
  • import java.util.Date;
  • public class DateDemo {
  • public static void main(String args[]) {
  • // Instantiate a Date object
  • Date date = new Date();
  • // display time and date using toString()
  • System.out.println(date.toString());
  • }
  • Default Constructor
  • Overloaded constructer
  • Ge er and se er.

Overriding

Person class fields are: Name and Gender.

  • Default Constructor
  • Constructor that takes 2 inputs, (Name, Gender);
  • Ge ers and se ers.
  • A toString() func�on.

Make a class Employee, derive it from Person class.

Employee has:

  • Data Members: Employee ID and Designa on, Grade (e.g 17 grade or 20 grades etc.) and Salary.
  • Declare Grade as a protected data member.
  • Default Constructor
  • Overloaded Constructor that takes inputs: Name, Gender, Employee ID, Designa on, Grade and call the Person constructor to set the values of Name and Gender using keyword “super”
  • Ge ers and Se ers.
  • ToString() method, use super.toString() and concatenate with Employee fi elds before returning.
  • Implement a calculateSalary() func on that calculates the salary based on the Grade. Use fixed salaries given below: Grade 16 – 50,000 PKR 17 – 75, 18 – 100,

Make a class Visi ng Employee, derive it from Employee class.

Visi ng Employee class:

  • The Visi�ng Employee class has an addi�onal data member Working Hours.
  • Default Constructor
  • Overloaded constructer that takes input Name, Employee ID, Gender, Designa on, Grade, Working Hours and calls the employee constructor to set the fi elds for Name, Employee ID, Gender, Designa on and Grade using keyword “super”.
  • Ge ers and Se ers.

Overriding

EmployeeVisiting Employee

  • Overridden calculateSalary(). The CalculateSalary() will work on the basis of protected data member “Grade” and private data member “Working Hours”. E.g. working hours = 24 hrs/ month and he is 17 grade person. Salary = 24 *75000/
  • ToString() method, use super.toString() and concatenate with Visi ng Employee fi elds before returning.

Create Main Class and a main func on:

  • Create two objects, Employee and Visi ngEmployee using overloaded constructors.
  • (^) Call calculateSalary() for both objects.
  • Display the objects.

Overriding