Skip to main content

Inheritance

Inheritance | Java: Inheritance means gets the properties from parent. With this child class can access the variables or methods from parent class.

Syntax:                            class child_class extends parent_class .

Here is the Code:

  1. import java.io.*;
  2. class Main {
  3. public static void main (String[] args) {
  4. A a = new A(1);
  5. B b = new B(3);
  6. System.out.println(a.getX());
  7. System.out.println(b.getX());
  8. }
  9. }
  10. class B extends A 
  11. {
  12.     public B(int x)
  13.     {
  14.         super(x);
  15.     }
  16. }
  17. class A 
  18. {
  19.     int x;
  20.     A(int x)
  21.     {
  22.         this.x = x;
  23.     }
  24.     public int getX()
  25.     {
  26.         return this.x;
  27.     }
  28. }

Output:
1
3

Here is the Video: