0% found this document useful (0 votes)
35 views

Lab 09 Inheritance2

The document provides instructions for 5 lab tasks related to object-oriented programming concepts in Java. Task 1 involves implementing an abstract class with an abstract method. Task 2 experiments with default values of instance variables in a class. Task 3 designs a bank account class with methods for credit, debit, and funds transfer. Task 4 requires coding the classes shown in a provided class diagram. Task 5 defines an abstract Shape superclass and concrete Circle and Rectangle subclasses that override abstract methods.

Uploaded by

Natasha Sadiq
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Lab 09 Inheritance2

The document provides instructions for 5 lab tasks related to object-oriented programming concepts in Java. Task 1 involves implementing an abstract class with an abstract method. Task 2 experiments with default values of instance variables in a class. Task 3 designs a bank account class with methods for credit, debit, and funds transfer. Task 4 requires coding the classes shown in a provided class diagram. Task 5 defines an abstract Shape superclass and concrete Circle and Rectangle subclasses that override abstract methods.

Uploaded by

Natasha Sadiq
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Object Oriented Programming

Instructor: Miss. Sabeen Sher


Lab# 9 Tasks
Date: 25-05-2023
Due Date: 01-06-2023
Note: Note: Submit code execution screenshots with their output Results (Create Folder
with your name)
1. Lab Task 1: Implementation Abstract Class
//abstract class
public abstract class AbPerson {
private String name;
private String gender;

public AbPerson(String nm, String gen){


this.name=nm;
this.gender=gen;
}

//abstract method
public abstract void work();

@Override
public String toString(){
return "Name="+this.name+"::Gender="+this.gender;
}

public void changeName(String newName) {


this.name = newName;
}
}
public class Employee extends AbPerson {

private int empId;

public Employee(String nm, String gen, int id) {


super(nm, gen);
this.empId=id;
}

@Override
public void work() {
if(empId == 0){
System.out.println("Not working");
}else{
System.out.println("Working as employee!!");
}
}

public static void main(String args[]){


//coding in terms of abstract classes
AbPerson student = new Employee("Dove","Female",0);
AbPerson employee = new Employee("Pankaj","Male",123);
student.work();
employee.work();
//using method implemented in abstract class - inheritance
employee.changeName("Pankaj Kumar");
System.out.println(employee.toString());
}

}
2. Lab Task 02 (Experimenting for Default Values of Instance Variables)

Implement a class Test and add 9 different data members in that class for each data type namely
(byte, short, int, long, float, double, boolean, char, String). Now in your main class, create an
object of the Test class and print the values of data members of that object. Now observe with
what values the object will be initialized by default.

public class Test {

private byte aByte;

private short aShort;

private int anInt;

private long aLong;

private float aFloat;


private double aDouble;

private boolean aBoolean;

private char aChar;

private String aString;

// Constructor

public Test(byte aByte, short aShort, int anInt, long aLong, float aFloat, double aDouble,
boolean aBoolean, char aChar, String aString) {

this.aByte = aByte;

this.aShort = aShort;

this.anInt = anInt;

this.aLong = aLong;

this.aFloat = aFloat;

this.aDouble = aDouble;

this.aBoolean = aBoolean;

this.aChar = aChar;

this.aString = aString;

// Getters and setters

public byte getByte() {

return aByte;

public void setByte(byte aByte) {

this.aByte = aByte;

public short getShort() {


return aShort;

3. Lab Task 3 A class called Account, which models a bank account of a customer, is designed
as shown in the following class diagram. The methods credit(amount) and debit(amount) add
or subtract the given amount to the balance. The method transferTo(anotherAccount,
amount) transfers the given amount from this Account to the given anotherAccount. Write
the Account class.

4. Task 4: write the codes for all the classes as shown in the class diagram.
5. Lab task 5: Abstract Superclass Shape and Its Concrete Subclasses
Shape is an abstract class containing 2 abstract methods: getArea() and getPerimeter(), where
its concrete subclasses must provide its implementation. All instance variables shall have
protected access, i.e., accessible by its subclasses and classes in the same package
In this exercise, Shape shall be defined as an abstract class, which contains:
 Two protected instance variables color(String) and filled(boolean). The protected variables
can be accessed by its subclasses and classes in the same package. They are denoted with
a '#' sign in the class diagram.
 Getter and setter for all the instance variables, and toString().
 Two abstract methods getArea() and getPerimeter() (shown in italics in the class diagram).
The
subclasses Circle and Rectangle shall override the abstract methods getArea() and getPerimeter() 
and provide the proper implementation. They also override the toString().

You might also like