Docsity

Inheritance and Interfaces - Lecture Notes | CS 112, Study notes of Computer Science

Toggle sidebar
bg1
CS112-2008S-35 Overview & Review:
Inheritance & Interfaces 1
35-0: Inheritance
•Inheritance is an “is-a” relationship
•If class A extends class B, then an element of class A is also an element of class B
•Class A contains all data members and methods of class B
35-1: Inheritance
public class USFPerson {
protected String name;
protected String id;
public USFPerson(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
35-2: 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;
}
}
35-3: Inheritance
•Since Student extends USFPerson, a student is a USFPerson
•Anything you can do with a USFPerson, you can do with a student
•getName, setName, getID, setID
•Also assign a Student value to a USFPerson variable
•A student is a USFPerson
35-4: Inheritance
Student s = new Student("John Smith", "1101101", 3.5);
USFPerson p1 = new USFPerson("Jane Eyre", "101101101");
USFPerson p2 = s;
System.out.println(s.getName());
System.out.println(p1.getName());
System.out.println(p2.getName());
35-5: Inheritance
Any compilation errors? Any runtime errors?
pf3
pf4
pf5

Partial preview of the text

Download Inheritance and Interfaces - Lecture Notes | CS 112 and more Study notes Computer Science in PDF only on Docsity!

CS112-2008S-

Inheritance & Interfaces 1

35-0: Inheritance

• Inheritance is an “is-a” relationship

• If class A extends class B, then an element of class A is also an element of class B

• Class A contains all data members and methods of class B

35-1: Inheritance

public class USFPerson { protected String name; protected String id;

public USFPerson(String name, String id) { this.name = name; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } Public String getId() { return id; } public void setId(String id) { this.id = id; } }

35-2: 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; } }

35-3: Inheritance

• Since Student extends USFPerson, a student is a USFPerson

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

• getName, setName, getID, setID

• Also assign a Student value to a USFPerson variable

• A student is a USFPerson

35-4: Inheritance

Student s = new Student("John Smith", "1101101", 3.5); USFPerson p1 = new USFPerson("Jane Eyre", "101101101"); USFPerson p2 = s;

System.out.println(s.getName()); System.out.println(p1.getName()); System.out.println(p2.getName());

35-5: Inheritance

Any compilation errors? Any runtime errors?

CS112-2008S-

Inheritance & Interfaces 2

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());

35-6: 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());

35-7: 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(((Studnet) p1).getGPA()); System.out.println(((Student) p2).getGPA());

35-8: 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, setID

public void print() { System.out.pritnln("Name: " + name); System.out.pritnln("ID: " + id); } }

35-9: 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); } }

35-10: 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() {

CS112-2008S-

Inheritance & Interfaces 4

35-16: Abstract Methods

public abstract class USFPerson { protected String name; protected String id;

public USFPerson(String name, String id) { this.name = name; this.id = id; }

// getName, getID, setName, setID

public abstract double getEmailQuota(); }

35-17: 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, setGPA

public double getEmailQuota() { return STUDENT_QUOTA; } }

35-18: 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 a FlyingAnimal Class, have the Bat class extend both.

• Java does not allow Multiple Inheritance

• What are some difficulties with multiple inheritance?

35-19: Multiple Inheritance

class FlyingAnimal class Mammal { { ... ... void move() void move() { { System.out.println("Flying!"); System.out.println("Walking"); } } } }

class Bat extends FlyingAnimal, Mammal { ... }

Bat b = new Bat(); b.move() <== What would this do?

35-20: Interfaces

• Can get most of the advantages of multiple inheritance with Interfaces

• An interface is a like a contract:

• Implement an interface ==¿ promise to implement all of the methods of the interface

CS112-2008S-

Inheritance & Interfaces 5

• No code associated with an interface

35-21: Interfaces

public interface Comparable { public int compareTo(Object o); }

Any class that implements Comparable needs to have compareTo method

35-22: Interfaces

• You can have a variable whose type is an interface

• Any class that implements that interface can be assigned to that variable

Comparable c1 = new Integer(2); Comparable c2 = new Integer(3); Comparable c3 = new String("foo"); Comparable c4 = new String("bar");

System.out.println(c1.compareTo(c2)); System.out.println(c3.compareTo(c4));

What would the output of this fragment be?

35-23: Interfaces

Comparable c1 = new Integer(2); Comparable c4 = new String("bar");

System.out.println(c1.compareTo(c4));

What do you think this code would do, and why?

35-24: Interfaces

• You can implement more than one interface

class A implements B, c

// Code for A

• Class A is pledging to implement all of the methods defined in both class B and class C

35-25: Interfaces

• We can use Interfaces as a substitute for multiple inheritance

class Bat exends Mammal implements FlyingAnimal {

}

• Not exactly the same as multiple inheritance (why?)