[go: up one dir, main page]

0% found this document useful (0 votes)
21 views19 pages

05 Inheritance

Uploaded by

imranmrayan04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views19 pages

05 Inheritance

Uploaded by

imranmrayan04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Inheritance

Objectives

• Describe inheritance
– syntax
– meaning
– access
– construction

2
Inheritance basics

• Inheritance allows new class to be created from existing


class
– derived class created from base class
• Derived class
– inherits fields and methods from base
– can add new fields
– can add new methods

3
Motivation: repeated code

• Related classes may contain repeated code


class Student
{
string name;
int age;
public void Birthday() { age++; }

int id;
}
repeated code
class Employee
{
string name;
int age;
public void Birthday() { age++; }

double salary;
}

4
Inheritance syntax

• Inheritance syntax is : followed by base class name


– common code placed in base class
– derived classes add members as needed
class Person
{
string name;
common code int age;
public void Birthday() { age++; }
}

class Student : Person


{
Student member int id;
}

class Employee : Person


{
Employee member double salary;
}

5
Memory allocation

• Creating derived object allocates memory for all fields


– base fields and derived fields

class Person
{
base string name;
fields int age;
... Student s = new Student();
}

class Student : Person name


{ s age
derived int id; id
field ...
}

6
Use of base services

• Base services available for use on all derived objects

class Person
{
base public void Birthday()
method {
age++;
}
Student s = new Student();
...
} s.Birthday();

class Student : Person


{
...
call base method
}
on derived object

7
Inheritance hierarchy

• Can derive from derived class to form inheritance hierarchy


– inherit members from direct and indirect base classes
class Person
{
string name;
int age;
...
}

class Student : Person Graduate g = new Graduate();


{
int id;
... name
} age
g
id
third advisor
class Graduate : Student
level {
string advisor;
...
}

8
Single inheritance only

• Only single inheritance supported


– derived class can have only one base
– multiple inheritance not allowed
– restriction imposed by CLR

class Student : Person class Employee : Person


{ {
... ...
} }

error, only one


class Graduate : Student, Employee
base allowed
{
...
}

9
Meaning of inheritance

• Inheritance expresses generalization/specialization


– fundamental to object-oriented programming
– often called the is-a relationship
– derived is-a base

Person

Student Employee

Undergraduate Graduate Staff Faculty

10
Protected access

• Derived class can access protected members of base class


– but other classes can not

class Person
{
derived classes protected string name;
granted access protected int age;
...
}

11
Hiding inherited member

• Derived class can hide inherited member


– label with new to indicate hiding desired
– compile time warning if not labeled new
– helps avoid unintended hiding as designs change over time
– should rarely be needed for most designs
class Person
{
government assigns
int id;
id to each person
set for person id public void SetId(int id) { this.id = id; }
}

class Student : Person


{
university assigns
int id;
id to each student
set for student id public new void SetId(int id) { this.id = id; }
}

12
Calling base method

• Derived method can call base method with keyword base


– often called chaining
class Person
{
public void Print()
{
Console.WriteLine(name);
Console.WriteLine(age);
}
...
}

class Student : Person


{
public new void Print()
{
call base base.Print();
class version Console.WriteLine(id);
}
...
}

13
Construction and inheritance

• Creating a derived class object should initialize entire object


– first base class part
– then derived class part

construct
Student s = new Student("Ann", 23, 12345);
Student

name Person part


s age
id Student part

14
Invoking base constructor

• Derived class constructor should call base class constructor


– use :base(...) syntax before constructor body
– base constructor run before body of derived constructor
– can only call one of :base(...) and :this(...)
class Person
{
public Person(string name, int age) { ... }
...
}

class Student : Person


{
int id;

public Student(string name, int age, int id)


call Person
:base(name, age)
constructor {
this.id = id;
}
}

15
Selecting base constructor

• Derived call to base constructor matched on argument list


– derived selects desired version by choosing arguments to pass
class Person
{
public Person() { ... }
multiple public Person(string name) { ... }
constructors public Person(string name, int age) { ... }
...
}

class Student : Person


{
int id;

public Student(string name, int age, int id)


Person(string,int) :base(name, age)
constructor called {
this.id = id;
}
}

16
Automatic invocation of base constructor

• Compiler automatically calls no-argument base constructor


– if derived constructor does not explicitly call base constructor
– ensures construction of entire object
class Person
{
public Person() { ... }
...
}

class Student : Person


{
int id;

automatic call public Student(int id)


to Person() {
this.id = id;
}
}

17
Base without default constructor

• Base class may not provide default constructor


– derived must explicitly call available constructor

class Person
{
assume Person has public Person(string name, int age) { ... }
no default constructor ...
}

class Student : Person


{
int id;
error: tries to call default public Student(int id)
Person constructor {
this.id = id;
}
}

18
Summary

• Inheritance used to model the is-a relationship


– derived class inherits members of base class
– can add members
– can hide inherited methods using new keyword
• Chaining
– can call base class constructor from derived class constructor
– can call base method from derived class method

19

You might also like