Skip to content

Instantly share code, notes, and snippets.

@jimmykurian
Created March 13, 2012 05:27
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jimmykurian/2027006 to your computer and use it in GitHub Desktop.
Save jimmykurian/2027006 to your computer and use it in GitHub Desktop.
A super class Person with two subclasses, Student and Instructor, that inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary.
//Instructor.java - Jimmy Kurian
public class Instructor extends Person
{
private double salary;
public Instructor(String n, int byear, double s)
{
super(n, byear);
salary = s;
}
public String toString()
{
return "Employee[super=" + super.toString() + ",salary=" + salary + "]";
}
}
//Person.java - Jimmy Kurian
public class Person
{
private String name;
private int birthYear;
public Person(String n, int byear)
{
name = n;
birthYear = byear;
}
public String toString()
{
return "Person[name=" + name + ",birthYear=" + birthYear + "]";
}
}
//PersonTester.java - Jimmy Kurian
public class PersonTester
{
public static void main(String[] args)
{
Person a = new Person("Anil", 1992);
Student b = new Student("Jimmy", 1919, "Information Technology");
Instructor c = new Instructor("Mike", 1998, 95000);
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
//Student.java - Jimmy Kurian
public class Student extends Person
{
private String major;
public Student(String n, int byear, String m)
{
super(n, byear);
major = m;
}
public String toString()
{
return "Student[super=" + super.toString() + ",major=" + major + "]";
}
}
@apskis
Copy link

apskis commented Apr 21, 2019

Great Job on this. Super simple

@David-Gobig
Copy link

cool

@SUVENDUC789
Copy link

class Cube {
double side;

public Cube(double side) {
    this.side = side;
}

public double getVolume() {
    return Math.pow(this.side, 3);
}

}

class Cylinder extends Cube {
double height;

public Cylinder(double radius, double height) {
    super(radius);
    this.height = height;
}

@Override
public double getVolume() {
    return Math.PI * (Math.pow(this.side, 2)) * height;
}

}

public class Q4 {
public static void main(String[] args) {
double side = 10;
Cube c = new Cube(side);
System.out.println("Cube Side = " + side + " | Cube Volume = " + c.getVolume());

    double height = 5;
    Cylinder dy = new Cylinder(side, height);
    System.out.println("Cylinder radious = " + side + " Cylinder height = " + height + " | Cylinder volume = "
            + dy.getVolume());
}

}

@SUVENDUC789
Copy link

class Person {
String name;
String address;

public Person(String name, String address) {
    this.name = name;
    this.address = address;
}

// incorporate one method setPerson() that updates Person variables
public void setPerson(String name, String address) {
    this.name = name;
    this.address = address;
}

public void showDetails() {
    System.out.println("Name = " + this.name);
    System.out.println("Address = " + this.address);
}

}

class Student extends Person {
String program;
String year;
double fees;

public Student(String name, String address, String program, String year, double fees) {
    super(name, address);
    this.program = program;
    this.year = year;
    this.fees = fees;
}

public void setStudent(String name, String address, String program, String year, double fees) {
    this.name = name;
    this.address = address;
    this.program = program;
    this.year = year;
    this.fees = fees;
}

@Override
public void showDetails() {
    super.showDetails();
    System.out.println("Programming language = " + this.program);
    System.out.println("Year = " + this.year);
    System.out.println("Fees = " + this.fees);
}

}

class Staff extends Person {
String school;
double pay;

public Staff(String name, String address, String school, double pay) {
    super(name, address);
    this.school = school;
    this.pay = pay;
}

public void setStaff(String name, String address, String school, double pay) {
    this.name = name;
    this.address = address;
    this.school = school;
    this.pay = pay;
}

@Override
public void showDetails() {
    super.showDetails();
    System.out.println("School = " + this.school);
    System.out.println("Pay = " + this.pay);
}

}

public class Q3 {
public static void main(String[] args) {
Person p = new Person("Suv", "48 kkt road kolkata - 35");
p.showDetails();
p.setPerson(null, null);
p.showDetails();

    Student s = new Student("Suvendu Chowdhury", "48 kkt road kolkata -35", "Java + python", "2020-2023", 8848);
    s.showDetails();
    s.setStudent(null, null, null, null, 0);
    s.showDetails();

    Staff google = new Staff("Suvendu Chowdhury", "48 kkt road kolkata -35",
            "Baranagar Narendra nath vidya mandir ", 1100000);
    google.showDetails();
    google.setStaff(null, null, null, 0);
    google.showDetails();
}

}

@DarrenWellsy
Copy link

DarrenWellsy commented Feb 17, 2023

I'd be happy to explain how to create a superclass called "Person" with two subclasses, "Student" and "Instructor", both inheriting from "Person". In object-oriented programming, a superclass is a class inherited by one or more subclasses, which can then inherit the superclass's attributes and methods. To create the "Person" superclass, we would define characteristics such as a person's name and birth year. The "Student" subclass would then inherit these attributes from "Person" and add its feature, such as a student's major. Similarly, the "Instructor" subclass would inherit from "Person" and add its attribute, such as an instructor's salary. By the way, https://edubirdie.com/top-writers is the source I use every day to get help and instructions on how to learn to code. Here are essay top writers whose knowledge I trust the most and who help me get good academic grades.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment