Inheritance
Click here to load reader
-
Upload
yugandhar-vadlamudi -
Category
Education
-
view
97 -
download
0
Transcript of Inheritance
InheritanceTypes of InheritanceSuper Keyword
Method Overriding
Inheritance• Inheritance is a mechanism wherein a new class is derived from an
existing class. In Java, classes may inherit or acquire the properties and methods of other classes.
• A class derived from another class is called a subclass• Where as the class from which a subclass is derived is called a super class• A subclass can have only one superclass• whereas a superclass may have one or more subclasses.
Use of Inheritance• For Method Overriding (so runtime polymorphism can be achieved).• For Code Reusability.
Types of inheritance supported in java
Single level inheritanceMulti level inheritanceHierarchal inheritance
Single level inheritance• When a class extends another one class only then we call it a single inheritance.
class Class_A{ void method_A() { System.out.println("the Class_A method_A a called"); }}class Class_B extendsClass_A{ void method_B() { System.out.println("the Class_b method_B a called"); }}public class inheritance{ public static void main(String[] y) { // with object of b class a class methods can be accessed
Class_B b=new Class_B(); b.method_B(); b.method_A(); }}
Multilevel inheritance• Multilevel inheritance refers to a mechanism in OO technology where one class can
inherit from a derived class, thereby making this derived class the base class for the new class
class Class_A{ void method_A() { System.out.println("the Class_A method_A a called"); }}class Class_B extends Class_A{ void method_B() { System.out.println("the Class_b method_B a called"); }}class Class_C extends Class_B{ void method_C() { System.out.println("the Class_C method_C a called"); }}public class inheritance{ public static void main(String[] y) { /* * with object of b class a class methods can be accessed */ Class_C C=new Class_C(); C.method_C(); C.method_B(); C.method_A(); }}
Hierarchical Inheritance• In such kind of inheritance one class is inherited by many sub classes.class Class_A{ void method_A() { System.out.println("the Class_A method_A a called"); }}class Class_B extends Class_A{ void method_B() { System.out.println("the Class_b method_B a called"); }}class Class_C extends Class_A{ void method_C() { System.out.println("the Class_C method_C a called"); }}public class inheritance{ public static void main(String[] y) { /* * with object of b class a class methods can be accessed */ Class_C C=new Class_C(); C.method_A(); C.method_C(); Class_B B=new Class_B(); B.method_A(); B.method_B(); }}
Super Keyword• super is used to serve following two objectives:
1) Accessing super class members (Methods and Variables).
2) Calling super class Constructor./** accessing super class methods and variables */class Class_A{ int int_a=100; void demo() { System.out.println("hello world"); } }class Class_B extends Class_A{ int int_a=200; void demo_1() { System.out.println("the value of int_a is"+super.int_a); super.demo(); }}public class inheritance{ public static void main(String[] y) { Class_B b=new Class_B(); b.demo_1(); }}
/** accessing super class Constructor */class Class_A{ int int_a; Class_A() { int_a=100; System.out.println("the int_a value is "+int_a); } }class Class_B extends Class_A{ Class_B() { super(); }//calls super class constructor}
public class inheritance{ public static void main(String[] y) { Class_B b=new Class_B(); }}
Method Overriding• Child class has the same method as of base class. In such cases child class
overrides the parent class method without even touching the source code of the base class. This feature is known as method overriding.
/* * Method overriding */class Class_A{ void demo() { System.out.println("the class A method called"); }}class Class_B extends Class_A{ void demo() { System.out.println("the class B method called"); }}public class inheritance{ public static void main(String[] y) { Class_B b=new Class_B(); b.demo(); }}