2.7 Inheritance Types of inheritance

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming Pillars Introduction to Object- Oriented Programming.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Introduction to Object-Oriented Programming Lesson 2.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Topics Instance variables, set and get methods Encapsulation
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
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.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
C# for C++ Programmers 1.
Classes (Part 1) Lecture 3
Classes C++ representation of an object
Static data members Constructors and Destructors
Inheritance-Basics.
Final and Abstract Classes
Inheritance and Polymorphism
Module 5: Common Type System
Table of Contents Class Objects.
Inheritance AKEEL AHMED.
OOP’S Concepts in C#.Net
6 Delegate and Lambda Expressions
Conditional Statements
Abstract Classes AKEEL AHMED.
Interfaces.
Object-Oriented Programming in PHP
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Class.
Classes C++ representation of an object
Final and Abstract Classes
CSG2H3 Object Oriented Programming
Computer Science II for Majors
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

2.7 Inheritance 2.7.1 Types of inheritance -> Creating a new class from existing class is called as inheritance Single Inheritance Hierarchical Inheritance Multi Level Inheritance Hybrid Inheritance Multiple Inheritance

1.Single Inheritance when a single derived class is created from a single base class then the inheritance is called as single inheritance.

2.Hierarchical Inheritance when more than one derived class are created from a single base class, then that inheritance is called as hierarchical inheritance.

3.Multi Level Inheritance when a derived class is created from another derived class, then that inheritance is called as multi level inheritance

4.Hybrid Inheritance Any combination of single, hierarchical and multi level inheritances is called as hybrid inheritance

Multiple Inheritance when a derived class is created from more than one base class then that inheritance is called as multiple inheritance. But multiple inheritance is not supported by .net using classes and can be done using interfaces

Calling base function Definition: A function is a block of code that performs a calculation and returns a value

Abstract class & function Classes can be declared as abstract by putting the keyword abstract before the class. EX. public abstract class A { public abstract void DoWork(int i); }

Example of abstract class using System; class Hello { public abstract class abs { public abstract void speak(); } public class abc : abs { public override void speak() { Console.WriteLine("Hello!"); Console.Read(); static void Main() { abc hello = new abc(); hello.speak();

Sealed Classes When you want to restrict your classes from being inherited by others you can create the class as sealed class.  ->To create a class as sealed class, create the class using the keyword sealed. -> sealed class cannot be used as a base class. [Access Modifier] sealed class classname { }

Sealed class example using System; sealed class SealedClass{ public int x; public int y; } class MainClass { static void Main() { SealedClass sc = new SealedClass(); sc.x = 110; sc.y = 150; Console.WriteLine("x = {0} \ny = {1}", sc.x, sc.y); Console.Read();

Constructor A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor. Constructors are mainly used to initialize private fields of the class while creating an instance for the class. When you are not creating a constructor in the class, then compiler will automatically create a default constructor in the class that initializes all numeric fields in the class to zero and all string and object fields to null.  To create a constructor, create a method in the class with same name as class name and has the following syntax. [Access Modifier] ClassName([Parameters]) { }

Example of constructore using System; class ProgramCall { int i, j; //default contructor public ProgramCall() { i = 45; j = 76; } public static void Main() { //When an object is created , contructor is called ProgramCall obj = new ProgramCall(); Console.WriteLine(obj.i); Console.WriteLine(obj.j); Console.Read();

Modifiers The access modifier is the key of Object Oriented Programming, to promote encapsulation, a type in C# should limit its accessibility and this accessibility limit are specified using Access modifiers.  public  The public keyword is an access modifier for types and type members. Public access is the most permissive access level.   There are no restrictions on accessing public members.  Accessibility:  Can be accessed by objects of the class Can be accessed by derived classes

Private   Private access is the least permissive access level.  Private members are accessible only within the body of the class or the struct in which they are declared.  Accessibility:  Cannot be accessed by object Cannot be accessed by derived classes protected   A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.   A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.  

Accessibility:  Cannot be accessed by object By derived classes

Overview Abstraction Definition : Abstraction is "Collection of data" and Encapsulation is grouping of data in appropriate access specified". Encapsulation is data wrapping for the propose of simplifying system design and implementation.

Interface Interface like classes define set of properties, methods and events. But unlike classes interface don't provide any implementation. They implemented by classes from different separate entities.

we create a Test instance and store it in an object reference. Program that uses interface [C#] using System; interface iface{ void Read(); } class Test : iface { public void Read() { Console.WriteLine("Read interface "); Console.Read(); } } class Program { static void Main() { iface iobj = new Test(); // Create instance. iobj.Read(); // Call method on interface.

KEY: When a class implements an interface, it can be used through a reference to that interface.

Derived interface Interfaces are some sort of Class, similar to the Abstract Classes (with difference Abstract Classes can implement its methods-functions, Interfaces can not.). Idea behind an Interface is to create a Class that can tell to inherited Class to implement certain methods or functions. It is some sort of contract between the Interface and inherited Class.