Lab Manual 04
Lab Manual 04
(Java)
Lab Manual No 04
Dated:
31-Jan-2024
Semester:
2024
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent
object.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes.
When you inherit from an existing class, you can reuse methods and fields of parent class, and you can
add new methods and fields also.
extends Keyword
extends is the keyword used to inherit the properties of a class. Following is the syntax of extends
keyword.
Syntax
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
In the terminology of Java, a class which is inherited is called parent or super class and the new class is
called child or subclass.
43 | P a g e
Example # 01:
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){ Programmer
p=new Programmer(); System.out.println("Programmer
salary is:"+p.salary); System.out.println("Bonus of
Programmer is:"+p.bonus);
}}
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
46 | P a g e
Super Keyword: The super keyword is similar to this keyword. Following are the scenarios where the
super keyword is used.
4 It is used to differentiate the members of superclass from the members of subclass, if they
have same names.
If a class is inheriting the properties of another class. And if the members of the superclass have the
names same as the sub class, to differentiate these variables we use super keyword as shown below.
super.variable
super.method();
Sample Code
This section provides you a program that demonstrates the usage of the super keyword.In the given
program, you have two classes namely Sub_class and Super_class, both have a method named display()
with different implementations, and a variable named num with different values. We are invoking
display() method of both classes and printing the value of the variable num of both classes. Here you can
observe that we have used super keyword to differentiate the members of superclass from subclass.
47 | P a g e
class Super_class {
int num = 20;
super(values);
Sample Code
The program given in this section demonstrates how to use the super keyword to invoke the
parametrized constructor of the superclass. This program contains a superclass and a subclass,
where the superclass contains a parameterized constructor which accepts a string value, and we
used the super keyword to invoke the parameterized constructor of the superclass
Example:
class Superclass {
int age;
Superclass(int age) {
this.age = age;
}
public void getAge() {
System.out.println("The value of the variable named age in super class is: " +age);
}
}public class Subclass extends Superclass {
Subclass(int age) {
super(age);
}public static void main(String argd[]) {
Subclass s = new Subclass(24);
s.getAge();}}
Instanceof Operator:
In this example we will show how to use the operator instanceof in Java.This operator is a Type
Comparison Operator and can be used when we want to check if an object is an instance of a specific
class, an instance of a subclass, or an instance of a class that implements a particular interface.
The instanceof operator compares an object to a specified type and returns true if the type of object
and the specified type are the same.
Lab Tasks:
Using inheritance create a class Vehicle as super class. Then create motorcycle, car, truck that
inherits the basic functionalities of a vehicle from the super class. Then create the user class in
which user can use any of the vehicles.
A default constructor.
A constructor that initializes the attribute, where cs is the
clockSpeed.
Two methods:
getClockSpeed() – return the clockSpeed value associated
with the CPU object.
setClockSpeed(cs) – change the clockSpeed value associated
with the CPU object, where cs is the new value.
51 | P a g e
getCPU() – return the CPU object associated with the Computer
object
setCPU(c) – change the CPU object associated with the
Computer object, where c is the new CPU object.
f) The code associated with your main() function should test your three class
definitions. It is important that this code use each constructor defined in each class.
Develop a program that helps the user to draw basic geometrical shapes using Turtles. The
program specification is as follow.
a) Develop a class named JPoint which stores x and y coordinates of stored points with
corresponding getters & setters.
b) Develop an abstract Shape class that contains following members.
Test your program by creating one object of each type and calculate the areas
and perimeters.
Develop a program that contains two classes. The specification of both classes is as follow:
a) Data Class
b) EncryptedData Class
This class extends the Data class.
Encryption Technique :
This is an encryption technique which is a combination of two steps. These steps are as follow:
Step 1: Change the state of each and every bit of lower byte of character, reverse the order of bytes in
character and toggle alternative bits of new lower byte.
Step 2: Create an array of Strings. Length of Array is equal to ceil of square root of length of stored data.
Any String in array can store maximum characters equal to length of array. Store data column
wise is array as first character of data goes to first character of first array , second goes to first
character of second array and so on until first columns are finished then start filling second
columns of all arrays until data and array both are finished. If there are some character fields
in arrays remained but data is exhausted then fill the remaining ones with ‘*’. You can store
data row wise back to data field to store encrypted data.
First K O S *
String
Second L P T *
String
Third M Q * *
String
Fourth N R * *
String
Lab Tasks:
ComputerGrades.java file and sample class text file is provided with the manual. Perform the
following tasks after running the given program.
How would you modify the ComputeGrades sample program if the formula for computing
the course grade were different for freshman, sophomore, junior, and senior
undergraduate students?
In the ComputeGrades sample program, we set the default size of the roster array to 25.
Modify the program so the size of the array will be increased if the input file contains
more than 25 students. You need to add a method that expands the array, say by 50
percent.
Modify the ComputeGrades sample program by using a list (ArrayList or LinkedList)
instead of an array.
55 | P a g e
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION
ENGINEERING COMPUTER ENGINEERING DEPARTMENT
56 | P a g e