[go: up one dir, main page]

0% found this document useful (0 votes)
12 views4 pages

Lab 07

The document describes three programming tasks involving constructor chaining and inheritance in Java. Task 1 creates a Class constructor that chains to set default values. Task 2 creates a BankAccount constructor that chains to set default values. Task 3 demonstrates method overriding by creating an Animal class with a speak method, and a Dog subclass that overrides the speak method. The main method creates a Dog object and calls its overridden speak method.

Uploaded by

taimoor.asad.789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Lab 07

The document describes three programming tasks involving constructor chaining and inheritance in Java. Task 1 creates a Class constructor that chains to set default values. Task 2 creates a BankAccount constructor that chains to set default values. Task 3 demonstrates method overriding by creating an Animal class with a speak method, and a Dog subclass that overrides the speak method. The main method creates a Dog object and calls its overridden speak method.

Uploaded by

taimoor.asad.789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

You might also like