Introduction to java
Control Statements in JAVA
Array in JAVA
Command Line Arguments in JAVA
Introducing Classes in JAVA
Methods and Classes in JAVA
Encapsulation in JAVA
Polymorphism in JAVA
Inheritance in PHP
Abstract Class in JAVA
Interface in JAVA
Packages in JAVA
Stream & File Input Output (IO) Java
Wrapper Classes in JAVA
Collection API in JAVA
Exception Handling in JAVA
Multi-Threading in JAVA
Applet and Graphics in JAVA

Core Java

Inheritance in PHP

Inheritance in Java

Content Author
Sameer A Patil
Sameer A Patil
Inheritance Program Java
                            
                            

class Red

{

  int a=10;

  int x=0;

  Red(int x){

    this.x=x;

    System.out.println("in Red Constructor! a="+a);

  }

  public void disp(){

    System.out.println("in disp() = "+x);

  }

}

class Green extends Red

{

  int a=50;

  Green(int x){

    super(x);

    System.out.println("in Green Constructor! a="+a);

  }

  public int getParentA(){

    return super.a;

  }

}

class Blue extends Green

{

  int a=20;

  Blue(int a){

    super(a);

    System.out.println("in Blue Constructor! a="+a);

    System.out.println("in Blue Constructor! this.a="+this.a);

    System.out.println("in Blue Constructor! super.a="+super.a);

    System.out.println("in Blue Constructor! super.a="+getParentA());

  }

}

class InDemo{

  public static void main(String[] ar){

    System.out.println("*** InDemo ***");

    Blue blue=new Blue(9);

    blue.disp();

  }

}

Output
D:\javaprg\Inh>java InDemo
*** InDemo ***
in Red Constructor! a=10
in Green Constructor! a=50
in Blue Constructor! a=9
in Blue Constructor! this.a=20
in Blue Constructor! super.a=50
in Blue Constructor! super.a=10
in disp() = 9
Comment is Only Available for registered users! Create Account or Login Now!