Inheritance in Java

Inheritance In Java

Inheritance means to use properties and methods of one class to another. We can use the extends keyword to achieve inheritance.


class Base 
  // properties and methods
class Derived extends Base
     // properties and methods



public class InheritanceWork {
    public static void main(String[] args) {
      Student s = new Student(); = "Himal";
      s.color = "White";
      s.height = 5.9;
      s.rollno = 6;

class Person{
    String name;
    double height;
    double weight;
    String color;

    void printAll(){
        System.out.println("Name is "+name);
        System.out.println("Height is "+height);
        System.out.println("Weight is "+weight);
        System.out.println("Color is "+color);
class Student extends Person{

    int rollno;

    void printStudentInfo(){
        System.out.println("Roll no is "+rollno);
  • Here Student class is using properties and methods from the Person class.