T his article is downloaded from

Similar documents
T his article is downloaded from

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

T his article is downloaded from

T his article is downloaded from

Object Oriented Programming

C Programming for Engineers Functions

Chapter 14 Abstract Classes and Interfaces

Java Object Oriented Design. CSC207 Fall 2014

Ch02. True/False Indicate whether the statement is true or false.

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Full file at Chapter 2 - Inheritance and Exception Handling

Data Structures and Other Objects Using C++

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Inheritance and Polymorphism

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Inheritance and Polymorphism

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

C++ (classes) Hwansoo Han

Inheritance, and Polymorphism.

Questions Answer Key Questions Answer Key Questions Answer Key

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Inheritance and Polymorphism

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

CSC330 Object Oriented Programming. Inheritance

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

ITI Introduction to Computing II

Object Oriented Design

IS0020 Program Design and Software Tools Summer, 2004 August 2, 2004 in Class

Introduction to Programming Using Java (98-388)

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Making New instances of Classes

Questions Answer Key Questions Answer Key Questions Answer Key

ECE 122. Engineering Problem Solving with Java

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Lecture 10: building large projects, beginning C++, C++ and structs

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Chapter 10 Classes Continued. Fundamentals of Java

ITI Introduction to Computing II

C++ Important Questions with Answers

CS-202 Introduction to Object Oriented Programming

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Memory management COSC346

Inheritance and Polymorphism

Inheritance, Polymorphism, and Interfaces

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

MODULE 3q - An Extended Java Object

Tutorial 7. If it compiles, what does it print? Inheritance 1. Inheritance 2

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

OVERRIDING. 7/11/2015 Budditha Hettige 82

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Practice Questions for Chapter 9

First IS-A Relationship: Inheritance

Class, Variable, Constructor, Object, Method Questions

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

Inheritance. Software Engineering with Inheritance. CSC330 Object Oriented Programming. Base Classes and Derived Classes. Class Relationships I

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Module 10 Inheritance, Virtual Functions, and Polymorphism

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Constructor in Java. What is a Constructor? Rules for create a Java Constructor

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Use the template below and fill in the areas in Red to complete it.

Unit 9 Study Guide. Multiple Choice (2 points) Identify the choice that best completes the statement or answers the question.

Polymorphism Part 1 1

Inheritance, Polymorphism and the Object Memory Model

T his article is downloaded from

G Programming Languages - Fall 2012

QUESTIONS FOR AVERAGE BLOOMERS

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Framework Fundamentals

Question 2. [5 points] Given the following symbolic constant definition

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Data Abstraction. Hwansoo Han

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Chapter 6 Introduction to Defining Classes

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Programming in C# Inheritance and Polymorphism

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Programming for Engineers Functions

CSharp. Microsoft. UPGRADE-MCAD Skills to MCPD Dvlpr by Using the MS.NET Frmwk

Methods (2A) Young Won Lim 12/10/14

Declarations and Access Control SCJP tips

CMSC 132: Object-Oriented Programming II

PROGRAMMING IN C AND C++:

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Powerful JavaScript OOP concept here and now. CoffeeScript, TypeScript, etc

F I N A L E X A M I N A T I O N

Previously, on Lesson Night... From Intermediate Programming, Part 1

Sections Covered Chapter 6.1, , , 9.7, , , Section 6.1

Y oung W on Lim 9 /1 /1 7

Transcription:

Commonly asked interview questions on inheritance I have summed up all the inheritance control flow related concepts generally asked during O O P S technical interview. M ore or les s, if you unders tand the below example, then you will be able to ans wer any ques tions as ked by interviewers on flow of c ontrols between the bas e c las s and the derived c las s.

For this example, Let us consider the following class which we'll us e as a bas e c las s.. class Shapes s tatic Shapes () C ons ole.writeline("shape Static M ethod"); public Shapes ()

C ons ole.writeline("shapes c ons truc tor"); public void A rea() C ons ole.writeline("shape A rea"); public void C irc umferenc e() C ons ole.writeline("shape C irc umferenc e"); public virtual void s ides () C ons ole.writeline("shapes Sides "); ; N ow let us derive another c las s from this bas e c las s. c las s C irc le : Shapes s tatic C irc le() C ons ole.writeline("c irc le Static M ethod"); public C irc le() C ons ole.writeline("c irc le c ons truc tor"); public new void A rea() C ons ole.writeline("c irc le A rea");

public override void s ides () C ons ole.writeline("c irc le C irc umferenc e"); ; Now try this code out. Shapes a1 = new Shapes (); a1.a rea(); a1.c irc umferenc e(); a1.s ides (); // output: Shape Area Shape Circumference Shapes Sides Static method is c alled when the c las s is loaded into the memory. So, the moment you us e the c las s, s tatic method of that c las s is c alled and s o, Static method c all is before constructor method call. A lso, static method is called only once during your c ode exec ution. Let us now try us ing an objec t of parent c las s and as s ign it to the ins tanc e of derived c las s Shapes a2 = new C irc le(); a2.a rea(); a2.c irc umferenc e();

a2.s ides (); // output: Circle Static Method Circle constructor Shape Area Shape Circumference Circle Sides H ere, we s ee that the c ompiler c alls the s tatic method of bas e class after calling static method of derived class. However execution of the constructor method is exactly reverse. We have an objec t of type Shapes, but it referenc es an objec t of type Circle. T hat is why, the bas e c las s c ons truc tor gets c alled firs t, followed by the derived c las s c ons truc tor. N ow we c all Area () and find the method that's exec uted is the bas e c las s method. T his is bec aus e you have dec lared objec t to be of the bas e type whic h is Shapes in this c as e. Sinc e there is no overridden C ircumference method in derived class, when we call C ircumference (), base class method is called.now, when we call sides () method, we find that the derived class method got called. T his is bec aus e in the bas e c las s the method is prototyped as public virtual void sides () and in the derived class we have overridden it by using public override void sides (). Now try the following code out. C irc le a2 = new C irc le(); a2.a rea(); a2.c irc umferenc e(); a2.s ides (); // output: Circle Static Method

Circle constructor Circle Area Shape Circumference Circle Sides A s explained above, static methods and constructor were called as expected. Since we have provided a new implementation to area () method of bas e c las s in derived c las s, area () method of derived class gets called. Since there is no C ircumference method in derived class, C ircumference () method of base class gets c alled. T he fac t that we c ould invoke the C irc umferenc e () method of bas e c las s is proof of inheritanc e in C #. Now let us overload circumference method of base class and make original C irc umferenc e() method as virtual public virtual void C irc umferenc e() C ons ole.writeline("shape C irc umferenc e"); public void C irc umferenc e(int radius ) C onsole.writeline("shape C ircumference radius is "+radius ); Now run this code out. Shapes a1 = new Shapes (); a1.c irc umferenc e(); a1.c irc umferenc e(8 ); // output

Shape Circumference Shape Circumference radius is 8 O kay, that went fine as expec ted. N ow let's override Circumference func tion in the derived c las s as s hown below: public override void C irc umferenc e() C ons ole.writeline("c irc le C irc umferenc e"); public new void C irc umferenc e(int radius ) C onsole.writeline("c ircle C ircumference radious is "+radius ); N ow let's try this c ode out. Circle a1 = new C irc le(); a1.c irc umferenc e(); a1.c irc umferenc e(8 ); // output Circle Static Method Circle Circumference Circle Circumference radius is 8 Well, that went fine too. T hus if you have overloaded methods, you c an mark s ome of them as virtual and override them in the derived class or create an entirely new implementation in derived c las s. N ow, it will be very eas y to unders tand the output of the below

c ode. Shapes a1 = new C irc le(); a1.c irc umferenc e(); a1.c irc umferenc e(8 ); // output Circle Static Method Circle constructor Circle Circumference Shapes Circumference radius is 8 Before we end, let s see some stuff on overloaded constructors too. C reate an overloaded c ons truc tor in bas e c las s as s hown below protec ted int _s ides ; public Shapes () _s ides = 5 ; C ons ole.writeline("shapes c ons truc tor"); public Shapes (int isides ) _s ides = isides ; N ow let us modify our c hild c las s c ons truc tor as s hown below: public C irc le() C ons ole.writeline("c irc le c ons truc tor");

C ons ole.writeline(_s ides ); N ow let s try running this c ode Circle a1 = new C irc le(); // C irc le a1 = new C irc le(8 ); C ompile time error output: Circle Static Method Circle constructor 5 T he bas e c las s has two overloaded c ons truc tors. O ne that takes zero arguments and one that takes an int parameter. I n the derived class we only have the zero argument constructors. C ons truc tors are not inherited by derived c las s es. T hus we cannot instantiate a derived class object using the constructor that takes an int as parameter N ow take a look at this s ec ond derived c las s. class Square : Shapes //Here I am explicitly informing the compiler about the overload constructor of the base class to be called public Square(int s ide1 ): bas e(s ide1 ) C ons ole.writeline(_s ides ); //Here I am explicitly informing the compiler to first call

the overloaded constructor of the base class. public Square(int s ide2, int s ide1 ) : this (s ide1 ) C ons ole.writeline(s ide2 ); ; H ere we have two c ons truc tors, one that takes a s ingle int parameter and one that takes two int parameters. Now run the following code. Square du1 = new Square(6 ); //O utput 6 Square du2 = new Square(5, 7 ); //O utput 7 5 First code calls the base class constructor and set the protected _sides variable to 6. And then the derived class constructor prints this _sides variable onto the console. Second code first calls the derived class double parameter constructor and print 7 onto the console. Since we have made call to the derived class single parameter constructor method using this object, this derived class constructor in turn calls the base class constructor and set the protected _sides variable to 5 and then the derived class constructor print this _sides variable onto the console. here