






















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
class LibraryClass { string name; public virtual void Delete() { name = null; } public void CleanUp() { Delete(); ... } } • In Java the call myObj.CleanUp() ...
Typology: Exams
1 / 62
This page cannot be seen from the preview
Don't miss anything!























































Inheritance
Asignments and Type Checks
class A {.
class B : A {...} class C: B {...}
A a = new A();
// static type of
a
: the type specified in the declaration (here
// dynamic type of
a
: the type of the object in
a
(here also
a = new B();
// dynamic type of
a
is
a = new C();
// dynamic type of
a
is
B b = a;
// forbidden; compilation error
a = new C(); if (a is C) ...
// true, if dynamic type of
a
is
or a subclass; otherwise false
if (a is B) ...
// true
if (a is A) ...
// true, but warning because it makes no sense
a = null; if (a is C) ...
// false: if
a
== null,
a is T
always returns false
Checked Type Casts Cast
A a = new C(); B b = (B) a;
// if (
a^
is
) stat.type(
a ) is
in this expression; else exception
C c = (C) a; a = null; c = (C) a;
// ok
null can be casted to any reference type
A a = new C(); B b = a as B;
// if (a is B) b = (B)a; else b = null;
C c = a as C; a = null; c = a as C;
// c == null
Dynamic Binding (simplified)
class A {
public virtual void WhoAreYou() { Console.WriteLine("I am an A"); } } class B : A {
public override void WhoAreYou() { Console.WriteLine("I am a B"); } }
A a = new B();a.WhoAreYou();
// "I am a B"
void Use (A x) {
x.WhoAreYou(); } Use(new A());
// "I am an A"
Use(new B());
// "I am a B"
Hiding Members can be declared as new in a subclass.They
class A {
public int x;public void F() {...}public virtual void G() {...} } class B : A {
public new int x;public new void F() {...}public new void G() {...} } B b = new B();b.x = ...;
// accesses B.x
b.F(); ... b.G();
// calls B.F and B.G
((A)b).x = ...;
// accesses A.x!
((A)b).F(); ... ((A)b).G();
// calls A.F and A.G!
Fragile Base Class Problem Initial situation
class LibraryClass {
public void CleanUp() { ... } } class MyClass : LibraryClass {
public void Delete() { ...
erase the hard disk
class LibraryClass {
string name;public virtual void Delete() { name = null; }public void CleanUp() { Delete(); ... } }
-^
-^
-^
Constructors and Inheritance
class A {
} class B : A {
public B(int x) {...} }
class A {
public A() {...} } class B : A {
public B(int x) {...} }
class A {
public A(int x) {...} } class B : A {
public B(int x) {...} }
class A {
public A(int x) {...} } class B : A {
public B(int x) : base(x) {...} }
B b = new B(3);
B b = new B(3);
B b = new B(3);
B b = new B(3);
default constr. A()
-^
B(int x)
B(int x)
Error! -^
no explicit call of the A() constructor
-^
default constr. A() does not exist
A(int x)
-^
B(int x)
Abstract Classes Example
abstract class Stream {
public abstract void Write(char ch);public void WriteString(string s) { foreach (char ch in s) Write(s); } } class File : Stream {
public override void Write(char ch) {...
write ch to disk
-^
-^
-^
Abstract Properties and Indexers Example
abstract class Sequence {
public abstract void Add(object x);
// method
public abstract string Name { get; }
// property
public abstract object this [int i] { get; set; }
// indexer
} class List : Sequence {
public override void Add(object x) {...}public override string Name { get {...} }public override object this [int i] { get {...} set {...} } }
Interfaces
Syntax
public interface IList : ICollection, IEnumerable {
int Add (object value);
// methods
bool Contains (object value);...bool IsReadOnly { get; }
// property
...object this [int index] { get; set; }
// indexer
-^
-^
-^
-^
-^
-^
Working with Interfaces Assignments:
MyClass c = new MyClass(); IList list = c;
list.Add("Tom");
// dynamic binding => MyClass.Add
if (list is MyClass) ...
// true
c = list as MyClass; c = (MyClass) list; ISerializable ser = (ISerializable) list;
MyClass
MyBaseClass
<
IList
<
Example interface ISimpleReader {
int Read(); } interface IReader : ISimpleReader {
void Open(string name); void Close(); } class Terminal : ISimpleReader {
public int Read() { ... } } class File : IReader {
public int Read() { ... } public void Open(string name) { ... } public void Close() { ... } } ISimpleReader sr = null;
// null can be assigned to any interface variable
sr = new Terminal(); sr = new File(); IReader r = new File(); sr = r;
Terminal Read
<
File ReadOpenClose
<