oop inheritance
TRANSCRIPT
A Bit About Me….
Computer Consultant, Trainer and Educator
Master of Science – Computer Science
Illinois Institute of Technology
• Artificial Intelligence and Expert Systems
• Current Interest: Machine Learning
Master of Education – Education, Policy, Organization, Leadership
University of Illinois – Urbana-Champaign
• eLearning in Higher Education
• Current Interest: Automated Adaptive Learning Systems
Learning Objectives
• To understand the Object Oriented programming concept - inheritance
• To identify class hierarchies, single and multiple inheritance
Learning Objectives
• To understand the Object Oriented programming concept - inheritance
• To identify class hierarchies, single and multiple inheritance
• To define the syntax of inheritance in Java
Learning Objectives
• To understand the Object Oriented programming concept - inheritance
• To identify class hierarchies, single and multiple inheritance
• To define the syntax of inheritance in Java
• To analyze super and sub classes
Learning Objectives
• To understand the Object Oriented programming concept - inheritance
• To identify class hierarchies, single and multiple inheritance
• To define the syntax of inheritance in Java
• To analyze super and sub classes
• To examine the effect of inheritance on encapsulation and constructors
What is Inheritance?
Inheritance is a relationship between two or more classes where derived classes inherit behaviours and attributes of pre-existing (base) classes.
What is Inheritance?
Inheritance is a relationship between two or more classes where derived classes inherit behaviours and attributes of pre-existing (base) classes.
What are some Benefits of Inheritance?
• Intended to help reuse existing code with little or no modification.
• Intended to help reuse existing code with little or no modification.
What are some Benefits of Inheritance?
• Intended to help reuse existing code with little or no modification.
• Easy to implement real world models
What are some Benefits of Inheritance?
• Intended to help reuse existing code with little or no modification.
• Easy to implement real world models
• Is of a transitive nature
What are some Benefits of Inheritance?
What is a Class Hierarchy?
• The class hierarchy is usually drawn as an inverted (upside-down) tree.
What is a Class Hierarchy?
• The class hierarchy is usually drawn as an inverted (upside-down) tree.
What is a Class Hierarchy?
• The class hierarchy is usually drawn as an inverted (upside-down) tree.
• The tree can have any number of levels.
What is a Class Hierarchy?
• The class hierarchy is usually drawn as an inverted (upside-down) tree.
• The tree can have any number of levels.
• The class at the top (base) of the inverted tree is called the root class.
What is a Class Hierarchy?
• The class hierarchy is usually drawn as an inverted (upside-down) tree.
• The tree can have any number of levels.
• The class at the top (base) of the inverted tree is called the root class.
Root Class
Java Class Hierarchy
• In Java, the root class is called Object.
• Every class is a descendant, direct or indirect, of the Object class.
Java Class Hierarchy
• In Java, the root class is called Object.
• Every class is a descendant, direct or indirect, of the Object class.
• Every class you use or write inherits the instance methods of Object.
Java Class Hierarchy
• In Java, the root class is called Object.
• Every class is a descendant, direct or indirect, of the Object class.
• Every class you use or write inherits the instance methods of Object.
Java Class Hierarchy
• In Java, the root class is called Object.
• Every class is a descendant, direct or indirect, of the Object class.
• Every class you use or write inherits the instance methods of Object.
• The Object class is defined in the java.lang package
What Would You Do?
How would you know what attributes and methods to define for the various levels in the hierarchy?
Inheritance Concepts - Terms
• OOP languages provide specific mechanisms for defining inheritance relationships between classes.
Inheritance Concepts - Terms
• OOP languages provide specific mechanisms for defining inheritance relationships between classes.
• (Derived) (Child) (Sub) Class - a class that inherits characteristics of another class.
Inheritance Concepts - Terms
• OOP languages provide specific mechanisms for defining inheritance relationships between classes.
• (Derived) (Child) (Sub) Class - a class that inherits characteristics of another class.
• (Base) (Parent) (Super) Class - a class from which characteristics are inherited by one or more other classes.
Inheritance Concepts - Terms
• OOP languages provide specific mechanisms for defining inheritance relationships between classes.
• (Derived) (Child) (Sub) Class - a class that inherits characteristics of another class.
• (Base) (Parent) (Super) Class - a class from which characteristics are inherited by one or more other classes.
Inheritance Concepts - Terms
• OOP languages provide specific mechanisms for defining inheritance relationships between classes.
• (Derived) (Child) (Sub) Class - a class that inherits characteristics of another class.
• (Base) (Parent) (Super) Class - a class from which characteristics are inherited by one or more other classes.
• What attributes are inherited?
Inheritance Concepts - Terms
• OOP languages provide specific mechanisms for defining inheritance relationships between classes.
• (Derived) (Child) (Sub) Class - a class that inherits characteristics of another class.
• (Base) (Parent) (Super) Class - a class from which characteristics are inherited by one or more other classes.
• A derived class inherits data and function members from ALL of its base classes.
Examples of Single and Multiple Inheritances
Each Java class has one
(and only one) superclass.
C++ allows for multiple
inheritance
Examples of Single and Multiple Inheritances
Each Java class has one
(and only one) superclass.
C++ allows for multiple
inheritance
Classes higher in the
hierarchy are more
general and more
abstract
Examples of Single and Multiple Inheritances
Each Java class has one
(and only one) superclass.
C++ allows for multiple
inheritance
Classes higher in the
hierarchy are more
general and more
abstract
Classes lower in the
hierarchy are more
specific and concrete
Inheritance can be continuous…Derived class can inherit from a base class
The derived class can act as a base class so another class can inherit from it
44
Inheritance can be continuous…Derived class can inherit from a base class
The derived class can act as a base class so another class can inherit from it
All features of the base class are available in the derived class
45
Inheritance can be continuous…Derived class can inherit from a base class
The derived class can act as a base class so another class can inherit from it
All features of the base class are available in the derived class
46
If you change the base class, all derived classes also change
Inheritance can be continuous…Derived class can inherit from a base class
The derived class can act as a base class so another class can inherit from it
All features of the base class are available in the derived class
47
If you change the base class, all derived classes also changeAny changes in the derived class do not change the base class
Inheritance can be continuous…Derived class can inherit from a base class
The derived class can act as a base class so another class can inherit from it
All features of the base class are available in the derived class
Also, the additional features in the derived class are not available in the base class
48
If you change the base class, all derived classes also changeAny changes in the derived class do not change the base class
Inheritance can be continuous…Derived class can inherit from a base class
The derived class can act as a base class so another class can inherit from it
All features of the base class are available in the derived class
Also, the additional features in the derived class are not available in the base class
49
If you change the base class, all derived classes also changeAny changes in the derived class do not change the base class
What really happens?
The object is created using new
The system allocates enough
memory to hold all its instance
variables.
What really happens?
The object is created using new
The system allocates enough
memory to hold all its instance
variables.This includes any inherited instance
variables
Inheritance and Encapsulation
• Three levels of access control• Public: members (data and methods)
can be used by the class and everybody else (other classes, functions, etc.)
• Protected: members can be accessed by the class (and its friends) and its derived classes
• Private: members can be accessed only by the class (and its friends)
Inheritance and Encapsulation
• Three levels of access control• Public: members (data and methods)
can be used by the class and everybody else (other classes, functions, etc.)
• Protected: members can be accessed by the class (and its friends) and its derived classes
• Private: members can be accessed only by the class (and its friends)
• Remark: without inheritance private and protected are the same
Inheritance and Encapsulation
• private member– Is accessible only via the base class
• public member– Is accessible everywhere (base class, derived class,
other classes)
• protected member– Is accessible by the base class and derived classes
What Are Constructors?
• Classes use constructors to initialize instance variables
• When a subclass object is created, its constructor is called.
What Are Constructors?
• Classes use constructors to initialize instance variables
• When a subclass object is created, its constructor is called
• It is the responsibility of the subclass constructor to invoke the appropriate superclass constructors
What Are Constructors?
• Classes use constructors to initialize instance variables
• When a subclass object is created, its constructor is called
• It is the responsibility of the subclass constructor to invoke the appropriate superclass constructors
• This ensures instance variables defined in the superclass are properlyinitialized
Calling the Super Class Constructor
Superclass constructors can be called using the "super" keywordIt must be the first line of code in the sub class constructor
Calling the Super Class Constructor
Superclass constructors can be called using the "super" keywordIt must be the first line of code in the sub class constructorIf a call to super is not made, the system will automatically attempt to invoke the no-argument constructor of the superclass.
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;
public BankAccount(int anAccountNumber, String aName)
{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}
public class OverdraftAccount extends BankAccount
{
private float overdraftLimit;
public OverdraftAccount(int anAccountNumber, String aName, float aLimit)
{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;
public BankAccount(int anAccountNumber, String aName)
{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}
public class OverdraftAccount extends BankAccount
{
private float overdraftLimit;
public OverdraftAccount(int anAccountNumber, String aName, float aLimit)
{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
What is this?
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;
public BankAccount(int anAccountNumber, String aName)
{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}
public class OverdraftAccount extends BankAccount
{
private float overdraftLimit;
public OverdraftAccount(int anAccountNumber, String aName, float aLimit)
{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
Constructor
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;
public BankAccount(int anAccountNumber, String aName)
{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}
public class OverdraftAccount extends BankAccount
{
private float overdraftLimit;
public OverdraftAccount(int anAccountNumber, String aName, float aLimit)
{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
What is this?
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;
public BankAccount(int anAccountNumber, String aName)
{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}
public class OverdraftAccount extends BankAccount
{
private float overdraftLimit;
public OverdraftAccount(int anAccountNumber, String aName, float aLimit)
{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
extends indicates inheritance
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;
public BankAccount(int anAccountNumber, String aName)
{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}
public class OverdraftAccount extends BankAccount
{
private float overdraftLimit;
public OverdraftAccount(int anAccountNumber, String aName, float aLimit)
{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
What is this?
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;
public BankAccount(int anAccountNumber, String aName)
{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}
public class OverdraftAccount extends BankAccount
{
private float overdraftLimit;
public OverdraftAccount(int anAccountNumber, String aName, float aLimit)
{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
What is this?
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;
public BankAccount(int anAccountNumber, String aName)
{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}
public class OverdraftAccount extends BankAccount
{
private float overdraftLimit;
public OverdraftAccount(int anAccountNumber, String aName, float aLimit)
{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
Subclass Constructor
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;
public BankAccount(int anAccountNumber, String aName)
{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}
public class OverdraftAccount extends BankAccount
{
private float overdraftLimit;
public OverdraftAccount(int anAccountNumber, String aName, float aLimit)
{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
Subclass Constructor
additional parameter
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;
public BankAccount(int anAccountNumber, String aName)
{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}
public class OverdraftAccount extends BankAccount
{
private float overdraftLimit;
public OverdraftAccount(int anAccountNumber, String aName, float aLimit)
{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
What is this?
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;
public BankAccount(int anAccountNumber, String aName)
{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}
public class OverdraftAccount extends BankAccount
{
private float overdraftLimit;
public OverdraftAccount(int anAccountNumber, String aName, float aLimit)
{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
Call to super class constructor
A portion of a Shape class hierarchy.
Shape
TwoDimensionalShape ThreeDimensionalShape
Circle Square Triangle Sphere Cube Tetrahedron
Abstract Classes & Interfaces
Definitions
• abstract methods = Methods that are declared, with no implementation
• abstract class = A class with abstract methods, not meant to be instantiated
• interface = A named collection of method definitions (without implementations)
Examples
• Food is an abstract class. Can you make an instance of food? No, of course not. But you can make an instance of an apple or a steak or a peanut butter cup, which are types of food. Food is the abstract concept; it shouldn’t exist.
• Skills are interfaces. Can you make an instance of a student, an athlete or a chef? No, but you can make an instance of a person, and have that person take on all these skills. Deep down, it’s still a person, but this person can also do other things, like study, sprint and cook.
Abstract and Interface Classes Revisited
Abstract Classes and Interfaces
cannot be instantiated
may contain a mix of methods declared with or without an implementation.
With abstract classes,
you can declare fields that are not static and final
you can define public, protected, and private concrete methods.
With interfaces,
all fields are automatically public, static, and final
all methods that you declare or define (as default methods) are public.
Method Overriding
• Method Overriding is when the subclass modifies the implementation of a method defined in the superclass.
• It can only be done if the superclass method is public.
• It cannot be done if the superclass method is static.
• Runtime polymorphism is essentially referred as method overriding and can only be implemented through inheritance
Overriding vs. Overloading
public class Test {
public static void main(String[] args) {
A a = new A();
a.p(10);
a.p(10.0);
}
}
class B {
public void p(double i) {
System.out.println(i * 2);
}
}
class A extends B {
// This method overrides the method in B
public void p(double i) {
System.out.println(i);
}
}
public class Test {
public static void main(String[] args) {
A a = new A();
a.p(10);
a.p(10.0);
}
}
class B {
public void p(double i) {
System.out.println(i * 2);
}
}
class A extends B {
// This method overloads the method in B
public void p(int i) {
System.out.println(i);
}
}
Output
CIRCLE
(X,Y) Position: (4,4)
Radius: 5
Area: 78.53981633974483
RECTANGLE
(X,Y) Position: (4,4)
Width & Height: 5 10
Area: 50.0
Output
CIRCLE
(X,Y) Position: (4,4)
Radius: 5
Area: 78.53981633974483
RECTANGLE
(X,Y) Position: (4,4)
Width & Height: 5 10
Area: 50.0
Which is from Shape and which is from Circ or Rect?
How to do Multiple Inheritance in Java?
• Java does not support multiple inheritance.• You can achieve partial multiple inheritance with the help of interfaces.
How to do Multiple Inheritance in Java?
• Java does not support multiple inheritance.• You can achieve partial multiple inheritance with the help of interfaces. • You can extend only one class (inheritance) but can implement any number
of interfaces.
How to do Multiple Inheritance in Java?
• Java does not support multiple inheritance.• You can achieve partial multiple inheritance with the help of interfaces. • You can extend only one class (inheritance) but can implement any number
of interfaces.
Example: under the assumption that Car and Automobile are interfaces.
How to do Multiple Inheritance in Java?
• Java does not support multiple inheritance.• You can achieve partial multiple inheritance with the help of interfaces. • You can extend only one class (inheritance) but can implement any number
of interfaces.
Example: under the assumption that Car and Automobile are interfaces.
public class FerrariF12011 extends Ferrari implements Car, Automobile
{
}