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.
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 ratings0% found this document useful (0 votes)
32 views21 pages
Inheritance Constructor in Child Class
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.
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. { System.out.println("x=" + x); } } 5. Base ob=new Base(); class Child extends Base // child class { 6. ob.set(10); int y; 7. ob.get(); Child() { 8. Child Obc=new Child(); x=0; // data member of parent 9. Obc.set(30); y=0; } 10. Obc.get(); void set(int a, int b) { 11. Obc.set(20,40); x=a; y=b; } void get() { 12. Obc.get(); System.out.println("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(); System.out.println("Show in C obc=new C(); A");} } oba.showA(); class B extends A { //oba.showB(); void showB() { //oba.showC(); System.out.println("Show in obb.showA(); obb.showB(); B");} } //obb.showC(); class C extends B { obc.showA(); void showC() { obc.showB(); System.out.println("Show in obc.showC(); 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 System.out.println("Constructor in as[]) A");} } { class B extends A { B() { A oba=new A(); System.out.println("Constructor in B obb=new B(); B");} } C obc=new C(); class C extends B { D obd=new D(); C() { } System.out.println("Constructor in C");} } } class D extends C { D() { System.out.println("Constructor in D");} } How Augmented Constructor Functions Are class A Called? { 1. class demo A() { 2. { System.out.println("no argument constructor of 3. public static void main (String a"); } as[]) A(int y) { 4. { System.out.println("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); System.out.println("no argument constructor of B"); } B(int y) { super(y); System.out.println("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() { System.out.println("x=" + x); } } class Child extends Base // child class { Child obc=new Child(); int x; obc.set(50,60); Child() { obc.get(); x=0; // data member of parent /* super.x=0; } void set(int a, int x) { Obc.set(20,60); set(a) obc.get(); this.x=x; Base ob; } Ob=obc; void get() Ob.set(50); { Ob.get();*/ super.get(); System.out.println("x"+x); //Ob.set(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. System.out.println(“x=“ + x); 4. Void set(int a) { 4. } 5. x=a; 5. } 6. } 7. Void get() 6. class demo 8. { 7. { 9. System.out.println(“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. Ob.set(50); 17. x=0; // data member of parent 14. Ob.get(); 18. y=0; 15. //Ob.set(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 { System.out.println("Show in A"); } public static void main(String as[]) { void display() { A oba=new A(); System.out.println("display in A"); } } B obb=new B(); A r; class B extends A r=oba; { r.show(); void show() { r.display(); System.out.println("Show in B"); } void display() { r=obb; System.out.println("display in B"); } r.show(); void abc() r.display(); {System.out.println("abc in B");} r.abc(); } }} 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!!!!!!!!!!!!!!!!!!!!!