Diamond Effect in Object Oriented Programming Languages



Similar documents
International Journal of Advanced Research in Computer Science and Software Engineering

COMPARISON OF OBJECT-ORIENTED AND PROCEDURE-BASED COMPUTER LANGUAGES: CASE STUDY OF C++ PROGRAMMING

C++ INTERVIEW QUESTIONS

13 Classes & Objects with Constructors/Destructors

Java Classes. GEEN163 Introduction to Computer Programming

Fundamentals of Java Programming

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

Computer Information Systems (CIS)

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Johannes Sametinger. C. Doppler Laboratory for Software Engineering Johannes Kepler University of Linz A-4040 Linz, Austria

Building Java Programs

Chapter 1 Fundamentals of Java Programming

Lesson Name: Introduction of OOP

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

McGraw-Hill The McGraw-Hill Companies, Inc.,

Design by Contract beyond class modelling

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

A Comparative Analysis of Structured and Object-Oriented Programming Methods ASAGBA, PRINCE OGHENEKARO; OGHENEOVO, EDWARD E. CPN, MNCS.

Objective C and iphone App

The first program: Little Crab

CEC225 COURSE COMPACT

How to Build a Simple Pac-Man Game

Moving from CS 61A Scheme to CS 61B Java

Introduction to Object-Oriented Programming

2 The first program: Little Crab

Tutorial on Writing Modular Programs in Scala

Algorithms, Flowcharts & Program Design. ComPro

Chapter 8 Approaches to System Development

Ch 7-1. Object-Oriented Programming and Classes

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

Konzepte objektorientierter Programmierung

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

Parsing Technology and its role in Legacy Modernization. A Metaware White Paper

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Java (12 Weeks) Introduction to Java Programming Language

Software Engineering. Software Engineering. Component-Based. Based on Software Engineering, 7 th Edition by Ian Sommerville

ATM Case Study OBJECTIVES Pearson Education, Inc. All rights reserved Pearson Education, Inc. All rights reserved.

Objects for lexical analysis

How To Teach Object Oriented Programming At An Introductory Level Course

Object-Oriented Programming in Java

Java Interview Questions and Answers

Friendship and Encapsulation in C++

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

The C Programming Language course syllabus associate level

EVALUATING METRICS AT CLASS AND METHOD LEVEL FOR JAVA PROGRAMS USING KNOWLEDGE BASED SYSTEMS

Chapter 6: Programming Languages

OBJECT ORIENTED EXTENSIONS TO SQL

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Glossary of Object Oriented Terms

Twin A Design Pattern for Modeling Multiple Inheritance

XFlash A Web Application Design Framework with Model-Driven Methodology

Comparing the Testing Approaches of Traditional, Object-Oriented and Agent- Oriented Software System

A Software and Hardware Architecture for a Modular, Portable, Extensible Reliability. Availability and Serviceability System

Object-Based Databases

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

Part VI. Object-relational Data Models

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Aspect-Oriented Programming

J a v a Quiz (Unit 3, Test 0 Practice)

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

II. TYPES OF LEVEL A.

TRANSITION FROM TEACHING VB6 TO VB.NET

Java Application Developer Certificate Program Competencies

How To Draw A Cell Phone Into A Cellphone In Unminimal Diagram (Uml)

Kentico Content Management System (CMS) Managing Events and News Content

OOP? What is OOP? Why? OOP in a nutshell. Stéphane Ducasse 2.1

1. Overview of the Java Language

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Software Engineering Techniques

CSCI 3136 Principles of Programming Languages

Server-Side Scripting and Web Development. By Susan L. Miertschin

a. Inheritance b. Abstraction 1. Explain the following OOPS concepts with an example

Object Oriented Design

Checking Access to Protected Members in the Java Virtual Machine

3 Improving the Crab more sophisticated programming

Java Programming Language

Advanced Data Structures

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer

The CLIPS environment. The CLIPS programming language. CLIPS production rules language - facts. Notes

Object Oriented Software Design II

Reusability of WSDL Services in Web Applications

Transcription:

Diamond Effect in Object Oriented Programming Languages Rajesh Jangade, Anil Barnwal, Satyakam Pugla Amity Institute of Biotechnology, Amity University, Noida, Uttar Pradesh, India Abstract---Now a day the maximum percentage of the people from the world of computer programmers are using object oriented programming languages like C++, Java, Eiffel, Dot Net and etc. Why, because these languages are incorporated with some easiness, advanced, re-useful and needful features. And Inheritance is most important portion of any object oriented programming languages that plays vital role to like the language by programmers. Here I am going to discuss some basics problems and their solutions related to a situation of multiple inheritance called Diamond Effect. This problem comes in existence when we use multiple inheritance where one class is inherited by two different sub classes and again these two subclasses are inherited by any single class. Multiple inheritance in object oriented language like C++ is a powerful, but tricky tool, that often leads to problems if not handled carefully. Diamond effect occurs in most of the object oriented languages and each language has some different syntax for coding. In this paper whatever coding is used, will be in the reference of C++. Keywords: OOP, inheritance, diamond effect, base class, derived class and virtual base class. I. INTRODUCTION Introduction of diamond effect includes so many things and topics that we have to understand first. Here I am trying to emphasize on the topics which will be coming in the way of diamond effect problems. Rest of all other topics will be discussed lightly sometimes [7][8]. The main path is OOP Language => Inheritance => Multiple Inheritance => Diamond Effect Introduction of Object Oriented Programming Language: Any computer language which supports the following features is called object oriented programming language: A. Class and Object Class is a collection of properties of anything where property includes each and everything related to that thing. Whereas object is a real time entity which properties are described or written inside the class. In other word we can say that object is just an instance of the predefined class. B. Data Abstraction Data abstraction is a way to show only required properties of anything and hide the other details or properties which are not required currently. ple: Electric power supply system in a building, the people who are living in the building are given only switch boards to use. We know which switch is for fan and witch switch is for tube light and so on. If we need to on the fan then we just need to press the switch which is connected to fan only. Here we don t need to know about the internal wire connection the switch board. Here we are given switches to use and all others details of the switch board is already hidden, is called data abstraction. C. Data Encapsulation Data Encapsulation is way to bind or wrap the related things together. In together word we can say that all the properties of any things should wander together. Whenever we need any of the properties we just need to search the thing and then we call desired property, instead of searching the property and the things separately. D. Inheritance Inheritance is a way to use the properties of predefined class in the new classes. We can say, it provides a facility which is known as reusability in area of computer languages. For example we know any person is inherited by his son so his son can use all or some of properties of father. In such case, the class which properties are being used by is called base/parent/super class and the class which is using properties is called derived/child/sub class. The types of inheritance and their properties will be discussed later in paragraph 1.2. II. CONCEPT OF INHERITANCE It is already briefly discussed in paragraph 1.1.4; inheritance is a way to use the properties of predefined class in the new classes [1][3]. Inheritance is denoted by arrows as shown in Fig. 1 that shows connectivity between two classes. Fig. 1 Arrows: used to connect two classes. In coding the special symbol colon (:) is used as symbol of inheritance. There are four types of inheritance. A. Simple Inheritance When any single class inherits only one class then it is called simple or single inheritance. www.ijcsit.com 3104

A B Animal Base/Parent/Super Derived/Child/Sub Class (a) Base/Parent/Super Class Base First level inheritance Derive 1 Second level inheritance Derive 2 (b) Fig.2 (a) and (b) show the two forms of simple inheritance. In Fig. no 2(a) one class A is inherited by other class B, similarly in Fig. 2(b) class Dog inherits the properties of class Animal. class A Dog Derived/Child/Sub Class class B : public A class Animal class Dog : public Animal (b) Fig. 3 (a) and (b) showing the example of multilevel inheritance class A class B : public A class C : public B C. Hierarchical Inheritance When one class is inherited by more than one class then it is called hierarchical inheritance. Base Derive 1 Derive 2 B. Multilevel Inheritance When one class is inherited by second class and again this second class is inherited by third class then it is called multilevel inheritance. It is diagrammatically shown in Fig. 3 [1][3]. Student (a) A Base Class Arts Science Commerc B Derived class of class A (b) C (a) Derived class of class B Fig. 4(a) and (b) showing the examples of multilevel inheritance www.ijcsit.com 3105

In Fig. 4(b) it is shown that students of arts, science and commerce are ultimately students so these tree different classes Arts, Science and Commerce inherit the properties of one base class Student. class Student class Arts : public Student class Science : public Student class Commerce : public Student D. Multiple Inheritance When two or more than two classes are inherited by one class then it is called multiple inheritance[1][3][5]. In other words we can say that in multiple inheritance any single class inherits more than one base classes as shown in Fig. 5. Base 1 Base 2 III. CONCEPT OF MULTIPLE INHERITANCE We have above discussed that in multiple inheritance any single class uses the properties of more than one base class. In other words, there is a class being derived from many classes [1][3][5]. It is clear from Fig. 6(a) that result of the student is combined evaluation of both internal and external exams. So the Result class inherits the properties of both class Internal and External. Similarly in Fig. 6(b) there are two different classes Animals and Pet, and the third class Cat is a pet animal so this Cat class uses the properties of both Animals and Pet classes. Basically the Diamond Effect is a situation occurs in the case of multiple inheritance and will be discussed later. class Internal class External class Result : public Internal, public External Derived Fig. 5 Multiple Inheritance Internal External class Base1 class Base2 class Derived : : public Base1, public Base2 The detail description of multiple inheritance is discussed in paragraph 1.3 with suitable examples Animals Result (a) Cat Pet (b) Fig. 6 ples of multiple inheritance www.ijcsit.com 3106

Now let us see the copy of program of above example: //Declaration of class Internal class Internal public: char subject[5][20]; int Imark[5]; void getinternaldetails(); void showinternaldetails(); //Declaration of class External class External public: char subject[5][20]; int Emark[5]; void getexternaldetails(); void showexternaldetails(); //Declaration of class Result class Result : public Internal, public External public: char subject[5][20]; int Tot_Mark[5]; void getresult(); void showresult(); // Definition of class Internal void Internal : : getinternaldetails() cout<< \nenter 5 name of subjects and marks obtained by student\n ; cout<< Enter Subject cout<< Enter Marks: ; cin>>imark[i]; void Internal : : showinternaldetails() cout<< \t\t <<Imark[i]; // Definition of class External void External : : getexternaldetails() cout<< \nenter 5 name of subjects and marks obtained by student\n ; cout<< Enter Subject cout<< Enter Marks: ; cin>>emark[i]; void External : : showexternaldetails() cout<< \t\t <<Emark[i]; // Definition of class Result void Result : : getresult() cout<< \nenter 5 name of subjects \n ; cout<< Enter Tot_Mark[i] = Imark[i] + Emark[i]; Subject void Result: : showresult() cout<< \t\t <<Tot_Mark[i]; In the above program in function geresult() it is shown that Tot_Mark are the sum of Imark and Emark of the corresponding subject. Here Imark and Emark are the members of class Internal and External subsequently but being used in function getresult() of class Result. The getresult() is able to access these variables only because of the inheritance. IV. INTRODUCTION OF DIAMOND EFFECT Now let us discuss about a modified situation of above program where there are four classes: Student Internal External Result The Student includes some personal details, internal include details of internal exam, External include details of external exam and class Result include the result of the students as shown above [4][6][7]. Here the class Student is inherited by both the classes Internal and External using hierarchical inheritance and again these two classes Internal and External are inherited by single class Result using multiple inheritance. And above both situations are shown in Fig. 7. Here both the situations are shown in two separate Figures. Let s try to transform the Fig. 7 in form of single Fig. and in combined form of hierarchical and multiple inheritance and Fig. 7 becomes now Fig. 8. It is cleared from Fig. 8 that, it looks like a diamond and this way of representing the structure of classes is called diamond effect. www.ijcsit.com 3107

Internal Internal Fig. 7 Showing hierarchical and multiple inheritances separately Internal Student Result Student Result External External External Fig. 8 Showing an aspect of Diamond Effect In technical word we can define the diamond effect as when one base class is having two different derived classes and again these derived classes have common and single derived class then this situation is called Diamond Effect in object oriented programming language. [3][7] In Fig. 8 one class Student is having two different derived classes Internal and External and again these two derived classes is having single derived class Result. Classes Internal and External are child class of base class Student and at same time these two classes are also base class of one common derived class Result. class Student class Internal : public Student class External : public Student class Result : public Internal, public External Copy of the program of classes Internal, External and Result has already been written in paragraph 1.3. So here I am giving the copy of program of class Student only. //Declaration of class Student class Student public: char SName[20]; int Roll_No; char course[20]; void getstudentdetails(); void showstudentdetails(); //Definition of class Student void Student : : getstudent() cout<< \nenter Name of Student\n ; cin>>sname; cout<< \nenter Roll Number of Student\n ; cin>>roll_no; cout<< \nenter Course of Student\n ; cin>>course; void Student : : showstudent() cout<< \nname of the Student:\t <<SName; cout<< \nroll Number of the Student:\t <<Roll_No; cout<< \ncourse of the Student:\t <<course; V. ADVANTAGE OF DIAMOND EFFECT In case of diamond effect, we say that there are at least four classes where one class has two child classes and these two children classes are having one common child class. As we know, in inheritance any derived class can use members of the base class [7][8]. www.ijcsit.com 3108

Here the function getinternaldetail() can access the getstudent() and so on. Similarly the function getexternaldetail() can also access the getstudent() and so on. Let us see the modified version of getinternaldetail() and getexternaldetail() functions: void Internal : : getinternaldetails() getstudent(); //accessing the function of base class cout<< \nenter 5 name of subjects and marks obtained by student\n ; cout<< Enter Subject cout<< Enter Marks: ; cin>>imark[i]; void External : : getexternaldetails() getstudent(); //accessing the function of base class cout<< \nenter 5 name of subjects and marks obtained by student\n ; cout<< Enter Subject cout<< Enter Marks: ; cin>>emark[i]; In the above program, highlighted lines show that getinternaldetail() and getexternaldetail() functions using the same function getstudent(). It has no problem because both the classes Internal and External have the same parent class Student(). Similarly the member functions of class Result can also access to the members of class Internal and External because Result is child class of both the classes. Here are the modified versions of functions getresult() and showresult(). void Result : : getresult() getinternaldetails(); // accessing the member function of class // Internal getexternaldetails(); // accessing the member function of class //External cout<< \nenter 5 name of subjects \n ; cout<< Enter Subject Tot_Mark[i] = Imark[i] + Emark[i]; void Result: : showresult() showinternaldetails(); // accessing the member function of class // Internal showexternaldetails(); // accessing the member function of class //External cout<< \t\t <<Tot_Mark[i]; There are again some highlighted lines which are showing that functions getresult() and showresult() are accessing the functions getinternal(), getexternal() and showinternal(), showexternal(), because the Result class is derived from both the base classes. VI. PROBLEM ASSOCIATED WITH DIAMOND EFFECT Before starting discussion about the problems associated with diamond effect concept let us try to restructure the Fig. 8 as shown in Fig. 9. Internal: getinternaldetails() showinternaldetails() Student: getstudent() showstudent() Result: getresult() showresult() External: getexternaldetails() showexternaldetails() Fig. 9 Diamond Effect with class and their member functions. As it is shown in Fig. 9, we can say that the class Student is grandparent of Result class hence the Result class can directly access the members of Student class as: void Result : : getresult() //getinternaldetails(); //getexternaldetails(); getstudent(); //Direct access to member of grandparent class(error) www.ijcsit.com 3109

cout<< \nenter 5 name of subjects \n ; cout<< Enter Subject Tot_Mark[i] = Imark[i] + Emark[i]; void Result: : showresult() //showinternaldetails(); //showexternaldetails(); showstudent();//direct access to member of grandparent class(error) cout<< \t\t <<Tot_Mark[i]; Error: there are two ways for Result class to access the members of Student class as: Result->Internal->Student and Result->External->Student Then the question is by which way the members of Student class will be accessed? This situation is called ambiguity problem in the world of object oriented programming. VII. SOLUTION OF PROBLEM ASSOCIATED WITH DIAMOND EFFECT Here the virtual base class comes in the life. We make the class Student as virtual base class for the class Result. For this we use virtual keyword when create derived class [2][6][7]. So the following syntax is used to make virtual base class as: Internal: getinternaldetails() showinternaldetails() Student: getstudent() showstudent() V i r t u a l P a t h Result: getresult() showresult() Fig. 10 Virtual Base Class External: getexternaldetails() showexternaldetails() class Student class Internal : virtual public Student class External : virtual public Student class Result : public Internal, public External By using above syntax the classes Internal and External will create a virtual path for the next derived class Result to first base class Student. Fig. 10 shows diagrammatic form of virtual base class. And the above given program with error syntax is rectified as : void Result : : getresult() //getinternaldetails(); //getexternaldetails(); getstudent(); //Direct access to member of grandparent class(no Error) cout<< \nenter 5 name of subjects \n ; cout<< Enter Subject Tot_Mark[i] = Imark[i] + Emark[i]; void Result: : showresult() //showinternaldetails(); //showexternaldetails(); showstudent();//direct access to member of grandparent class(no Error) cout<< \t\t <<Tot_Mark[i]; So the concept of virtual base class is used to solve the problem generated in situation of diamond effect. www.ijcsit.com 3110

REFERENCES [1] Donna Malayeri, Jonathan Aldrich, CZ: Multiple Inheritance without Diamond Effect, in OOPSlA09. [2] What is a virtual base class?, CareerRide.com. [3] G. Singh. Single Versus Multiple Inheritance in Object Oriented Programming. SIGPLAN OOPS., (5): 34-43, 1994. [4] A. Shalit. The Dylan Reference Manual: The definitive Guide of the new object-oriented dynamic Language. Addison Wesley, 1997. [5] Mathew Cochran. Coding Better: Using Classes V Interfaces, January 18, 2009. [6] V. Krishnapriya, Dr K Ramar. Exploring the difference between object oriented class inheritance and Interface using coupling Measures., 2010, International Conference on Advances in Computer Engineering. [7] E. Balaguruswami. Object Oriented Programming with C++, 6 th Edition, Tata McGraw-Hill Education. [8] Herbert Schildt, C++: The Complete Reference, 4 th Edition, 2002. www.ijcsit.com 3111