1class Arithmetic {
2
3}
4class Adder extends Arithmetic {
5 public int add(int a, int b)
6 {
7 return a+b;
8 }
9}
10
1Inheritance in Java is a mechanism in which one object acquires
2all the properties and behaviors of a parent object.
3It is an important part of OOPs (Object Oriented programming system).
1// super class
2class Animal
3{
4 void eat()
5 {
6 System.out.println("Animal is eating.");
7 }
8}
9// subclass
10class Lion extends Animal
11{
12 public static void main(String[] args)
13 {
14 Lion obj = new Lion();
15 obj.eat();
16 }
17}
1// Multiple inheritance in java
2interface M
3{
4 public void helloWorld();
5}
6interface N
7{
8 public void helloWorld();
9}
10// implementing interfaces
11public class MultipleInheritanceExample implements M, N
12{
13 public void helloWorld()
14 {
15 System.out.println("helloWorld() method");
16 }
17 public static void main(String[] args)
18 {
19 MultipleInheritanceExample obj = new MultipleInheritanceExample();
20 obj.helloWorld();
21 }
22}
1// Multilevel inheritance in java
2class Animal
3{
4 void eating()
5 {
6 System.out.println("animal eating");
7 }
8}
9class Lion extends Animal
10{
11 void roar()
12 {
13 System.out.println("lion roaring");
14 }
15}
16public class Cub extends Lion
17{
18 void born()
19 {
20 System.out.println("cub drinking milk");
21 }
22 public static void main(String[] args)
23 {
24 Animal obj1 = new Animal();
25 obj1.eating();
26 Lion obj2 = new Lion();
27 obj2.eating();
28 obj2.roar();
29 Cub obj3 = new Cub();
30 obj3.eating();
31 obj3.roar();
32 obj3.born();
33 }
34}
1// single inheritance example
2import java.util.*;
3class Animal
4{
5 void eat()
6 {
7 System.out.println("Animal is eating.");
8 }
9}
10class Lion extends Animal
11{
12 void roar()
13 {
14 System.out.println("lion is roaring.");
15 }
16 public static void main(String[] args)
17 {
18 Lion obj = new Lion();
19 obj.eat();
20 obj.roar();
21 }
22}
1// Hybrid inheritance in java
2class A
3{
4 public void display()
5 {
6 System.out.println("class A display method");
7 }
8}
9interface B
10{
11 public void print();
12}
13interface C
14{
15 public void print();
16}
17public class HybridInheritanceDemo extends A implements B, C
18{
19 public void print()
20 {
21 System.out.println("implementing print method");
22 }
23 public void show()
24 {
25 System.out.println("show method of class HybridInheritanceDemo");
26 }
27 public static void main(String[] args)
28 {
29 HybridInheritanceDemo obj = new HybridInheritanceDemo();
30 obj.show();
31 obj.print();
32 }
33}
1// Hierarchical inheritance in java
2class Animal
3{
4 void eating()
5 {
6 System.out.println("animal eating");
7 }
8}
9class Lion extends Animal
10{
11 void roar()
12 {
13 System.out.println("lion roaring");
14 }
15}
16public class Dog extends Animal
17{
18 void bark()
19 {
20 System.out.println("dog barking");
21 }
22 public static void main(String[] args)
23 {
24 Animal obj1 = new Animal();
25 obj1.eating();
26 Lion obj2 = new Lion();
27 obj2.eating();
28 obj2.roar();
29 Dog obj3 = new Dog();
30 obj3.eating();
31 obj3.bark();
32 }
33}
1Builds relations between classes, main purpose:
2create a TEST BASE CLAS and use it in other classes.
3Inheritance allows a class to inherit properties
4(objects, variables, methods) from another source (class or interface).
5Allows code reusability and easy to maintain.
6SUPER CLASS (also known as parent or base class):
7is the class where the fields are being inherited from.
8SUB CLASS (also known as the child or derived class):
9is the class inheriting the properties
10INHERITANCE EXAMPLE
11In my framework I have a TestBase class where I store
12all my reusable code and methods. My test execution classes,
13and elements classes will extend the TestBase in order to reuse the code.
14My framework follow POM and some pages have similar actions,
15so I can easily use those similar actions and fields
16by inheriting them from the ready classes.
17
18 Example: Base Page Class and Test Base Class.
19These 2 class are being inherited from so many different classes.
20For Example; In Pages Package, Base Page Class is being extended
21by all the class by Base Package. So that Constructor
22can be automatically be called in the sub classes.
23That way you will be able to locate the elements
24by using same driver.
25Test Base Class can also be inheritance.
26One driver, TestNG framework one before method to
27setup browser and reuse it every single test class it.
28By inheriting them to other test class. These 2 class are
29abstract class and meant to be inherited to other classes.
30We are not creating any object in these 2 class.
31These class is super class. Comes from Selenium library WebDriver,
32takes Screenshots, javascriptexecuter these are interface.
33List and Set also interface. You cannot create object in interface.
34They are only being reference. These are also example for abstraction.
35
1Java Inheritance (Subclass and Superclass)
2In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:
3
4subclass (child) - the class that inherits from another class
5superclass (parent) - the class being inherited from
6To inherit from a class, use the extends keyword.