1 / 32

COMP 110: Introduction to Programming

COMP 110: Introduction to Programming. Tyler Johnson Apr 6, 2009 MWF 11:00AM-12:15PM Sitterson 014. Announcements. Lab 7 has been graded Program 5 assigned Milestone 1 due Apr 15th by 5pm Final Submission due Apr 27th by 5pm. Questions?. Today in COMP 110. Go over Lab 7 Inheritance

ila-bell
Download Presentation

COMP 110: Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 110:Introduction to Programming Tyler Johnson Apr 6, 2009 MWF 11:00AM-12:15PM Sitterson 014

  2. Announcements • Lab 7 has been graded • Program 5 assigned • Milestone 1 due Apr 15th by 5pm • Final Submission due Apr 27th by 5pm

  3. Questions?

  4. Today in COMP 110 • Go over Lab 7 • Inheritance • In-Class Exercise

  5. Lab 7

  6. Inheritance • Section 8.2 in text

  7. Inheritance • What are some properties of a Person? • Name, height, weight, age • How about a Student? • ID, major • Does a Student have a name, height, weight, and age? • Student inherits these properties from Person

  8. Inheritance A Student is a Person! This is called an is-a relationship

  9. The is-a Relationship • This inheritance relationship is known as an is-a relationship • A Doctoral student is a Grad student • A Grad student is a Student • A Student is a Person • Is a Person a Student? • Not necessarily!

  10. Inheritance • Define a general class • Such as Person • Later, define specialized classes based on the general class • Such as Student • These specialized classes inherit properties from the general class

  11. Inheritance • Examples of class inheritance Person Transportation Student Employee Car Airplane Animal Undergrad Grad Faculty Staff Horse Elephant Camel Masters Doctoral Nondegree

  12. Base Class • Our general class is called a base class • Also called a parent class or a superclass • Examples: • Person, Transportation

  13. Derived Class • A specialized class that inherits properties from a base class is called a derived class • Also called a child class or a subclass • Examples: • Student is-a Person • Employee is-a Person • Car is-a formof Transportation • Animal is-a formof Transportation Person Student Employee Undergrad Grad Faculty Staff Masters Doctoral Nondegree

  14. Derived Classes • Derived classes can also be base classes • Student is a child class of Person • Student is also the parent class of Undergrad and Grad Person Student Employee Undergrad Grad Faculty Staff Masters Doctoral Nondegree

  15. Why is Inheritance Useful? • Enables you to define shared properties and actions once • If two classes share the same properties, why define them twice? Use inheritance. • Derived classes can perform the same actions as base classes without having to redefine the actions • If desired, the actions can be redefined – more on this later

  16. Inheritance in Java public class Person { private String name; public Person() { name = “No name yet”; } public void setName(String newName) { name = newName; } public String getName() { return name; } }

  17. Inheritance in Java public class Student extends Person { private int id; public Student() { super(); id = 0; } public Student(String stdName, int idNumber) { setName(stdName); setID(idNumber); } public void setID(int idNumber) { id = idNumber; } public int getID() { return id; } } Derived classes only define the functionality that is added to the base class!

  18. The Keyword extends public classDerived_Class_NameextendsBase_Class_Name { Declaration_of_Added_Instance_Variables Definitions_of_Added_And_Overridden_Methods } public class Student extends Person { // stuff goes here } • A derived (child) class inherits the public instance variables and public methods of its base (parent) class

  19. Private vs. Public • private instance variables and private methods in the base class can NOT be accessed by derived classes • This would not work: public Student(String stdName, int idNumber) { name = stdName; // ERROR! name is private to Person setID(idNumber); }

  20. Private vs. Public • private instance variables of the base class CAN be accessed by derived classes using the base class’ public methods • This works: public Student(String stdName, int idNumber) { setName(stdName); // OK! setName is a public method in Person setID(idNumber); }

  21. public class A { private int var1; public int var2; public void setVar1(int newVar1) { var1 = newVar1; } public void setVar2(int newVar2) { var2 = newVar2; } private int foo() { System.out.println("foo"); } } public class B extends A { private int var3; public B(int newVar1) { var1 = newVar1; setVar1(newVar); } public void setVar3(int newVar3) { var3 = newVar3; } } Example //ILLEGAL //LEGAL public static void main(String[] args) { B b = new B(); b.setVar3(7); b.setVar2(4); b.setVar1(5); b.foo(); } //LEGAL, method in class B //LEGAL, public method inherited from class A //LEGAL, public method inherited from class A //ILLEGAL, private method NOT inherited from class A

  22. The Keyword super • Used to call a constructor of the base class (remember, a base class is also known as a superclass) //constructor for the Student class public Student() { super(); //call the constructor of the base class id = 0; }

  23. Overriding Methods • What if the class Person had a method called printInfo? public class Person { // ... public void printInfo() { System.out.println("Name: " + name); } }

  24. Overriding Methods • What if the class Student also had a method called printInfo? public class Student extends Person { // ... public void printInfo() { System.out.println("Name: " + getName()); System.out.println("ID: " + getID()); } }

  25. Overriding Methods • Student inherits the printInfo() method • Student also defines its own printInfo() method • Student would seem to have two methods with the same signature • We saw before that this is illegal, so what happens?

  26. Overriding Methods • Java handles this situation as follows: • If a derived class defines a method with the same name, number and types of parameters, and return type as a method in the base class, the derived class’ method overrides the base class’ method • The method definition in the derived class is the one that is used for objects of the derived class

  27. Example • When using an object of class Person, we call the printInfo method of the Person class Person person = new Person("John Smith"); person.printInfo(); // calls Person’s printInfo method • Output would be: Name: John Smith

  28. Example • When using an object of class Student, we call the printInfo method of the Student class Student std = new Student("John Smith", 37183); std.printInfo(); // calls Student’s printInfo method, // not Person’s • Output would be: Name: John Smith ID: 37183

  29. Overriding vs. Overloading • If a derived class defines a method of the same name, same number and types of parameters, and SAME return type as a base class method, this is overriding • You can also have another method of the same name in the derived class, that differs in the number or types of parameters, this is overloading

  30. Overriding vs. Overloading • What if you define a method in the derived class that has the same name, number and types of parameters, and a DIFFERENT return type? • This is an error • It’s not overloading because the signatures are the same (return type is not part of signature) • It’s not overriding because the return type is different

  31. In-Class Exercise • Turn in for class participation credit

  32. Wednesday • More Inheritance

More Related