Subclasses and inheritance
Madhavan Mukund
[Link]
Programming Concepts using Java
Week 3
A Java class
public class Employee{
An Employee class private String name;
private double salary;
// Some Constructors ...
// "mutator" methods
public boolean setName(String s){ ... }
public boolean setSalary(double x){ ... }
// "accessor" methods
public String getName(){ ... }
public double getSalary(){ ... }
// other methods
public double bonus(float percent){
return (percent/100.0)*salary;
}
}
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 2/6
A Java class
public class Employee{
An Employee class private String name;
private double salary;
Two private instance variables
// Some Constructors ...
// "mutator" methods
public boolean setName(String s){ ... }
public boolean setSalary(double x){ ... }
// "accessor" methods
public String getName(){ ... }
public double getSalary(){ ... }
// other methods
public double bonus(float percent){
return (percent/100.0)*salary;
}
}
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 2/6
A Java class
public class Employee{
An Employee class private String name;
private double salary;
Two private instance variables
// Some Constructors ...
Some constructors to set up the
// "mutator" methods
object public boolean setName(String s){ ... }
public boolean setSalary(double x){ ... }
// "accessor" methods
public String getName(){ ... }
public double getSalary(){ ... }
// other methods
public double bonus(float percent){
return (percent/100.0)*salary;
}
}
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 2/6
A Java class
public class Employee{
An Employee class private String name;
private double salary;
Two private instance variables
// Some Constructors ...
Some constructors to set up the
// "mutator" methods
object public boolean setName(String s){ ... }
public boolean setSalary(double x){ ... }
Accessor and mutator methods to set
instance variables // "accessor" methods
public String getName(){ ... }
public double getSalary(){ ... }
// other methods
public double bonus(float percent){
return (percent/100.0)*salary;
}
}
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 2/6
A Java class
public class Employee{
An Employee class private String name;
private double salary;
Two private instance variables
// Some Constructors ...
Some constructors to set up the
// "mutator" methods
object public boolean setName(String s){ ... }
public boolean setSalary(double x){ ... }
Accessor and mutator methods to set
instance variables // "accessor" methods
public String getName(){ ... }
A public method to compute bonus public double getSalary(){ ... }
// other methods
public double bonus(float percent){
return (percent/100.0)*salary;
}
}
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 2/6
Subclasses
Managers are special types of employees with extra features
public class Manager extends Employee{
private String secretary;
public boolean setSecretary(name s){ ... }
public String getSecretary(){ ... }
}
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 3/6
Subclasses
Managers are special types of employees with extra features
public class Manager extends Employee{
private String secretary;
public boolean setSecretary(name s){ ... }
public String getSecretary(){ ... }
}
Manager objects inherit other fields and methods from Employee
Every Manager has a name, salary and methods to access and manipulate these.
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 3/6
Subclasses
Managers are special types of employees with extra features
public class Manager extends Employee{
private String secretary;
public boolean setSecretary(name s){ ... }
public String getSecretary(){ ... }
}
Manager objects inherit other fields and methods from Employee
Every Manager has a name, salary and methods to access and manipulate these.
Manager is a subclass of Employee
Think of subset
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 3/6
Subclasses
Manager objects do not
automatically have access to private
data of parent class.
Common to extend a parent class
written by someone else
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 4/6
Subclasses
Manager objects do not
automatically have access to private
data of parent class.
Common to extend a parent class
written by someone else
How can a constructor for Manager
set instance variables that are private
to Employee?
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 4/6
Subclasses
Manager objects do not public class Employee{
automatically have access to private ...
public Employee(String n, double s){
data of parent class. name = n; salary = s;
Common to extend a parent class }
written by someone else public Employee(String n){
this(n,500.00);
How can a constructor for Manager }
set instance variables that are private }
to Employee?
Some constructors for Employee
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 4/6
Subclasses
Manager objects do not public class Employee{
automatically have access to private ...
public Employee(String n, double s){
data of parent class. name = n; salary = s;
Common to extend a parent class }
written by someone else public Employee(String n){
this(n,500.00);
How can a constructor for Manager }
set instance variables that are private }
to Employee?
Some constructors for Employee
Use parent class’s constructor using
super
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 4/6
Subclasses
Manager objects do not public class Employee{
automatically have access to private ...
public Employee(String n, double s){
data of parent class. name = n; salary = s;
Common to extend a parent class }
written by someone else public Employee(String n){
this(n,500.00);
How can a constructor for Manager }
set instance variables that are private }
to Employee?
public class Manager extends Employee{
Some constructors for Employee ..
public Manager(String n, double s, String sn){
Use parent class’s constructor using super(n,s); /* super calls
super Employee constructor */
secretary = sn;
A constructor for Manager }
}
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 4/6
Inheritance
In general, subclass has more features
than parent class
Subclass inherits instance variables,
methods from parent class
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 5/6
Inheritance
In general, subclass has more features
than parent class
Subclass inherits instance variables,
methods from parent class
Every Manager is an Employee, but not
vice versa!
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 5/6
Inheritance
In general, subclass has more features
than parent class
Subclass inherits instance variables,
methods from parent class
Every Manager is an Employee, but not
vice versa!
Can use a subclass in place of a
superclass
Employee e = new Manager(...)
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 5/6
Inheritance
In general, subclass has more features
than parent class
Subclass inherits instance variables,
methods from parent class
Every Manager is an Employee, but not
vice versa!
Can use a subclass in place of a
superclass
Employee e = new Manager(...)
But the following will not work
Manager m = new Employee(...)
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 5/6
Inheritance
In general, subclass has more features Recall
than parent class int[] a = new int[100];
Subclass inherits instance variables, Why the seemingly redundant
methods from parent class reference to int in new?
Every Manager is an Employee, but not
vice versa!
Can use a subclass in place of a
superclass
Employee e = new Manager(...)
But the following will not work
Manager m = new Employee(...)
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 5/6
Inheritance
In general, subclass has more features Recall
than parent class int[] a = new int[100];
Subclass inherits instance variables, Why the seemingly redundant
methods from parent class reference to int in new?
Every Manager is an Employee, but not One can now presumably write
vice versa! Employee[] e = new Manager(...)[100]
Can use a subclass in place of a
superclass
Employee e = new Manager(...)
But the following will not work
Manager m = new Employee(...)
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 5/6
Summary
A subclass extends a parent class
Subclass inherits instance variables and methods from the parent class
Subclass can add more instance variables and methods
Can also override methods — later
Subclasses cannot see private components of parent class
Use super to access constructor of parent class
Madhavan Mukund Subclasses and inheritance Programming Concepts using Java 6/6