Inheritance with Constructor

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
1 Inheritance Classes and Subclasses Or Extending a Class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation. Inheritance. Abstract Classes.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Classes - Intermediate
COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Staples are our staple Building upon our solution.
Modern Programming Tools And Techniques-I
GC211 Data structure Lecture 3 Sara Alhajjam.
Polymorphism.
Lecture 12 Inheritance.
Examples of Classes & Objects
using System; namespace Demo01 { class Program
Final and Abstract Classes
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Introduction to Programming with Java
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Web Design & Development Lecture 5
Week 8 Lecture -3 Inheritance and Polymorphism
Modern Programming Tools And Techniques-I Inheritance
OOP’S Concepts in C#.Net
Lecture 11 C Parameters Richard Gesick.
CSC 113 Tutorial QUIZ I.
Class Inheritance (Cont.)
Review of Object-Oriented Concepts in JAVA
Interface.
Classes & Objects: Examples
Everything the light touches, Simba, will be yours
slides created by Alyssa Harding
Unit-2 Objects and Classes
Assignment 7 User Defined Classes Part 2
S.VIGNESH Assistant Professor/CSE, SECE
S.VIGNESH Assistant Professor, SECE
Inheritance Inheritance is a fundamental Object Oriented concept
Inheritance Cse 3rd year.
JAVA Constructors.
Review of Object-Oriented Concepts in JAVA
Sampath Kumar S Assistant Professor, SECE
class PrintOnetoTen { public static void main(String args[]) {
Chapter 10: Method Overriding and method Overloading
Lecture 6 Inheritance CSE /26/2018.
Chapter 11 Inheritance and Polymorphism
Lecture 11 Parameters CSE /26/2018.
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Object Oriented Programming.
Parameters, Overloading Methods, and Random Garbage
Methods (a.k.a functions)
Lecture 6 Inheritance CSE /26/2018.
Presentation transcript:

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