xFactorSchool.com(Beta)
How to Do Examples
Python Examples
HTML Examples
Code Examples
C Code Examples
C++ Code Examples
C# Code Examples
Java Code Examples
Python Code Examples
HTML Code Examples
CSS Code Examples
JavaScript Code Examples
Online Editor
C Code Editor
C++ Code Editor
C# Code Editor
Java Code Editor
Python Code Editor
HTML Code Editor
CSS Code Editor
JavaScript Code Editor
Practice how to inherit a class in Java
Java Online Editor
Execute Code
More Java Sample Codes
public class ExampleApp { public static void main(String[] args) { Student studentObj = new Student(); // studentObj is the object of the Student class studentObj.setName("John"); // assign value to class property studentObj.setAge(15); // assign value to class property studentObj.setStudentId(11); System.out.println(studentObj.getName() + " is " + studentObj.getAge() + " years old. His student id is " + studentObj.getStudentId() + "."); } } // Person class is the base (parent) class class Person { private String name; private int age; // Getter and setter methods for name property public String getName() { return name; } public void setName(String name) { this.name = name; } // Getter and setter methods for age property public int getAge() { return age; } public void setAge(int age) { this.age = age; } } // Student class is the derived (child) class inheriting from the base (Person) class class Student extends Person { //Inherit Person class private int studentId; // Getter and setter methods for studentId property public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } }