Name: Mohammad Taimoor Asad
CMSID: 458118
Class: SE-14 B
Faculty of Computing
CS212: Object Oriented
Programming Class: BESE-14B
Lab 7: Constructor Chaining and
Inheritance Methods
Date: 20th March, 2024
2:00pm – 5:00 pm
Task 1:
Code:
public class Class {
int studentId;
Stirng name;
int age;
String course;
Class(String name){
this.name = name;
this.age = 20;
this.course = "OOP";
this.studentId = 458118;
}
Class(int studentId, String name, String course){
this(name);
this.course = course;
this.studentId = studentId;
}
Class(int studentId, String name, int age, String course){
this(studentId , name, course);
this.age = age;
}
public static void main(String[] args){
Class student1 = new Class("Taimoor");
system.out.print(student1.age);
}
Output:
Task 2:
Code:
public class BankAccount {
int accountNumber;
String accountHolderName;
int balance;
String accountType;
BankAccount(String accountHolderName){
this.accountHolderName = accountHolderName;
this.accountNumber = 456546;
this.accountType = "Current";
this.balance = 1000;
}
BankAccount(int accountNumber, String accountHolderName, String
accountType){
this(accountHolderName);
this.accountType = accountType;
this.accountNumber = accountNumber;
}
BankAccount(int accountNumber, String accountHolderName, int balance,
String accountType){
this(accountNumber , accountHolderName, accountType);
this.balance = balance;
}
public static void main(String[] args){
BankAccount account1 = new BankAccount(458118, "Taimoor",
"Current");
System.out.printf("Details are: \nHolder Name: %s\nAccount Number:
%d\nAccount type: %s\nBalance: %d",account1.accountHolderName,
account1.accountNumber,account1.accountType,account1.balance );
}
Output:
Task 3:
Code:
public class Animal {
String name;
Animal(String name){
this.name = name;
}
public void speak(){
System.out.println("An animal makes a sound");
}
public static class dog extends Animal{
String breed;
dog(String name, String breed){
super(name) ;
this.breed = breed;
}
public void speak(){
System.out.println("A dog barks");
}
}
public static void main(String[] args){
dog myDog = new dog("Buddy", "Golder Retriever");
myDog.speak();
}
}
Output: