SDSU CS 683 Emerging Technologies
Spring Semester, 2003
C# Inheritance
Previous    Lecture Notes Index    Next    
© 2003, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 17-Apr-03

Contents of Doc 22, C# Inheritance


References

C# Language Specification, http://download.microsoft.com/download/0/a/c/0acb3585-3f3f-4169-ad61-efc9f0176788/CSharp.zip

Programming C#, Jesse Liberty, O’Reilly, Chapter 5


Doc 22, C# Inheritance Slide # 2
Constants Verses static readonly

Contants are known at compile time
Readonly vaules are not know until runtime

using System;
namespace Program1
{
   public class Utils
   {
      public static readonly int X = 1;
   }
}
namespace Program2
{
   class Test
   {
      static void Main() {
         Console.WriteLine(Program1.Utils.X);
      }
   }
}



Doc 22, C# Inheritance Slide # 3

Inheritance


Single Inheritance

public class A
        {
        int value = 0;
        public A(int value)
                {
                this.value = value;
                }
        }
class B : A
        { 
        int x;
        public B(int first, int second) : base(first)
                {
                x = second;
                }
        }

Doc 22, C# Inheritance Slide # 4
Class Access Modifiers


The class modifier limits the access of its members

class B : A
        { 
        int x;
        public B(int first, int second) : base(first)
                {
                x = second;
                }
        }


Doc 22, C# Inheritance Slide # 5

Hiding & New


class Base
{
   public void F() {}
}
class Derived: Base
{
   public void F() {}      // Warning, hiding an inherited name
}



class Base
{
   public void F() {}
}
class Derived: Base
{
   new public void F() {}
}

Doc 22, C# Inheritance Slide # 6
Some Fun
class Base
{
   public static void F() {}
}
class Derived: Base
{
   new private static void F() {}   // Hides Base.F in Derived only
}
class MoreDerived: Derived
{
   static void G() { F(); }         // Invokes Base.F
}


class Base
{
   public static void F() {}
}
class Derived: Base
{
   new public static void F() {}
}
class MoreDerived: Derived
{
   static void G() { F(); }         // Invokes Derived.F
}

Doc 22, C# Inheritance Slide # 7
What Gets Printed?

using System;

class Base
{
        public void Foo()        
                {
                Console.WriteLine("Base");
                }
}
class Derived: Base
{
        public void Foo()           
                {
                Console.WriteLine("Derived");
                }
}

class Tester
{
        public static void Main()
        {
                Base top = new Derived();
                top.Foo();
         }
}

Doc 22, C# Inheritance Slide # 8

Polymorphism


Base method must be declared virtual
Derived method must be declared override

class Base
{
        public virtual void Foo()
                {
                Console.WriteLine("Base");
                }
}
class Derived: Base
{
        public override void Foo()  
                {
                Console.WriteLine("Derived");
                }
}


class Tester
{
        public static void Main()
        {
                Base top = new Derived();
                top.Foo();
        }
}


Doc 22, C# Inheritance Slide # 9
The Rules

A method named N is invoked with an argument list A
On an instance with a compile-time type C & a run-time type R

R is either C or a class derived from C

The invocation is processed as follows:





The most derived implementation of a virtual method M with respect to a class R is determined as follows:





Doc 22, C# Inheritance Slide # 10
Example
using System;
class A
{
   public void F() { Console.WriteLine("A.F"); }
   public virtual void G() { Console.WriteLine("A.G"); }
} 
class B: A
{
   new public void F() { Console.WriteLine("B.F"); }
   public override void G() { Console.WriteLine("B.G"); }
}
class Test
{
   static void Main() {
      B b = new B();
      A a = b;
      a.F();
      b.F();
      a.G();
      b.G();
   }
}
Output
A.F
B.F
B.G
B.G


Doc 22, C# Inheritance Slide # 11
What Happens Here?

using System;
class A
{
        public virtual void F() {Console.WriteLine("A");}
}
class B: A
{
    // Get a compile warning, hiding inherited F()
        public virtual void F() {Console.WriteLine("B");}    
}
class C : B
{
        public override void F() {Console.WriteLine("C");}
}
class Tester
        {
        public static void Main()
                {
                A top = new C();
                top.F();
                B middle = new C();
                middle.F();
                }
        }


Doc 22, C# Inheritance Slide # 12

Override Rules


The method overridden by an override declaration is known as the overridden base method.

For an override method M declared in a class C, the overridden base method is determined by examining each base class of C, starting with the direct base class of C and continuing with each successive direct base class, until an accessible method with the same signature as M is located.

For the purposes of locating the overridden base method, a method is considered accessible if it is public, if it is protected, if it is protected internal, or if it is internal and declared in the same program as C.

A compile-time error occurs unless all of the following are true for an override declaration:



Doc 22, C# Inheritance Slide # 13
Calling Base Method

using System;
public class A  
{
        virtual public void F()   
        {
                Console.WriteLine("A");
        }
}
class B: A
{
        public override void F()   
        { 
                base.F();
                Console.WriteLine("B");
        }
}


Doc 22, C# Inheritance Slide # 14

Sealed


A sealed method overrides an inherited virtual method with the same signature

using System;
class A
{
   public virtual void F() {
      Console.WriteLine("A.F");
   }
   public virtual void G() {
      Console.WriteLine("A.G");
   }
}
class B: A
{
   sealed override public void F() {
      Console.WriteLine("B.F");
   } 
   override public void G() {
      Console.WriteLine("B.G");
   } 
}
class C: B
{
   override public void G() {
      Console.WriteLine("C.G");
   }
}

C cannot override F()

Doc 22, C# Inheritance Slide # 15
What Happens Here

using System;
class A
{
        public virtual void F() {
                Console.WriteLine("A.F");
        }
        public virtual void G() {
                Console.WriteLine("A.G");
        }
}
class B: A
{
        sealed override public void F() {
                Console.WriteLine("B.F");
        }
        override public void G() {
                Console.WriteLine("B.G");
        }
}
class C: B
{
        override public void G() {
                Console.WriteLine("C.G");
        }
        new public void F() {
                Console.WriteLine("C.F");
        }
}

Doc 22, C# Inheritance Slide # 16
Sealed Classes

A sealed class cannot have any derived classes

A sealed class cannot have any virtual methods

using System;
sealed class A
{
        public void F() {
                Console.WriteLine("A.F");
        }
}
class B: A      //Compile Error
{
}

Doc 22, C# Inheritance Slide # 17

Abstract Classes


An abstract method is declared with the modifier abstract

An abstract method

A class that contains an abstract method must be declared abstract

An abstract class

Does not have to have abstract methods

Cannot be instantiated directly


Doc 22, C# Inheritance Slide # 18
Example

public abstract class Shape
{
   public abstract void Paint(Graphics g, Rectangle r);
}
public class Ellipse: Shape
{
   public override void Paint(Graphics g, Rectangle r) {
      g.DrawEllipse(r);
   }
}
public class Box: Shape
{
   public override void Paint(Graphics g, Rectangle r) {
      g.DrawRect(r);
   }
}


Doc 22, C# Inheritance Slide # 19
Abstract Classes can have constructors

abstract class A
{
        int cat;
        abstract public void F(); 
        public A(int value)  
        {
                cat = value;
        }
}

Doc 22, C# Inheritance Slide # 20
Cannot call a base abstract method

abstract class A
{
   public abstract void F();
}
class B: A
{
   public override void F() {
      base.F();      // Error, base.F is abstract
   }
}



Doc 22, C# Inheritance Slide # 21
Overriding with Abstract

Class B forces class C to implement F()

class A
{
   public virtual void F() {
      Console.WriteLine("A.F");
   }
}
abstract class B: A
{
   public abstract override void F();
}
class C: B
{
   public override void F() {
      Console.WriteLine("C.F");
   }
}


Doc 22, C# Inheritance Slide # 22

Protected access for instance members

When a protected instance member is accessed outside the program text of the class in which it is declared, the access is required to take place through an instance of the derived class type in which the access occurs.

public class A
{
   protected int x;
   static void F(A a, B b) {
      a.x = 1;      // Ok
      b.x = 1;      // Ok
   }
}
public class B: A
{
   static void F(A a, B b) {
      a.x = 1;      // Error, must access through instance of B
      b.x = 1;      // Ok
   }
}


Doc 22, C# Inheritance Slide # 23

Accessibility constraints

The following accessibility constraints exist:


Doc 22, C# Inheritance Slide # 24
Examples


   class A {}
   public class B: A {} //Compile Error


   class A {}
   public class B
   {
      A F() {}
      internal A G() {}
      public A H() {}      //Compile Error
   }


   class A {}
   public class B
   {
           public A sam; 
   }


Copyright ©, All rights reserved.
2003 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 17-Apr-03    Next