Programming in java
Topic-Single inheritance
By
M.Raj Pradeep(181617)
G.Sivaganesh(181618)
2nd
_ Maths[CA]
INHERITANCE
The mechanism of deriving a new class
from an old one is called INHERITANCE.
The old class is referred to as the base class
the new one is called the DERIVED CLASS (or) SUB CLASS
There are five types of inheritance are,
Single inheritance
Multiple inheritance
Hierarchical inheritance
Multilevel inheritance
Hybrid inheritance
SINGLE
INHERITANCE
The derived class inheritance some or all of
the traits from the base class .A class can also inherit
properties from more than one class or from more than one
level. A derived class with only one base class, is called single
inheritance.
Form of single inheritance:
A
B
#include<iostream.h>
#include<conio.h>
class B
{
int a; //private; not inheritable
public:
int b; //public; ready for inheritable
void get ab();
int get a(void);
void show a(void);
};
class D::public B //public derivation
{
int c;
public:
Void mul(void);
Void display(void);
};
void B::get ab(void)
{
A=5;
B=10;
}
int B::get a()
{
return a;
}
void B::show a()
{
cout<<”a=”<<a<<”n”;
}
void D::mul()
{
c=b*get a();
}
void D::display()
{
cout<<”a=”<<get a()<<”n”;
cout<<”b=”<<b<<”n”;
cout<<”c=”<<c<<”nn”;
}
int main()
{
D d;
d.get ab();
d.mul();
d.show a();
d.display();
d.b=20;
`d.mul();
d.display();
return 0;
}
OUTPUT:
a=5
a=5
b=10
c=50
a=5
b=20
c=100
The class D is a public derivation of the
base class B. Therefore D inherits all the public
members of B and retains their visibility. Thus a public
member of the base class B is also a public member of
the derived class D. The private member of B cannot be
inherited by D. The class D, in effect, will have more
members than what it contains at the time of
declaration as shown
The membership of the derived class D
is shown in chart. The public member of the base class
become private members of the derived class.
Therefore , the object of D can not have direct access to
the public member functions of B.
class D
Private
Section
C
Inherited
from B
b
get ab()
get a()
show a()
Public
Section
mul()
display()
The statements such as,
d.get ab(); //get ab() is private
d.get a(); //so also get a()
d.show a(); //and show a()
will not work. However, these functions can be used
inside mul() and display() like thew normal functions as
shown below:
void mul()
{
get ab();
c=b*get a();
}
void display()
{
show a(); //outputs value of a
cout<<”b=”<<b<<”n”;
cout<<”c=”<<c<<”nn”;
}
Suppose a base class and a derived class define a
functions of the same name. what will happen when a
derived class object invokes the functions?. In such
cases, the derived class functions supersedes the base
class definition. The base class functions, wil be called
only if the derived class does not redefine the function.
Inheritance

Inheritance

  • 1.
    Programming in java Topic-Singleinheritance By M.Raj Pradeep(181617) G.Sivaganesh(181618) 2nd _ Maths[CA]
  • 2.
    INHERITANCE The mechanism ofderiving a new class from an old one is called INHERITANCE. The old class is referred to as the base class the new one is called the DERIVED CLASS (or) SUB CLASS There are five types of inheritance are, Single inheritance Multiple inheritance Hierarchical inheritance Multilevel inheritance Hybrid inheritance
  • 3.
    SINGLE INHERITANCE The derived classinheritance some or all of the traits from the base class .A class can also inherit properties from more than one class or from more than one level. A derived class with only one base class, is called single inheritance.
  • 4.
    Form of singleinheritance: A B
  • 5.
    #include<iostream.h> #include<conio.h> class B { int a;//private; not inheritable public: int b; //public; ready for inheritable void get ab(); int get a(void); void show a(void); }; class D::public B //public derivation {
  • 6.
    int c; public: Void mul(void); Voiddisplay(void); }; void B::get ab(void) { A=5; B=10; } int B::get a() { return a; } void B::show a() {
  • 7.
    cout<<”a=”<<a<<”n”; } void D::mul() { c=b*get a(); } voidD::display() { cout<<”a=”<<get a()<<”n”; cout<<”b=”<<b<<”n”; cout<<”c=”<<c<<”nn”; } int main() { D d; d.get ab();
  • 8.
  • 9.
    b=20 c=100 The class Dis a public derivation of the base class B. Therefore D inherits all the public members of B and retains their visibility. Thus a public member of the base class B is also a public member of the derived class D. The private member of B cannot be inherited by D. The class D, in effect, will have more members than what it contains at the time of declaration as shown
  • 10.
    The membership ofthe derived class D is shown in chart. The public member of the base class become private members of the derived class. Therefore , the object of D can not have direct access to the public member functions of B. class D Private Section C Inherited from B b get ab() get a() show a() Public Section mul() display()
  • 11.
    The statements suchas, d.get ab(); //get ab() is private d.get a(); //so also get a() d.show a(); //and show a() will not work. However, these functions can be used inside mul() and display() like thew normal functions as shown below: void mul() { get ab(); c=b*get a(); } void display() { show a(); //outputs value of a cout<<”b=”<<b<<”n”; cout<<”c=”<<c<<”nn”;
  • 12.
    } Suppose a baseclass and a derived class define a functions of the same name. what will happen when a derived class object invokes the functions?. In such cases, the derived class functions supersedes the base class definition. The base class functions, wil be called only if the derived class does not redefine the function.