Inheritance with Constructor Lecture 6B Inheritance with Constructor CSE 1322 4/26/2018
OBJECTIVES What is a constructor? Why a constructor is needed 2 OBJECTIVES What is a constructor? Why a constructor is needed How a constructor is defined and called? Features of a constructor Default constructor Different types of constructors Accessing base class constructors 4/26/2018 2
3 Introduction Constructor is a method of a class with same of the class name with no return type A constructor is used to create and initialize an object of a class 4/26/2018 3
Constructor Concept class Student { long roll; String name; 4 Constructor Concept class Student { long roll; String name; float mark; public Student() // Constructor Method { // ... ... ... } void display() // Normal Method 4/26/2018 4
Constructor Concept class Student { long roll; String name; 5 Constructor Concept class Student { long roll; String name; float mark; public Student(){ } // Empty Constructor public void display() { // ... ... ... } 4/26/2018 5
6 Constructor Concept 4/26/2018 6
Class with Multiple Constructors 7 Class with Multiple Constructors 4/26/2018 7
8 Types of Constructors Based on number of arguments, constructors are classified into the following types: Default Constructor Argumented Constructor Copy Constructor Empty Constructor ???? 4/26/2018 8
An Example of Using Argumented and Copy Constructors /* J10_08.java */ /* Using Multiple Constructor Methods */ class Student { int Roll; String Name; float Mark; Student(){ // Default Constructor Roll = 318; Name = "Masud"; Mark = 85.5F; } Student(int R, String N, float M){ // Argumented Constructor Roll = R; Name = N; Mark = M; Student(Student S) // Copy Constructor { Roll = S.Roll ; Name = S.Name ; Mark = S.Mark; } // Copy Constructor Defined void display() { System.out.println("Roll is : " +Roll); System.out.println("Name is : " +Name); System.out.println("Mark is : " +Mark); public class J10_08{ public static void main(String args[]) { Student S1 = new Student(); // Default Constructor Invoked Student S2 = new Student(101, "Monirul", 80); // Argumented Constructor Invoked Student S3 = new Student(S2); // Copy Constructor Invoked S3.display(); // displays Record of S2 } An Example of Using Argumented and Copy Constructors 9 /* Student.java */ class Student { int Roll; String Name; float Mark; Student(){ // Default Constructor Roll = 318; Name = “Mokter"; Mark = 95.5F; } Student(int R, String N, float M){ // Argumented Constructor Roll = R; Name = N; Mark = M; Student(Student S){ // Copy Constructor Roll = S.Roll ; Name = S.Name ; Mark = S.Mark; } // Copy Constructor Defined void display() { System.out.println("Roll is : " +Roll); System.out.println("Name is : " +Name); System.out.println("Mark is : " +Mark); 4/26/2018 9
An Example of Using Argumented and Copy Constructors /* J10_08.java */ /* Using Multiple Constructor Methods */ class Student { int Roll; String Name; float Mark; Student(){ // Default Constructor Roll = 318; Name = "Masud"; Mark = 85.5F; } Student(int R, String N, float M){ // Argumented Constructor Roll = R; Name = N; Mark = M; Student(Student S) // Copy Constructor { Roll = S.Roll ; Name = S.Name ; Mark = S.Mark; } // Copy Constructor Defined void display() { System.out.println("Roll is : " +Roll); System.out.println("Name is : " +Name); System.out.println("Mark is : " +Mark); public class J10_08{ public static void main(String args[]) { Student S1 = new Student(); // Default Constructor Invoked Student S2 = new Student(101, "Monirul", 80); // Argumented Constructor Invoked Student S3 = new Student(S2); // Copy Constructor Invoked S3.display(); // displays Record of S2 } 10 An Example of Using Argumented and Copy Constructors /* CopyConstructorEx1.java */ public class CopyConstructorEx1 { public static void main(String args[]) { StudentS1 = new Student(); // Default Constructor Invoked System.out.println("Record of S1 : " ); S1.display(); System.out.println(); Student S2 = new Student(101, "Hossain", 80); // Argumented Constructor Invoked System.out.println("Record of S2 : " ); S2.display(); Student S3 = new Student(S1); // Copy Constructor Invoked System.out.println("Record of S3 : " ); S3.display(); // displays Record of S1 } Record of S1 : Roll is : 318 Name is : Mokter Mark is : 95.5 Record of S2 : Roll is : 101 Name is : Hossain Mark is : 80.0 Now Record of S3 : Output: 4/26/2018 10
Calling Base and Child Class Constructors 11 Calling Base and Child Class Constructors 4/26/2018 11
Calling Base Constructors Using super() 12 Calling Base Constructors Using super() class Employee{ String empName; int empId; Employee(){ System.out.println("Employee class non-argumented Constructor called"); } // End of the Employee() constructor } // End of Employee class public class Payroll extends Employee { float baseSalary; public Payroll() { super(); // implicitly called by default - optional System.out.println("Payroll class non-argumented constructor called"); } // End of the Payroll() constructor public static void main(String[] args) { Payroll pr1 = new Payroll(); } // end of the main() method } // End of the Payroll class Employee class non-argumented Constructor called Payroll class non-argumented constructor called 4/26/2018 12
Calling Base Constructors Using super() 13 Calling Base Constructors Using super() class Employee{ String empName; int empId; Employee(){ System.out.println("Employee class non-argumented Constructor called"); } // End of the Employee() constructor Employee(String empName, int empId){ this(); System.out.println("============================="); this.empName = empName; this.empId = empId; System.out.println("Employee class argumented Constructor called"); } // // End of the Employee() argumented constructor } // End of Employee class 4/26/2018 13
Calling Base Constructors Using super() 14 Calling Base Constructors Using super() public class Payroll extends Employee { float baseSalary; public Payroll() { super(); // implicitly called by default - optional System.out.println("Payroll class non-argumented constructor called"); } // End of the Payroll() constructor public Payroll(String empName, int empId, float baseSalary) { super(empName, empId); // Does not call implicitly - Required System.out.println("Payroll class argumented constructor called"); this.empName = super.empName; this.empId = super.empId; this.baseSalary = baseSalary; System.out.println("In Payroll empName : " + this.empName); System.out.println("In Payroll empId : " + this.empId); System.out.println("In Payroll baseSalary : " + this.baseSalary); } public static void main(String[] args) { Payroll pr2 = new Payroll("John", 303, 78000f); } // end of the main() method } // End of the Payroll class 4/26/2018 14
Calling Base Constructors Using super() 15 Calling Base Constructors Using super() public static void main(String[] args) { Payroll pr2 = new Payroll("John", 303, 78000f); } // end of the main() method } // End of the Payroll class Output: Employee class non-argumented Constructor called ============================= Employee class argumented Constructor called Payroll class argumented constructor called In Payroll empName : John In Payroll empId : 303 In Payroll baseSalary : 78000.0 4/26/2018 15
More on Inheriting Base Class with Constructor 16 More on Inheriting Base Class with Constructor 4/26/2018 16