Uploaded by Yazeed Bassam

chap 4 Inheritance

advertisement
CHAPTER 4: INHERITANCE
OUTLINE
•
Create and interact with objects that inherit from other objects
•
Create subclasses
•
Define reusable classes based on inheritance and
•
Define methods, using the protected modifier
INHERITANCE
•
Inheritance is an object-oriented mechanism that derives a new class from an
existing class.
•
The Java programming language allows a class to extend only one other class.
This restriction is referred to as single inheritance (not like C++ who support multiple
inheritance).
•
inheritance allows defining a class in terms of a previously defined class. In the
Java programming language, this can be achieved by the keyword extends
INHERITANCE
public class Employee {
public String name = "";
public double salary;
public Date birthDate;
public String getDetails() {...}
}
•
public class Manager {
public String name = "";
public double salary;
public Date birthDate;
public String department;
public String getDetails() {...}
}
INHERITANCE
The Manager class is created as a subclass of the Employee class.
The Manager class is defined to have all the variables and methods
that an Employee object has.
public class Manager extends Employee {
public String department;
}
INHERITANCE
• Inheritance is appropriate where an "is a"
relationship exists between two classes.
• one of the classes is a specialized version of the
other.
– A manager is an employee
– A taxi is a car
– An Undergraduate Student is a Student
– A Graduate Student is a Student
INHERITANCE
INHERITANCE
• Before beginning the specialization process, it is essential to
determine precisely what will be inherited from the parent class.
• Use the access control rules to identify the attributes and methods
that the subclass will inherit from the parent class.
• The key factors that determine which attributes and methods are
inherited are the access control modifiers public, protected,
private, default access, and the packages to which the parent
class and the child class belong:
INHERITANCE: THE PROTECTED MODIFIER
• The modifier protected makes a data member or method visible
and accessible to the instances of the class and the descendant
classes.
• Public data members and methods are accessible to everyone.
• Private data members and methods are accessible only to
instances of the class.
INHERITANCE: THE PROTECTED MODIFIER
We use the following visual representation of inheritance to illustrate
data member accessibility.
Instances
This shows the inherited
components of the
superclass are part of
the subclass instance
Class Hierarchy
INHERITANCE : THE PROTECTED MODIFIER
INHERITANCE : THE PROTECTED MODIFIER
THE PROTECTED MODIFIER
 The protected modifier can be applied on data
and methods in a class. A protected data or a
protected method in a public class can be
accessed by any class in the same package or its
subclasses, even if the subclasses are in a different
package.
 private, default, protected, public
Visibility increases
private, none (if no modifier is used), protected, public
13
ACCESSIBILITY SUMMARY
Modifier
on members
in a class
Accessed
from the
same class
Accessed
from the
same package
Accessed
from a
subclass
Accessed
from a different
package
public
-
protected
default
private
-
-
-
-
-
14
VISIBILITY MODIFIERS
package p1;
public class C1 {
public int x;
protected int y;
int z;
private int u;
public class C2 {
C1 o = new C1();
can access o.x;
can access o.y;
can access o.z;
cannot access o.u;
protected void m() {
}
}
can invoke o.m();
}
package p2;
public class C3
extends C1 {
can access x;
can access y;
can access z;
cannot access u;
public class C4
extends C1 {
can access x;
can access y;
cannot access z;
cannot access u;
can invoke m();
}
public class C5 {
C1 o = new C1();
can access o.x;
cannot access o.y;
cannot access o.z;
cannot access o.u;
can invoke m();
}
cannot invoke o.m();
}
15
INHERITANCE : CONSTRUCTOR
• Unlike members of a superclass, constructors of a
superclass are not inherited by its subclasses.
• You must define a constructor for a class or use the default
constructor added by the compiler.
• The statement
Super () : calls the superclass’s constructor.
The super keyword in Java is a reference variable which is used to
refer immediate parent class object.
Usage of Java super Keyword
super can be used to refer immediate parent class instance variable.
super can be used to invoke immediate parent class method.
super() can be used to invoke immediate parent class constructor.
INHERITANCE : CONSTRUCTOR
•
If the subclass constructor does not call the constructor of the superclass, then
the default behavior, is to always call (implicit call) the constructor with no
arguments of the parent class.
•
If the parent class does not define a constructor as having no arguments, then
the subclass will not compile
•
The invocation of the parent class constructor occurs before the first executable
line of the subclass constructor.
public A() {
}
public A(double d) {
// some statements
}
public A() {
super();
}
is equivalent to
is equivalent to
public A(double d) {
super();
// some statements
}
INHERITANCE : CONSTRUCTOR IMPLICIT CALL
public class Employee {
•
String name;
public Employee() {
name = "unallocated";
}
public Employee(String n) {
name = n;
}
}
•
public class Manager extends Employee {
•
String department;
public Manager(String s, String d) {
// Will call no-arg parent constructor Employee()
// Inherited attribute name is set to "unallocated"
department = d;
}
INHERITANCE: CONSTRUCTOR IMPLICIT CALL
public class Employee {
•
String name;
public Employee(String n) {
name = n; }
}
public class Manager extends Employee {
•
String department;
public Manager(String s, String d) {
// Will call no-arg parent constructor Employee()
// here, we will get a compile error.
department = d;
}
}
INHERITANCE : CONSTRUCTOR IMPLICIT CALL
•
public class Employee {
String name;
}
•
public class Manager extends Employee {
String department;
public Manager( String d) {
// Will call no-arg parent constructor Employee()
// here, the default constructor of Employee is called.
department = d;
}}
INHERITANCE
•
public class Employee {
protected
String name;
public Employee() {
name = "unallocated"; }
public Employee(String n) { name = n; }}
•
public class Manager extends Employee {
String department;
public Manager(String s, String d) {
// Will call no-arg parent constructor Employee()
// Inherited attribute name is set to "unallocated"
name = s;
department = d; }
INHERITANCE : MANAGEMENT OF PARENT CLASS CONSTRUCTOR
INVOCATION
•
We can explicitly invoke a particular parent class constructor as part of a child
class initialization by using the keyword super from the child constructor's first
line.
•
The super keyword refers to the superclass of the class in which the keyword is
used.
•
We can use any number of appropriate arguments by using the keyword super
to call the appropriate constructor of the parent class.
•
If there is no such parent constructor, a compile error occurs.
INHERITANCE : MANAGEMENT OF PARENT CLASS CONSTRUCTOR
INVOCATION
•
public class Employee {
private String name;
public Employee() {name = "unallocated";
}
public Employee(String n) { name = n;}
}
•
public class Manager extends Employee {
String department;
public Manager(String s, String d) {
super(s); // Call specific parent constructor with string argument
department = d;
}
}
INHERITANCE : MANAGEMENT OF PARENT CLASS CONSTRUCTOR
INVOCATION
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); }
public Faculty()
{ System.out.println("(?) Faculty's no-arg constructor is invoked");}}
class Employee extends Person {
public Employee() {
this("(?) Invoke Employee’s overloaded constructor");
System.out.println("(?) Employee's no-arg constructor is invoked");}
public Employee(String s) { System.out.println(s);
} }
class Person {
public Person() {
System.out.println("(?) Person's no-arg constructor is invoked");
} }
INHERITANCE : MANAGEMENT OF PARENT CLASS CONSTRUCTOR
INVOCATION
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); }
public Faculty()
1. Start from the
main method
{ System.out.println("(4) Faculty's no-arg constructor is invoked");}}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");}
public Employee(String s) { System.out.println(s);
} }
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
} }
INHERITANCE : MANAGEMENT OF PARENT CLASS CONSTRUCTOR
INVOCATION
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); }
public Faculty()
2. Invoke Faculty
constructor
{ System.out.println("(4) Faculty's no-arg constructor is invoked");}}
class Employee extends Person {
public Employee() {
3. Invoke Employee’s noarg constructor
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");}
public Employee(String s) { System.out.println(s);
} }
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
} }
INHERITANCE : MANAGEMENT OF PARENT CLASS CONSTRUCTOR
INVOCATION
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); }
public Faculty()
{ System.out.println("(4) Faculty's no-arg constructor is invoked");}}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");}
public Employee(String s) { System.out.println(s);
} }
class Person {
5. Invoke Person()
constructor
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
} }
INHERITANCE : MANAGEMENT OF PARENT CLASS CONSTRUCTOR
INVOCATION
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); }
public Faculty()
{ System.out.println("(4) Faculty's no-arg constructor is invoked");}}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");}
public Employee(String s) { System.out.println(s);
} }
class Person {
6. Execute println
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
} }
INHERITANCE : MANAGEMENT OF PARENT CLASS CONSTRUCTOR
INVOCATION
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); }
public Faculty()
{ System.out.println("(4) Faculty's no-arg constructor is invoked");}}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");}
public Employee(String s) { System.out.println(s);
} }
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
} }
7. Execute println
INHERITANCE : MANAGEMENT OF PARENT CLASS CONSTRUCTOR
INVOCATION
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); }
public Faculty()
{ System.out.println("(4) Faculty's no-arg constructor is invoked");}}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");}
public Employee(String s) { System.out.println(s);
} }
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
} }
8. Execute println
INHERITANCE : MANAGEMENT OF PARENT CLASS CONSTRUCTOR
INVOCATION
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); }
public Faculty()
{ System.out.println("(4) Faculty's no-arg constructor is invoked");}}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");}
public Employee(String s) { System.out.println(s);
} }
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
} }
9. Execute println
run:
(1) Person's no-arg constructor is invoked
(2) Invoke Employee’s overloaded constructor
(3) Employee's no-arg constructor is invoked
(4) Faculty's no-arg constructor is invoked
BUILD SUCCESSFUL (total time: 0 seconds)
EXAMPLE ON THE IMPACT OF A SUPERCLASS
WITHOUT NO-ARG CONSTRUCTOR
Find out the errors in the program:
public class Apple extends Fruit {
}
class Fruit {
public Fruit(String name) {
System.out.println("Fruit's constructor is invoked");
}
}
33
INHERITANCE: OVERRIDING METHODS
• When a subclass inherits a method, it inherits the interface of the
method as well as the implementation of the method from its
parent class.
– For some methods, the parent implementation might not be appropriate for
the subclass.
• A method of a new class that has the name, return type, and
arguments exactly as a method in the parent class, then the new
method is said to override the old one.
INHERITANCE: OVERRIDING VS. OVERLOADING
public class Test {
public static void main(String[] args) {
A a = new A();
a.p(10);
a.p(10.0);
}
}
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 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);
}
}
class A extends B {
// This method overloads the method in B
public void p(int i) {
System.out.println(i);
}
}
35
A SUBCLASS CANNOT WEAKEN THE
ACCESSIBILITY
A subclass may override a protected
method in its superclass and change its
visibility to public. However, a subclass
cannot weaken the accessibility of a
method defined in the superclass. For
example, if a method is defined as public
in the superclass, it must be defined as
public in the subclass.
36
Download