Docsity

Introduction to Inheritance and Interfaces in Computer Science, Study notes of Computer Science

Partial preview of the text

Download Introduction to Inheritance and Interfaces in Computer Science and more Study notes Computer Science in PDF only on Docsity!

Introduction to Computer Science

II

CS112-2008S-

Overview & Review:

Inheritance & Interfaces

David Galles

Department of Computer Science

University of San Francisco

Inheritance

Inheritance is an “is-a” relationship

If class A extends class B, then an element ofclass A

is also

an element of class B

Class A contains all data members andmethods of class B

Inheritance

public class Student extends USFPerson {

protected double gpa;public Student(String name, String id, double gpa){

super(name, id);this.gpa = gpa; } public double getGpa(){

return gpa; } public void setGpa(double gpa){

this.gpa = gpa; } }

Inheritance

Since Student extends USFPerson, a student

is

a

USFPerson Anything you can do with a USFPerson, you cando with a student

getName, setName, getID, setID

Also assign a Student value to a USFPersonvariable

A student

is

a USFPerson

Inheritance

Any compilation errors? Any runtime errors?

Student s = new Student("John Smith", "1101101", 3.5);USFPerson p1 = new USFPerson("Jane Eyre", "101101101");USFPerson p2 = s;System.out.println(s.getGPA());System.out.println(p1.getGPA());System.out.println(p2.getGPA());

Inheritance

Any compilation errors? Any runtime errors?

Student s = new Student("John Smith", "1101101", 3.5);USFPerson p1 = new USFPerson("Jane Eyre", "101101101");USFPerson p2 = s;System.out.println(s.getGPA());Student s1 = (Student) p1;System.out.println(s1.getGPA());Student s2 = (Student) p1;System.out.println(s2.getGPA());

Polymorphism

public class USFPerson {

protected String name;protected String id;public USFPerson(String name, String id) {

this.name = name;this.id = id; } // getName, setName, getID, setIDpublic void print() {

System.out.pritnln("Name: " + name);System.out.pritnln("ID:

" + id);

Polymorphism

public class Student extends USFPerson {

protected double gpa;public Student(String name, String id, double gpa){

super(name, id);this.gpa = gpa; } public double getGpa(){

return gpa; } public void setGpa(double gpa){

this.gpa = gpa; } public void print() {

System.out.pritnln("Name: " + name);System.out.pritnln("ID:

" + id);

System.out.println("GPA:

" + gpa);

Polymorphism

USFPerson personArray[] = new USFPerson[4];personArray[0] = new USFPerson("Emily Dickison", "1101101");personArray[1] = new USFStudent("Anton Chekhov", "111010011", 3.5);personArray[2] = new USFStudent("Doris Lessing", "101010101", 3.8);personArray[3] = new USFPerson("Charles Dickens", "11111101");for (int i = 0; i < 4; i++)

personArray[i].print();

Abstract Classes

A class can be defined as “abstract”

Can’t instantiate instances of abstract classes Can

have variables whose types are abstract

classes We can make USFPerson abstract

Abstract Classes

What is legal? Illegal? USFPerson p;p = new Student("John", "11011", 2.2);System.out.println(p.getName());USFPerson p2;p2 = new Student("John", "11011", 2.2);System.out.println(p.getGPA());USFPerson p3;p3 = new USFPerson("John", "11011");System.out.println(p3.getGPA());

Abstract Methods

Abstract classes can have abstract methods

Give the signature (name, return typeparameters) of the method Don’t give the body of the method Any class that extends a class with abstractmethods needs to either implement the abstractmethods, or be abstract itself

Abstract Methods

public class Student extends USFPerson {

public final static double STUDENT_QUOTA = 3000000.0;protected double gpa;public Student(String name, String id, double gpa){

super(name, id);this.gpa = gpa; } // getGPA, setGPApublic double getEmailQuota(){

return STUDENT_QUOTA; } }

Multiple Inheritance

Have an object that is a member of two sets

Cat is a Mammal and a Carnivor Bat is a Mammal and a FlyingAnimal

Might be nice to create a Mammal class and aFlyingAnimal Class, have the Bat class extendboth. Java does not allow Multiple Inheritance

What are some difficulties with multipleinheritance?