170 likes | 292 Views
CS12230 Introduction to Programming Lecture 8-1 – Inheritance. Review you should be able to do:. A Night class has a code, a title, a number of lectures, one Teacher, and lots of Students. Draw a use-case diagram for a program to do normal registering etc for students for the class
E N D
CS12230Introduction to ProgrammingLecture 8-1 – Inheritance
Review you should be able to do: • A Night class has a code, a title, a number of lectures, one Teacher, and lots of Students. • Draw a use-case diagram for a program to do normal registering etc for students for the class • Draw the class diagram assuming all links are in NightClass. • Draw an object diagram as of 3/11/09 for class ‘Advanced cookery’ with students fred and bill and teacher mary • What responsibilities/methods does this lead to in NightClass?
public class Application { private NightClass nc; public Application(String name) { nc=new NightClass(name); } public void runApp(){ do{ //print menu //get response switch (response) { case 'a': //do whatever nc.method1() break; case 'b': //do whatever nc.method2() break; // etc etc } } while (response!= 'q'); } //////////////////////////////////// public void main(String args[] ) { Application app=new Application(“cooking”) ; app.runApp(); } } public class NIghtClass { private String name; private Teacher teach; private ArrayList<Student> students; public NightClass(String n){ name=n; teach=new Teacher(); //default students=new ArrayList<Student>(); } public void addStudent(Student s) { students.add(s); } public void addTeacher (Teacher t) { teach=t; } public String toString() { String ret="Class is "+code+"\n Teacher : "+ teach.toString()+"\n+ Students are: \n"; for (Student s:students) { ret+=s.toString(); } return ret; } //etc etc }
Along the way we have also mentioned: • Exceptions • Files • Searching • Sorting (compareTo) • ….
Now, a new relationship between things • We have done ‘has-a’ and ‘has-some’ • A Module has-a Teacher • A Module has-some Students • Now, ‘is-a’ • A Teacher is-a Person • A Student is-a Person This is called inheritance
This is a way of figuring out common attributes and behaviours toString() is overridden (exists at lower level also) Person -String name +String getName() +void setName(String n) +String toString() This is called the BASE class These are new – for Students not Staff Staff -double salary Also has a name +getSalary() and setsalary() +String toString() Can also use getName() and setName() Student -String degree Also has a name +getdegree() and setdegree() +String toString() Can also use getName() and setName() These are called the DERIVED classes
This is how BlueJ shows it (codeExamples/8-inheritance-code)
public class Person{ // instance variables protected String name; //protected makes 'inheritance ready' //methods public Person () { name="unknown"; } public Person(String n) { name=n; } public String toString(){ return name; } public String getName(){ return name; } public void setName(String n) { name=n; } } public class Student extends Person{ //instance variables //also has name! private String degree; public Student(){ super(); } public Student(String n, String d) { super(n); degree=d; } public void setDegree(String d) { degree=d; } public String getDegree() { return degree; } //this is called overriding because there is one at the level above (Person) public String toString() { return name+" studies " +degree+"\n"; } }
What is this ‘protected’ word? • Every Student will have a name (that’s what inheritance means) • BUT you won’t be able to use the variable ‘name’ in your Student class UNLESS you make it protected in the BASE (ie. Person) class • You’d have to use getName() and setName() to manipulate it (look back at the toString() for Student – it USES name) • Making instance variables protected is called making a class ‘inheritance ready’
How use? public void runApp(){ System.out.println(p.toString()); System.out.println(lynda.toString()); System.out.println(fred.toString()); //works fine: System.out.println("name is “ +fred.getName()); //cannot do: // lynda=p; // lynda=fred; //uses the ‘right’ toString(): p=lynda; System.out.println(p.toString()); //uses ‘right’ toString(): p=fred; System.out.println(p.toString()); } } public class TestThis{ private Person p; private Staff lynda; private Student fred; ////////////////////// public static void main(String args[]) { TestThis app=new TestThis(); app.runApp(); } //////////////////// public TestThis() { p=new Person("basic-person-p"); lynda =new Staff("lynda",1000); fred=new Student("fred","G400"); }
Polymorphism is the ability to pick the ‘right’ version of the method • How does JVM (Jave Virtual Machine) do that? • It starts with the most specific and works its way up the inheritance hierarchy • Notice that the toString() is overriden in Student - it uses the ‘right’ one for Students not the one in Person
Bigger example – draw a picture public void runApp() { Person p=new Person("basic-p"); Staff lynda =new Staff("lynda",1000); Student fred=new Student(“fred","G400"); people.add(p); people.add(lynda); people.add(fred); System.out.println("all"); printAll(); } import java.util.*; public class BigTest { private ArrayList<Person> people; ///////////////////// public static void main(String args[]) { BigTest app=new BigTest(); app.runApp(); } //////////////////// public BigTest() { people=new ArrayList<Person>(); } public void printAll() { for (Person q:people) System.out.println(q.toString()); }
Consider a drawing program • It has lots of kinds of shapes: circle, square, line, filled or not etc • They all have a position and a color • They all have to be stored in an ArrayList • But they also have things that are different • And they are all drawn – but differently! Draw a class diagram for this problem!
What have we just done? • Basic inheritance • And polymorphism