Inheritance PDF
Document Details
Uploaded by Deleted User
Tags
Related
Summary
This document describes the concept of inheritance in Java programming. It explains how to create subclasses and superclasses, and how to use the super keyword to call the superclass constructor. It also covers method overriding and the toString() method. The document provides code examples to illustrate the concepts.
Full Transcript
Inheritance Introduction Classes as templates for objects Often there is code overlap in what is required for a class Methods Variables Creating a system for a university example There are common variables we need such as name and ID Redundant to have to create those fi...
Inheritance Introduction Classes as templates for objects Often there is code overlap in what is required for a class Methods Variables Creating a system for a university example There are common variables we need such as name and ID Redundant to have to create those fields for each object This is where inheritance becomes useful Allows us to design classes without redundancy and improve readability Superclass and Subclasses To create an inheritance relationship between classes, first define a superclass or a parent class This parent class defines common properties and behaviors A subclass or child class "extends" the superclass/parent class The subclass inherits the data fields and methods from the superclass Subclass and Superclass relationship You can define the relationship between a subclass and superclass is as an "is-a" relationship A dog is an animal A cat is an animal A circle is a shape A rectangle is a shape Creating a Superclass We can start by defining a User superclass Declare common variables and methods in this class Protected Protected is another access modifier along with public and private Protected variables can be accessed in the same class, subclass, and other classes in the same package 1 public class User{ 2 private String name; 3 private String email; 4 private int ID; 5 protected static int IDCount = 10000; 6 7 public User(String name, String email) { 8 this.name = name; 9 this.email = email; 10 ID = createID(); 11 } 12 13 private int createID(){ 14 IDCount++; 15 return Integer.parseInt(89 + "" + IDCount); 16 } 17 18 public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public String getEmail() { 27 return email; 28 } 29 30 public void setEmail(String email) { 31 this.email = email; 32 } 33 34 public int getID() { 35 return ID; 36 } 37 38 public void setID(int ID) { 39 this.ID = ID; 40 } 41 } 42 Creating a subclass To create a subclass you use the extends keyword in the class declaration class childClass extends parentClass{} super To call the superclass constructor you can use the super keyword This allows for the variables from the parent class to be defined The call to the superclass must be the first line in the constructor 1 public class Faculty extends User{ 2 3 private String role; 4 5 public Faculty(String name, String email, String role) { 6 super(name, email); 7 this.role = role; 8 } 9 10 } Overriding To override a method, the method must be defined in the subclass using the same signature and the same or compatible return type. Remember, to overload a method we changed the signature You cannot override a static method A private method cannot be overridden The toString is a built in method that prints a description of an object, includes class name and hash code of the object by default We can override this to be more helpful 1 2 3 import java.util.ArrayList; 4 5 class Course { 6 private String name; 7 private Faculty instructor; 8 private ArrayList students = new ArrayList(); 9 private int section; 10 public Course(String name, int Section) { 11 this.name = name; 12 this.section = Section; 13 } 14 15 public void setInstructor(Faculty instructor) { 16 instructor.addCourse(this); 17 18 this.instructor = instructor; 19 } 20 21 public void addStudent(Student s){ 22 23 students.add( s); 24 } 25 26 public void dropStudent(Student s){ 27 students.remove(s); 28 } 29 30 31 } 32 33 class User{ 34 private String name; 35 private String email; 36 private int ID; 37 protected static int IDCount = 10000; 38 39 public User(String name, String email) { 40 this.name = name; 41 this.email = email; 42 ID = createID(); 43 } 44 45 private int createID(){ 46 IDCount++; 47 return Integer.parseInt(89 + "" + IDCount); 48 } 49 50 public String getName() { 51 return name; 52 } 53 54 public void setName(String name) { 55 this.name = name; 56 } 57 58 public String getEmail() { 59 return email; 60 } 61 62 public void setEmail(String email) { 63 this.email = email; 64 } 65 66 public int getID() { 67 return ID; 68 } 69 70 public void setID(int ID) { 71 this.ID = ID; 72 } 73 74 @Override 75 public String toString(){ 76 return name + " " + email + " " + ID; 77 } 78 } 79 80 81 class Faculty extends User{ 82 83 private String role; 84 private ArrayList courses= new ArrayList(); 85 86 public Faculty(String name, String email, String role) { 87 super(name, email); 88 this.role = role; 89 } 90 public void addCourse(Course course){ 91 courses.add(course); 92 } 93 94 @Override 95 public String toString(){ 96 return super.toString() + " " + role ; 97 } 98 } 99 100 class Student extends User { 101 private ArrayList courses = new ArrayList(); 102 private String major; 103 104 public Student(String name, String email) { 105 super(name, email); 106 major = "undeclared"; 107 } 108 public void addCourse(Course c){ 109 courses.add(c); 110 c.addStudent(this); 111 } 112 113 public String getMajor() { 114 return major; 115 } 116 117 public void setMajor(String major) { 118 this.major = major; 119 } 120 121 @Override 122 public String toString(){ 123 return super.toString() + " " + major; 124 } 125 126 } 127 public class LSU_Users { 128 public static void main(String[] args) { 129 User maintenance = new User("john", "[email protected]"); 130 Student s1 = new Student("sam", "[email protected]"); 131 Student s2 = new Student("jill", "[email protected]"); 132 System.out.println(s1.getID()); 133 System.out.println(s2.getID()); 134 Course csc1350 = new Course("CSC1350", 1); 135 s1.addCourse(csc1350); 136 Faculty f1 = new Faculty("Billie Smith", "[email protected]", "instructor" 137 csc1350.setInstructor(f1); 138 s2.addCourse(csc1350); 139 ArrayList users = new ArrayList(); 140 users.add(maintenance); 141 users.add(s1); 142 users.add(s2); 143 users.add(f1); 144 145 System.out.println(f1); 146 System.out.println(s1); 147 System.out.println(maintenance); 148 149 150 } 151 } 152 8910002 8910003 Billie Smith [email protected] 8910004 instructor sam [email protected] 8910002 undeclared john [email protected] 8910001