cs 2430 day 9. announcements quiz 2.1 this friday program 2 due this friday at 3pm (grace date...
TRANSCRIPT
CS 2430
Day 9
Announcements
• Quiz 2.1 this Friday
• Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Features of OOP
• Encapsulation
• Data hiding
• Abstraction / abstract data type
• NEW: Inheritance
A new keyword: extendspublic class A{ private int x = 0;
public void doIt(int y) { x += y; }
public void print() { System.out.println(x); }}
public class B extends A{ // empty!}
public static void main(String args[]){ B b = new B();
// The method is inherited: b.doIt(10);
// x is printed out! b.print();}
Inheritance example
UML: Class B inherits from class A
A B
Inheritance represents an “is a” relationship
public class A
{
. . .
}
public class B extends A
{
. . .
}
Is-a relationship
• public class Student extends Person{ . . . }
• public class Square extends Rectangle{ . . . }
• public class Civic extends Car { . . . }
An example project// Assume Class A and B exist and B a sub-class of A
public class C
{
public static void main(String args[])
{
A a = new A();
B b = new B();
a.doIt(5);
a.print();
b.doIt(10);
b.print();
}
}
UML for example project
Class C uses classes A and B
Class B inherits from class A
A B
C
Any questions?
Why use inheritance?
• Basic idea: when you want to create a new class and there is already a class that includes some of the code you want
• Simply extend the existing class!
• Can reuse the fields and methods in the existing class
• Less typing!
Some inheritance details
Subclass and superclass
• A class that is derived from another class is called a subclass (a.k.a., child class)
• The class from which a class is derived is called a superclass (a.k.a., parent class)
Subclasses and constructors
• A subclass inherits all members of its parent
• Constructors are (technically) NOT members, so they are not inherited by subclasses
• The constructor of the superclass can (and MUST) be invoked from the subclass using super()– Must be invoked on the first line of the
constructor!
Any questions?
Inheritance example: class Person
public class Person
{
private String name;
private int age; // in years
public Person(String inName, int inAge)
{
name = inName;
age = inAge;
}
public void speak()
{
System.out.print("Hello, my name is: " + name);
}
public void happyBirthday()
{
age += 1;
}
}
A subclass of Person: Student
public class Student extends Person
{
private float GPA; // grade point average
public Student(String inName, int inAge, float inGPA)
{
super(inName, inAge); // NECESSARY!
GPA = inGPA;
}
public void setGPA(float inGPA)
{
GPA = inGPA;
}
}
Subclasses can modify public and protected
methods from the superclass by overriding
them
public class Student extends Person
{
private float GPA; // grade point average
public Student(String inName, int inAge, float inGPA)
{
super(inName, inAge);
GPA = inGPA;
}
public void setGPA(float inGPA)
{
GPA = inGPA;
}
@Override
public void speak()
{
System.out.println("Hello, my name is " + name
+ " and my GPA is " + GPA); // syntax error!
}
}
public class Student extends Person
{
private float GPA; // grade point average
public Student(String inName, int inAge, float inGPA)
{
super(inName, inAge);
GPA = inGPA;
}
public void setGPA(float inGPA)
{
GPA = inGPA;
}
@Override
public void speak()
{
super.speak(); // can't access name directly!
System.out.println(" and my GPA is " + GPA);
}
}
public class Person
{
protected String name;
protected int age; // in years
public Person(String inName, int inAge)
{
name = inName;
age = inAge;
}
public void speak()
{
System.out.print("Hello, my name is: " + name);
}
public void happyBirthday()
{
age += 1;
}
}
public class Student extends Person
{
private float GPA; // grade point average
public Student(String inName, int inAge, float inGPA)
{
super(inName, inAge);
GPA = inGPA;
}
public void setGPA(float inGPA)
{
GPA = inGPA;
}
@Override
public void speak()
{
System.out.println("Hello, my name is " + name
+ " and my GPA is " + GPA); // NOT a syntax error anymore!
}
}
Any questions?