Controlling Base-Class Creation

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object Oriented Programming with C# Session: August-December 2009 Subject: C# Programming.
C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
The Role of .NET Exception Handling
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Inheritance. Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object Inheritance represents the IS-A.
N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Chapter - 4 OOP with C# S. Nandagopalan.
Classes (Part 1) Lecture 3
Static data members Constructors and Destructors
Object-Oriented Programming: Classes and Objects
Review What is an object? What is a class?
2.7 Inheritance Types of inheritance
Inheritance and Polymorphism
Module 5: Common Type System
Inheritance & Polymorphism
Review: Two Programming Paradigms
11.1 The Concept of Abstraction
Week 4 Object-Oriented Programming (1): Inheritance
Object-Oriented Programming: Classes and Objects
Lecture 4-7 Classes and Objects
OOP’S Concepts in C#.Net
Java LESSON 7 Objects, Part 1
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Object Based Programming
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
Java Programming Language
Conditional Statements
MSIS 670 Object-Oriented Software Engineering
Abstract Classes AKEEL AHMED.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Packages and Interfaces
Built-In (a.k.a. Native) Types in C++
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Java – Inheritance.
How to organize and document your classes
Method of Classes Chapter 7, page 155 Lecture /4/6.
Overview of C++ Polymorphism
CIS 199 Final Review.
Class.
Submitted By : Veenu Saini Lecturer (IT)
Agenda Inheritance Polymorphism Type compatibility Homework.
Programming in C# CHAPTER 5 & 6
11.1 The Concept of Abstraction
Chapter 5 Classes.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Controlling Base-Class Creation Usually, the base class constructors are used to initialize the members of base class. When derived class has few more members and if we need to initialize, then we will write constructor for derived class. In such a situation, there is a repetition of code. For Eg:   public class Emp { protected string Name; protected int EmpID; public Emp(string n, int eid, float s) Name=n; EmpId=eid; } ` public class SalesMan:Emp { int number_of_sales;   public SalesMan(string n, int eid, int s) Name=n; //code repeated EmpId=eid; //code repeated number_of_sales=s; }

protected string Name; protected int EmpID; If there is more number of data fields in the base class, then in each of the derived class constructors, we need to repeat such assignment statements. To avoid this, C# provides an option to explicitly call base class constructors as shown below – public class Emp { protected string Name; protected int EmpID;   public Emp(string n, int eid) Name=n; EmpId=eid; } public class SalesMan:Emp int bonus; public SalesMan(string n, int eid, int b): base(n, eid) bonus=b; //other members are passed to base class to initialize class Test { public static void Main() Emp e1=new Emp(“Ram”, 25); SalesMan s1= new SalesMan(“Rohan”, 12, 10); ------------ }

Keeping Family Secrets: The protected Keyword The private members of a class can not be available to derived classes. The protected members of a class can not be accessed from outside, but still be available to derived classes.   Preventing Inheritance: Sealed Classes In some of the situations, we may don’t want our class to be inherited by any other class. For example –

Here, a class called Part-time SalesMan is derived from SalesMan class which in-turn is derived from Employee class as shown in the diagram. Now, we don’t want any more classes to be derived from Part-time SalesMan class. To prevent such inheritance, C# provides a keyword sealed. public sealed class Part_TimeSalesMan: SalesMan { //body of Part_TimeSalesMan class }   Now any attempt made to derive a class from Part_TimeSalesMan class will generate an error: public class Test: Part_TimeSalesMan //compile-time error!!! …………..

int CurrSpeed, MaxSpeed; string name; bool carIsDead=false; Programming for Containment/Delegation class Radio { public void TurnOn(bool on) if(on) Console.WriteLine(“Radio is on”); else Console.WriteLine(“Radio is off”); } class Car int CurrSpeed, MaxSpeed; string name; bool carIsDead=false;

public Car() { MaxSpeed=100;} public Car(string n, int m, int c) { name=n; MaxSpeed=m; CurrSpeed=c; } public void SpeedUp(int a) if(carIsDead) Console.WriteLine(“Out of order”); else CurrSpeed+=a;   Now, we have two classes viz. Radio and Car. But, we can not say, “Car is a Radio” or “Radio is a Car”. Rather, we can say, “Car has a Radio”. Now, the Car is called containing Class and Radio is called contained class.

Nested Type Definitions To make the contained class to work, we need to re-define Car class as – class Car { ……….. private Radio rd=new Radio(); } Nested Type Definitions class C1 { ……….. //members of outer class class C2 ………. //members of inner class In C#, it is possible to define a type (enum, class, interface, struct, delegate) directly within the scope of a class. In containment/delegation, the object of contained class is created within containing class. But, it is possible to create the objects wherever we wish.