0% found this document useful (0 votes)
23 views

CHAPTER 3 Inheritance

Inheritance allows classes to inherit and extend the functionality of other classes. Subclasses inherit all fields and methods of the superclass. Subclasses can override methods from the superclass. Access levels like public, private, and protected determine which classes can access fields and methods. The final keyword prevents inheritance or modification of classes, variables, and methods.

Uploaded by

dawitdabale09
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

CHAPTER 3 Inheritance

Inheritance allows classes to inherit and extend the functionality of other classes. Subclasses inherit all fields and methods of the superclass. Subclasses can override methods from the superclass. Access levels like public, private, and protected determine which classes can access fields and methods. The final keyword prevents inheritance or modification of classes, variables, and methods.

Uploaded by

dawitdabale09
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

CHAPTER THREE

Inheritance

1
What is Inheritance?
 In the real world: We inherit traits from our
mother and father. We also inherit traits from
our grandmother, grandfather, and ancestors.
We might have similar eyes, the same smile,
a different height . . . but we are in many
ways "derived" from our parents.

 In software: Object inheritance is more well


defined! Objects that are derived from other
object "resemble" their parents by inheriting
both state (fields) and behavior (methods).
2
Dog Class
public class Dog {
private String name;
private int fleas;

public Dog(String n, int f) {


name = n;
fleas = f;
}

public String getName() { return name; }

public int getFleas() { return fleas; }

public void speak() {


System.out.println("Woof");
}
} 3
Cat Class
public class Cat {
private String name;
private int hairballs;

public Cat(String n, int h) {


name = n;
hairballs = h;
}

public String getName() { return name; }

public int getHairballs() { return hairballs; }

public void speak() {


System.out.println("Meow");
}
} 4
Problem: Code Duplication

• Dog and Cat have the name field and


the getName method in common

• Classes often have a lot of state and


behavior in common

• Result: lots of duplicate code!

5
Solution: Inheritance
• Inheritance allows you to write new classes
that inherit from existing classes

• The existing class whose properties are


inherited is called the "parent" or superclass

• The new class that inherits from the super


class is called the "child" or subclass

• Result: Lots of code reuse!


6
Dog Cat
String name String name
int fleas int hairballs
String getName() String getName()
int getFleas() int getHairballs()
void speak() void speak()

using
inheritance

superclass
Animal
subclass
String name
subclass String getName()

Dog Cat
int fleas int hairballs
int getFleas() int getHairballs()
void speak() void speak() 7
Animal Superclass
public class Animal {

private String name;

public Animal(String n) {
name = n;
}

public String getName() {


return name;
}
}
8
Dog Subclass
public class Dog extends Animal {

private int fleas;

public Dog(String n, int f) {


super(n); // calls Animal constructor
fleas = f;
}

public int getFleas() {


return fleas;
}

public void speak() {


return System.out.println("Woof");
}
} 9
Cat Subclass
public class Cat extends Animal {

private int hairballs;

public Cat(String n, int h) {


super(n); // calls Animal constructor
hairballs = h;
}

public int getHairballs() {


return hairballs;
}

public void speak() {


return System.out.println("Meow");
}
} 10
Inheritance Quiz 1
• What is the output of the following?

Dog d = new Dog("Rover" 3);


Cat c = new Cat("Kitty", 2);
System.out.println(d.getName() + " has " +
d.getFleas() + " fleas");
System.out.println(c.getName() + " has " +
c.getHairballs() + "
hairballs");

Rover has 3 fleas


Kitty has 2 hairballs
11
(Dog and Cat inherit the getName method from Animal)
Inheritance Rules

• Use the extends keyword to indicate that


one class inherits from another

• The subclass inherits all the fields and


methods of the superclass

• Use the super keyword in the subclass


constructor to call the superclass constructor

12
Subclass Constructor
• The first thing a subclass constructor must do
is call the superclass constructor

• This ensures that the superclass part of the


object is constructed before the subclass part

• If you do not call the superclass constructor


with the super keyword, and the superclass
has a constructor with no arguments, then that
superclass constructor will be called implicitly.
13
Implicit Super Constructor Call
then this Beef subclass:

public class Beef extends Food {


If I have this Food class: private double weight;
public Beef(double w) {
public class Food { weight = w
private boolean raw; }
public Food() { }
raw = true;
} is equivalent to:
}
public class Beef extends Food {
private double weight;
public Beef(double w) {
super();
weight = w
}
} 14
Inheritance Quiz 2
public class A {
public A() { System.out.println("I'm A"); }
}

public class B extends A {


public B() { System.out.println("I'm B"); }
}

public class C extends B {


public C() { System.out.println("I'm C"); }
}

What does this print out? I'm A


I'm B
C x = new C(); I'm C 15
Overriding Methods
• Subclasses can override methods in their superclass
class Therm { class ThermUS extends Therm {
public double celsius;
public ThermUS(double c) {
public Therm(double c) { super(c);
celsius = c; }
}
// degrees in Fahrenheit
public double getTemp() { public double getTemp() {
return celcius; return celsius * 1.8 + 32;
} }
} }
• What is the output of the following?
212
ThermUS thermometer = new ThermUS(100);
System.out.println(thermometer.getTemp());
16
Calling Superclass Methods
• When you override a method, you can call
the superclass's copy of the method by
using the syntax super.method()

class Therm { class ThermUS extends Therm {


private double celsius;
public ThermUS(double c) {
public Therm(double c) { super(c);
celcius = c; }
}
public double getTemp() {
public double getTemp() { return super.getTemp()
return celcius; * 1.8 + 32;
} }
} } 17
Access Level
• Classes can contain fields and methods
of four different access levels:
• private: access only to the class itself

• package: access only to classes in the


same package

• protected: access to classes in the


same package and to all subclasses

• public: access to all classes everywhere


18
Accessibility Summary

Modifier Accessed Accessed Accessed Accessed


on members from the from the from a from a different
in a class same class same package subclass package

public

protected -

default - -

private - - -

19
20
The final Modifier
• The final class cannot be extended:
final class Math {
...
}

• The final variable is a constant:


final static double PI = 3.14159;

• The final method cannot be overridden by


its subclasses.

21
Variable Type vs Object Type
• Variables have the types they are given when
they are declared and objects have the type of
their class.

• For an object to be assigned to a variable is


must be of the same class or a subclass of
the type of the variable.

• You may not call a method on a variable if it's


type does not have that method, even if the
object it references has the method.
22
Which Lines Don't Compile?
public static void main(String[] args) {
Animal a1 = new Animal();
a1.getName();
a1.getFleas(); // Animal does not have getFleas
a1.getHairballs(); // Animal does not have getHairballs
a1.speak(); // Animal does not have speak
Animal a2 = new Dog();
a2.getName();
a2.getFleas(); // Animal does not have getFleas
a2.getHairballs(); // Animal does not have getHairballs
a2.speak(); // Animal does not have speak
Dog d = new Dog();
d.getName();
d.getFleas();
d.getHairballs(); // Dog does not have getHairballs
d.speak();
} 23
Remember Casting?
• "Casting" means "promising" the compiler
that the object will be of a particular type

• You can cast a variable to the type of the


object that it references to use that object's
methods without the compiler complaining.

• The cast will fail if the variable doesn't


reference an object of that type.
24
Which Castings Will Fail?
public static void main(String[] args) {
Animal a1 = new Animal();
((Dog)a1).getFleas(); // a1 is not a Dog
((Cat)a1).getHairballs(); // a1 is not a Cat
((Dog)a1).speak(); // a1 is not a Dog

Animal a2 = new Dog();


((Dog)a2).getFleas();
((Cat)a2).getHairballs(); // a2 is not a Cat
((Dog)a2).speak();

Dog d = new Dog();


((Cat)d).getHairballs(); // d is not a Cat
25
}
Programming Example
• A Company has a list of Employees. It asks you
to provide a payroll sheet for all employees.
– Has extensive data (name, department, pay amount,
…) for all employees.
– Different types of employees – manager, engineer,
software engineer.
– You have an old Employee class but need to add
very different data and methods for managers and
engineers.
• Suppose someone wrote a name system, and already
provided a legacy Employee class. The old Employee class
had a printData() method for each Employee that only
printed the name. We want to reuse it, and print pay info.
26
REVIEW PICTURE
Encapsulation Message passing "Main event loop"

Employee e1 public … Main(…){


printData Employee e1…("Mary","Wang");
...
private:
e1.printData();
lastName // Prints Employee names.
firstName
...
}

27
Employee class
This is a simple super or base class.
class Employee {
// Data
private String firstName, lastName;

// Constructor
public Employee(String fName, String lName) {
firstName= fName; lastName= lName;
}
// Method
public void printData() {
System.out.println(firstName + " " + lastName);}
}

28
Inheritance
Already written:
Class Employee
firstName printData()
lastName

is-a is-a
Class Engineer
Class Manager
firstName firstName
lastName lastName

hoursWorked
salary
printData() wages
getPay()
printData()
getPay()
You next write: 29
Engineer class
Subclass or (directly) derived class
class Engineer extends Employee {
private double wage;
private double hoursWorked;
public Engineer(String fName, String lName,
double rate, double hours) {
super(fName, lName);
wage = rate;
hoursWorked = hours;
}

public double getPay() {


return wage * hoursWorked;
}

public void printData() {


super.printData(); // PRINT NAME
System.out.println("Weekly pay: $" + 30
getPay(); }
Manager class
Subclass or (directly) derived class
class Manager extends Employee {
private double salary;

public Manager(String fName, String lName, double sal){


super(fName, lName);
salary = sal; }

public double getPay() {


return salary; }

public void printData() {


super.printData();
System.out.println("Monthly salary: $" + salary);}
}
31
Inheritance…
Class Manager
firstName
lastName

is-a
Salary
printData
getPay
Class SalesManager
firstName
lastName

Salary
printData
getPay salesBonus
32
SalesManager Class
(Derived class from derived class)
class SalesManager extends Manager {
private double bonus; // Bonus Possible as commission.

// A SalesManager gets a constant salary of $1250.0


public SalesManager(String fName, String lName, double b) {
super(fName, lName, 1250.0);
bonus = b; }

public double getPay() {


return 1250.0; }

public void printData() {


super.printData();
System.out.println("Bonus Pay: $" + bonus; }
}
33
Main method
public class PayRoll {
public static void main(String[] args) {
// Could get Data from tables in a Database.
Engineer fred = new Engineer("Fred", "Smith", 12.0, 8.0);
Manager ann = new Manager("Ann", "Brown", 1500.0);
SalesManager mary= new SalesManager("Mary", "Kate", 2000.0);

// Polymorphism, or late binding


Employee[] employees = new Employee[3]; Java knows the
employees[0]= fred; object type and
employees[1]= ann;
employees[2]= mary;
chooses the
for (int i=0; i < 3; i++) appropriate method
employees[i].printData(); at run time
}
}

34
Output from main method
Fred Smith
Weekly pay: $96.0
Ann Brown
Monthly salary: $1500.0
Mary Kate
Monthly salary: $1250.0
Bonus: $2000.0

Note that we could not write:


employees[i].getPay();
because getPay() is not a method of the superclass Employee.

In contrast, printData() is a method of Employee, so Java can find the


appropriate version.
35
Class Relationships
• To design classes, you need to explore the
relationships among classes.
• The common relationships among classes are:
association,
aggregation,
composition, and
inheritance.

36
Class Relationships
• Association is a general binary relationship
that describes an activity between two classes.
For example,
• A student taking a course is an association
between the Student class and the Course
class, and
• A faculty member teaching a course is an
association between the Faculty class and the
Course class.
37
Class Relationships

• Aggregation is a special form of association that


represents an ownership relationship between two
objects.
• Aggregation models has-a relationships .
• The owner object is called an aggregating object, and
its class is called an aggregating class.
• The subject object is called an aggregated object, and
its class is called an aggregated class.
38
Class Relationships
• An object can be owned by several other aggregating
objects.
• If an object is exclusively owned by an aggregating
object, the relationship between the object and its
aggregating object is referred to as a composition.
• For example, “a student has a name” is a
composition relationship between the Student class
and the Name class, whereas “a student has an
address” is an aggregation relationship between the
Student class and the Address class, since an address
can be shared by several students.
39
Class Relationships
.

40
Common Class Relationships
• Composition
- E.g. A Car has a Wheel
• Inheritance
- E.g. Man is a LivingThing

• A Cat can move Both act as Movables


• A Car can move (common behavior of different
objects)

41
Composition example

An Employee has a birthDate and a hireDate (both objects of type Date)

public class Date public class Employee {


{ private String firstName;
private int month; private String lastName;
private int day; private Date birthDate;
private int year; private Date hireDate;
Date(int month, int day, int public Employee( ){
year) ….
{ }
this.month=month; }
this.day=day;
this.year=year;
}
}

42
Object Class
• All Java classes implicitly inherit from
java.lang.Object

• So every class you write will automatically


have methods in Object such as equals,
hashCode, and toString.

• We'll learn about the importance of some


of these methods in later lectures.
43
Quiz_One

B x = new B( );
A y = new A( );

44

You might also like