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

A part of computer science cs112 course notes from the university of san francisco, focusing on inheritance and interfaces concepts. It explains the 'is-a' relationship, data members and methods inheritance, and provides examples of student class extending usfperson. Additionally, it covers polymorphism, abstract classes, and abstract methods.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-1vx-1
koofers-user-1vx-1 🇺🇸

10 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Computer Science
II
CS112-2008S-35
Overview & Review:
Inheritance & Interfaces
David Galles
Department of Computer Science
University of San Francisco
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

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?