Advertisement
fosterbl

Student/Person inheritance code

Feb 4th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. //This class is created in the same file as Student. This is typically not common practice but I thought it was a good idea to see both classes in the same file.
  2. class Person{
  3.    private String name;
  4.  
  5.    public Person(String n){
  6.       name = n;
  7.    }
  8.      
  9.    public String toString(){
  10.       return name;
  11.    }      
  12. }
  13.  
  14. public class Student extends Person{
  15.    private int id;
  16.      
  17.    public Student(String n,int i){
  18.       super(n);
  19.       id = i;
  20.    }
  21.      
  22.    public String toString(){
  23.       return super.toString() + " ID: " + id;
  24.    }
  25.  
  26.    public static void main(String[] args){
  27.       Student s = new Student("Nancy", 55);
  28.       System.out.println(s instanceof Student);
  29.       System.out.println(s instanceof Person);
  30.       System.out.println(s);
  31.    }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement