rava2
home
documentation
examples
public class Main { public static void main(String[] args) { Person alice = new Person("Alice", 30); Person bob = new Person("Bob", 25); System.out.println(alice.introduce()); System.out.println(bob.introduce()); alice.haveBirthday(); System.out.println("After birthday: " + alice.introduce()); Student student = new Student("Charlie", 20, "Computer Science"); System.out.println(student.introduce()); } } class Person { private String name; private int age; Person(String name, int age) { this.name = name; this.age = age; } String getName() { return name; } int getAge() { return age; } void haveBirthday() { age++; } String introduce() { return "Hi, I'm " + name + " and I'm " + age + " years old."; } } class Student extends Person { private String major; Student(String name, int age, String major) { super(name, age); this.major = major; } String introduce() { return "Hi, I'm " + getName() + ", " + getAge() + " years old, studying " + major + "."; } }
run program
ready