Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Inheritance
1.
2. We will cover
Inheritance
Advantages of inheritance
Categories of inheritance
Single inheritance
Multiple inheritance
Multilevel inheritance
Types of inheritance
Public inheritance
Private inheritance
Protected inheritance
3. Inheritance
A programming technique that is used to reuse an existing
class to build a new class
Terminologies
Base/parent/super class
That is reused to create a new class
Derived/child/parent sub class
That inherits the functions and properties of an existing class
Example
Vehicle
Bus Truck Motorcycle
5. Difference b/w different access specifiers
Access
Specifier
Accessible
from own
class
Accessible
from derived
class
Accessible
from objects
outside class
Public Yes Yes Yes
Protected Yes Yes No
Private Yes No No
7. Syntax
Single inheritance
class sub_class: specifier parent_class
{
body
}
Multiple inheritance
class sub_class: specifier parent_class1 , specifier parent_class2
{
body
}
Multilevel inheritance
class sub_class: specifier grandparent_class
{
body
}
class sub_class: specifier parent_class
{
body
}
8. Example single inheritance
class Person
{
protected:
int id;
Char name[50], address[100];
public:
Person()
{
id=0;
name[0]=‘0’;
address[0]= ‘0’;
}
void getinfo()
{
cout<<“ Enter ur id, name. address”;
cin>>id;
gets(name);
gets(address);
}
Void showinfo()
{
cout<<“ your personal info is”;
cout<<“ id is”<<id<<endl;
cout<<“ name is”<<name<<endl;
cout<<“ address is<<address<<endl;
}
};
9. Continued…
class Student: public Person
{
private:
int rno, marks;
public:
Student()
{
Person:: Person();
rno=marks=0;
}
void getedu()
{
cout<<“enter R# & marks”;
cin>>rno>>marks;
}
void showedu()
{
cout<<“ ur education info is”;
cout<<“ Roll no is”<<rmo<<endl;
cout<<“ Marks are”<<marks<<endl;
}
};
void main(void)
{
Student s;
s.getinfo();
s.getedu();
s.showinfo();
s.showedu();
getch();
}
10. Multilevel inheritance ( Example)
class A
{
private:
int a;
public:
void in()
{
cout<<“ Enter value for a”;
cin>>a;
}
void out()
{
cout<<“ value of a is”<<a;
}
};
class B : public A
{
private:
int b;
public:
void in()
{
A::in();
cout<<“ Enter value for b”;
cin>>b;
}
void out()
{
A::out();
cout<<“ value of b is”<<b;
}
};
11. Continued…
class C : public B
{
private:
int c;
public:
void in()
{
B::in();
cout<<“ Enter value for c”;
cin>>c;
}
void out()
{
B::out();
cout<<“ value of c is”<<c;
}
};
void main(void)
{
C obj;
obj.in();
obj.out();
getch();
}
12. Multiple inheritance ( Example)
class A
{
private:
int a;
public:
void in()
{
cout<<“ Enter value for a”;
cin>>a;
}
void out()
{
cout<<“ value of a is”<<a;
}
};
class B
{
private:
int b;
public:
void input()
{
cout<<“ Enter value for b”;
cin>>b;
}
void output()
{
cout<<“ value of b is”<<b;
}
};
13. Continued…
class C : public A, public B
{
private:
int c;
public:
void get()
{
A::in();
B::input();
cout<<“ Enter value for c”;
cin>>c;
}
void show()
{
A::out();
B::output();
cout<<“ value of c is”<<c;
}
};
void main(void)
{
C obj;
obj.get();
obj.show();
getch();
}
14. Ambiguity in multiple inheritance
Ambiguity will be created in multiple inheritance
when the names of functions is similar in two or more
parent classes, so compiler can’t determine which
function to execute when the object of the derived
class attempts to execute such function
Removal of such ambiguity
By overloading
By use of scope resolution operator
15. Types of inheritance
Three types
Public inheritance
Private inheritance
Protected inheritance
16. Public inheritance
In which access level in both parent and child class is
same
Public members become public in child class
Private members become private in child class
Protected members become protected in child class
Syntax
class child_class : public Parent_class
{
Body
};
17. Example
class Parent
{
public:
int a;
Private:
int b;
protected :
int c;
};
class Child : public Parent
{
public:
void in()
{
cout<<“ enter values for a & b”;
cin>>a>>b;
}
void out()
{
cout<<“Values of a & b are”<<a<<b;
}
};
void main(void)
{
Child obj;
Obj.in();
Obj.show();
getch();
}
18. Protected inheritance
In which access level in both parent and child class is
restricted
Public members become public in child class
Private members become private in child class
Protected members become protected in child class
Syntax
class child_class : protected Parent_class
{
Body
};
19. Example
{
public:
int a;
Private:
int b;
protected :
int c;
};
class Child : protected Parent
{
public:
void in()
{
cout<<“ enter values for a & b”;
cin>>a>>b;
}
void out()
{
cout<<“Values of a & b are”<<a<<b;
}
};
void main(void)
{
Child obj;
Obj.in();
Obj.show();
getch();
}
20. Private
In which access level in both parent and child class is
restricted
Public members become private in child class
Private members become private in child class
Protected members become private in child class
Syntax
class child_class : protected Parent_class
{
Body
};
21. Example
{
public:
int a;
Private:
int b;
protected :
int c;
};
class Child : private Parent
{
public:
void in()
{
cout<<“ enter values for a & b”;
cin>>a>>b;
}
void out()
{
cout<<“Values of a & b are”<<a<<b;
}
};
void main(void)
{
Child obj;
Obj.in();
Obj.show();
getch();
}