Answers for "what is inheritance in java"

1

interface inheritance in java

Inheritance is an important pillar of OOPs(Object Oriented Programming). 
It is the mechanism in java by which one class is allowed to inherit 
the features(fields and methods) of another class. Like a class, an 
interface can have methods and variables, but the methods declared 
in an interface are by default abstract (only method signature, no body).

// Java program to demonstrate
// the multiple inheritance
// in interface
  
// Interface to implement the
// addition and subtraction methods
interface Add_Sub {
    public void add(double x, double y);
    public void subtract(double x, double y);
}
  
// Interface to implement the
// multiplication and division
interface Mul_Div {
    public void multiply(double x, double y);
    public void divide(double x, double y);
}
  
// Calculator interface which extends
// both the above defined interfaces
interface Calculator extends Add_Sub, Mul_Div {
    public void printResult(double result);
}
  
// Calculator class which
// implements the above
// interface
public class MyCalculator implements Calculator {
  
    // Implementing the addition
    // method
    public void add(double x, double y)
    {
        double result = x + y;
        printResult(result);
    }
  
    // Implementing the subtraction
    // method
    public void subtract(double x, double y)
    {
        double result = x - y;
        printResult(result);
    }
  
    // Implementing the multiplication
    // method
    public void multiply(double x, double y)
    {
        double result = x * y;
        printResult(result);
    }
  
    // Implementing the division
    // method
    public void divide(double x, double y)
    {
        double result = x / y;
        printResult(result);
    }
  
    // Implementing a method
    // to print the result
    public void printResult(double result)
    {
        System.out.println(
            "The result is : " + result);
    }
  
    // Driver code
    public static void main(String args[])
    {
  
        // Creating the object of
        // the calculator
        MyCalculator c = new MyCalculator();
        c.add(5, 10);
        c.subtract(35, 15);
        c.multiply(6, 9);
        c.divide(45, 6);
    }
}
Posted by: Guest on November-11-2021
1

inheritance in java

Builds relations between classes, main purpose: 
create a TEST BASE CLAS and use it in other classes.
Inheritance allows a class to inherit properties 
(objects, variables, methods) from another source (class or interface). 
Allows code reusability and easy to maintain.
SUPER CLASS (also known as parent or base class): 
is the class where the fields are being inherited from. 
SUB CLASS (also known as the child or derived class): 
is the class inheriting the properties
INHERITANCE EXAMPLE
In my framework I have a TestBase class where I store 
all my reusable code and methods. My test execution classes, 
and elements classes will extend the TestBase in order to reuse the code. 
My framework follow POM and some pages have similar actions, 
so I can easily use those similar actions and fields 
by inheriting them from the ready classes.

  Example: Base Page Class and Test Base Class. 
These 2 class are being inherited from so many different classes. 
For Example; In Pages Package, Base Page Class is being extended 
by all the class by Base Package. So that Constructor 
can be automatically be called in the sub classes. 
That way you will be able to locate the elements 
by using same driver. 
Test Base Class can also be inheritance. 
One driver, TestNG framework one before method to 
setup browser and reuse it every single test class it. 
By inheriting them to other test class. These 2 class are 
abstract class and meant to be inherited to other classes. 
We are not creating any object in these 2 class. 
These class is super class. Comes from Selenium library WebDriver, 
takes Screenshots, javascriptexecuter these are interface. 
List and Set also interface. You cannot create object in interface. 
They are only being reference. These are also example for abstraction.
Posted by: Guest on June-17-2021
4

Inheritance in java

// Hierarchical inheritance in java
class Animal
{
   void eating()
   {
      System.out.println("animal eating");
   }
}
class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion roaring");
   }
}
public class Dog extends Animal
{
   void bark()
   {
      System.out.println("dog barking");
   }
   public static void main(String[] args)
   {
      Animal obj1 = new Animal();
      obj1.eating();
      Lion obj2 = new Lion();
      obj2.eating();
      obj2.roar();
      Dog obj3 = new Dog();
      obj3.eating();
      obj3.bark();
   }
}
Posted by: Guest on November-23-2020
1

inheritance in java

class Arithmetic {

}
class Adder extends Arithmetic {
    public int add(int a, int b) 
    {
        return a+b;
    }
}
Posted by: Guest on August-10-2021
2

Inheritance in java

// single inheritance example
import java.util.*;
class Animal
{
   void eat()
   {
      System.out.println("Animal is eating.");
   }
}
class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion is roaring.");
   }
   public static void main(String[] args)
   {
      Lion obj = new Lion();
      obj.eat();
      obj.roar();
   }
}
Posted by: Guest on November-23-2020
2

Inheritance in java

// Multiple inheritance in java
interface M
{
   public void helloWorld();
}
interface N
{
   public void helloWorld();
}
// implementing interfaces
public class MultipleInheritanceExample implements M, N
{
   public void helloWorld()
   {
      System.out.println("helloWorld() method");
   }
   public static void main(String[] args) 
   {
      MultipleInheritanceExample obj = new MultipleInheritanceExample();
      obj.helloWorld();
   }
}
Posted by: Guest on November-23-2020

Code answers related to "what is inheritance in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language