0% found this document useful (0 votes)
44 views21 pages

Java Inheritance Guide

Inheritance allows classes to establish a parent-child relationship where the child class inherits properties and behaviors of the parent class. The child class can override or extend the parent's methods and properties. When a child class object is instantiated, the parent class constructor is called first before the child constructor code executes.

Uploaded by

Pirzada Swati
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)
44 views21 pages

Java Inheritance Guide

Inheritance allows classes to establish a parent-child relationship where the child class inherits properties and behaviors of the parent class. The child class can override or extend the parent's methods and properties. When a child class object is instantiated, the parent class constructor is called first before the child constructor code executes.

Uploaded by

Pirzada Swati
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

Inheritance

Constructor In Child Class


Inheritance

 Principle of Object Oriented Programming


Encapsulation
Inheritance
Polymorphism
Inheritance

 Inheritance is the phenomena that links


class in parent child relationship.
 The parent class is also called
super/base/parent class
 The child class is also known as
sub/derived/child class.
Inheritance
 All the members of the parent class
become the member of the child class
 The members declared as private are not
accessible inside the child class.
 All other members i.e. data members and
member functions can be accessed from
the child class.
 extends key word is used to create parent
child hierarchy.
Inheritance

 The child class may have its own


members.
 The object of the child class have access
to all the members of the parent class
 The object of the parent class cannot
access any member of the child class.
Inheritance Example 1
 class Base //super class { 1. class demo
 int x; 2. {
 void set(int a) { 3. public static void main (String
 x=a; } as[])
 void get() { 4. {
 [Link]("x=" + x); } }

5. Base ob=new Base();
class Child extends Base // child
class { 6. [Link](10);
 int y; 7. [Link]();
 Child() { 8. Child Obc=new Child();
 x=0; // data member of parent 9. [Link](30);
 y=0; } 10. [Link]();
 void set(int a, int b) {
11. [Link](20,40);
 x=a; y=b; }
 void get() {
12. [Link]();
 [Link]("x=" + x 13. }
+"y="+y); } } 14. }
Inheritance

 Java does not support multiple inheritance


like C++.
 A base class can have multiple subclasses
at a time.
 Whereas a subclass can have only single
direct parent class at a time.
Multilevel Inheritance

 In java inheritance hierarchy can be


extended to multiple levels.
 Such phenomenon is called multilevel
inheritance.
 A child class in the multilevel hierarchy
can access all the members of the parent
classes at the upper level.
Multilevel Inheritance

 However, none of the parent class is


capable to access the members of it child
classes in downwards hierarchy.
 Similarly, an object of the child class at any
level can access all the members of the
parent class in the upwards hierarchy.
 Whereas, none of the object is capable to
access the members of the objects in the
downwards hierarchy.
Example
 class Demo{
 class A {  public static void main(String as[]){
 void showA() {  A oba=new A();
 B obb=new B();
 [Link]("Show in
 C obc=new C();
A");} }
 [Link]();
 class B extends A {  //[Link]();
 void showB() {  //[Link]();
 [Link]("Show in  [Link]();
 [Link]();
B");} }
 //[Link]();
 class C extends B {
 [Link]();
 void showC() {  [Link]();
 [Link]("Show in  [Link]();
C");} }  }
 }
Constructor in child class
 As child class inherits the members of the parent
class, therefore constructor function of the parent
class is also called when an object of the child
class is created.
 By default no argument constructor (if any) is
called.
 Super key word is used to call a specific
constructor function.
 Super (argument list) must be the first statement
in child class constructor.
Example
 class A {  class Demo{
 A() {  public static void main(String
 [Link]("Constructor in as[])
A");} }
 {
 class B extends A {
 B() {
 A oba=new A();
 [Link]("Constructor in  B obb=new B();
B");} }  C obc=new C();
 class C extends B {  D obd=new D();
 C() {
 }
 [Link]("Constructor in
C");} }
 }
 class D extends C {
 D() {
 [Link]("Constructor in
D");} }
How Augmented Constructor Functions
Are
class A
Called?
{ 1. class demo
A()
{ 2. {
[Link]("no argument constructor of 3. public static void main (String
a"); } as[])
A(int y)
{
4. {
[Link]("one argument constructor 5. //A oba=new A();
of a"); 6. B obb1=new B();
}}
class B extends A 7. B obb2=new B(20);
{ 8. }
B() 9. }
{
super(10);
[Link]("no argument constructor of
B");
}
B(int y)
{
super(y);
[Link]("one argument constructor
of B");
}}
Method Overriding

 Member functions in the parent and child


class having same name, same number of
arguments and same data types are called
overrided functions.
 The phenomenon is called function
overriding.
 The phenomenon in which member
functions in the parent and child class have
exactly same type signature.
Example
 class Base {  class demo
 int x;  {
 void set(int a) {
 public static void main (String as[])
 x=a; }

 {
void get() {
 [Link]("x=" + x); } }
 class Child extends Base // child class {  Child obc=new Child();
 int x;  [Link](50,60);
 Child() {  [Link]();
 x=0; // data member of parent
 /*
 super.x=0; }
 void set(int a, int x) {
 [Link](20,60);
 set(a)  [Link]();
 this.x=x;  Base ob;
 }  Ob=obc;
 void get()  [Link](50);
 {
 [Link]();*/
 [Link]();
 [Link]("x"+x);
 //[Link](20,60); invalid }}
 }}
Inheritance

 The reference of child class object can


be assigned to reference variable of the
parent class.
1. e.g. Base ob;
2. Child obc=new Child();
3. ob=obc;
Inheritance

Now ob can access those members of the


child class object which belongs to its own
class.
Ob cannot access the members of the
child class object even though it has
reference of the child class object
Inheritance Example 3
1. class Base //super class 1. Void get()
2. { 2. {
3. int x; 3. [Link](“x=“ + x);
4. Void set(int a) { 4. }
5. x=a; 5. }
6. }
7. Void get() 6. class demo
8. { 7. {
9. [Link](“x=“ + x); 8. public static void main (String as[])
10. } 9. {
11. }
12. Class Child extends Base // child class 10. Child obc=new Child();
13. { 11. Base ob;
14. Int y; 12. Ob=obc;
15. Child()
16. { 13. [Link](50);
17. x=0; // data member of parent 14. [Link]();
18. y=0; 15. //[Link](20,60); invalid
19. }
20. Void set(int a, int b) 16. }
21. { 17. }
22. x=a; y=b;
23. }
24. ………………..>
Example
 class A {
 void show() {  class demo {
 [Link]("Show in A"); }  public static void main(String as[]) {
 void display() {  A oba=new A();
 [Link]("display in A"); } }  B obb=new B();
 A r;
 class B extends A  r=oba;
 {  [Link]();
 void show() {  [Link]();
 [Link]("Show in B"); }
 void display() {  r=obb;
 [Link]("display in B"); }  [Link]();
 void abc()  [Link]();
 {[Link]("abc in B");}  [Link]();
 }  }}
Constructor in child class

 Child class may have its own constructor


 First the constructor of the child class
object is called.
 Before executing the child constructor the
parents constructor function gets called.
Thanks!!!!!!!!!!!!!!!!!!!!!

You might also like