What is inheritance in Java?

Hi,

    Please tell me about inheritance in java.

Last updated:7/17/2019 8:06:45 AM

2 Answers

Rahul Roi
Rahul Roi

Inheritance;- 


Types of Inheritance:-

Inheritance;-

  • When one class is derived from another class.
  • In other words when one object (child) holds all the properties of another object (parent) is called inheritance.
  • extend keyword is used to derive the class properties.

A term used in Inheritance concepts:-

Subclass (child class):- it is a class that inherits another class. It is also called “derived class”.

Superclass (parent class):- it is also class from where derived class inherits the property.

Types of Inheritance:-

1) Single inheritance:-

What is inheritance in Java?

Example-

class Fruits
{     void M1()     {         System.out.println('M1');     } }
class Mango extends Fruits {     void M2()        
{     System.out.println('M2'); }
}
class Mask{    
 public static void main(String args[]) { Mango m=new Mango(); m.M1(); m.M2(); } } Output-
M1
M2

2) Multilevel Inheritance:-

     What is inheritance in Java?

Example- 

class Multilevel
{
    void M1()
    {
      System.out.println('M1');
    }
}
class child1 extends Multilevel
{
    void M2(){
        System.out.println('M2');
       }
 }
class child2 extends child1{
    void M3(){
        System.out.println('M3');
         }
}
class child3 extends child2{
    void M4(){
        System.out.println('M4');
        }
}
class More{
    public static void main(String[] args) {
        child3 c=new child3();
        c.M3();
        c.M2();
        c.M1();
        c.M4();
    }
Output-
       M3
M2
M1
M4

3)Multiple Inheritance:-

What is inheritance in Java?

4) Hierarchical Inheritance:-

What is inheritance in Java?

Example- 

 class Flowers
{ void M1() {     System.out.println('M1'); } } class Dog extends Flowers{ void M2() {     System.out.println('M2');} } class Cat extends Flowers{ void M3() {     System.out.println('M3');} } class Dav {
public static void main(String args[])
{
Cat c=new Cat();
c.M3();
c.M1();
Dog d=new Dog();
d.M2();
d.M1();
}
}
Output-
M3
M1
M2
M1

5) Hybrid Interface:-

What is inheritance in Java?


What is inheritance in Java?



ultra keto 360
ultra keto 360

Others fail on account of an issue often called information ultra keto 360 overload, which means that you've so much information about weight loss dieting and exercising that you are unable to start. You would like only one extra day to analyze as well as try to make the perfect program. While nothing at all is wrong with studying weight loss and fitness, it's more important to get started. Get started now and as soon as you discover an innovative or more desirable method, make improvements to your dieting.

https://fairsupplement.com/ultra-keto-360/


Answer