Professional Documents
Culture Documents
GraduateStudent UndergraduateStudent
Student
+ NUM_OF_TESTS
# name
# test
# courseGrade
UndergraduateStudent GraduateStudent
0 1 2 3 4 36 37 38
39
…..
public Sub () {
public_Sub_Field = 100;
protected_Sub_Field = 200;
private_Sub_Field = 300;
} ………………
}
Super :Super
This shows the
inherited
Public Public
components of
Protected Protected the supperclass
Private Private are part of the
subclass instance
:Sub
:Super
Sub
Public
Public
Protected
Protected
Private
Private
Public
Protected
Class hierarchy Private Instances
You already know the difference between the public
can private modifiers. A public member is accessible to
any method, but a private member is accessible only to
the method that belong to the same class.
class Client {
public void test () {
Super mySuper = new Super ();
Sub mySub = new Sub ();
int i = mySuper.public_Super_Field;
VALID int j = mySub.public_Super_Field;
int k = mySub.public_Sub_Field; }
}
int l = mySuper.private_Super_Field;
NOT
int m = mySub.private_Sub_Field;
VALID
:Super
int n = mySub.private_Sub_Field;
Public
Protected
:Client Private
:Sub
:Super
Public
Protected
Only public members, those defined
For the class and those inherited, are Private
Visible from outside. All else is hidden
From outside. Public
Protected
Private
A protected member is accessible only to the methods
that belong to the same class or to the descendant
classes. It is inaccessible to the methods of an
unrelated class.
:Sub
The difference between public,
Private, and Protected modifiers. :Super
Public
Everything except the private
Public Protected
members of the Super class is
visible from a method of the Sub Protected Private
class. Private
Method
13.4 Inheritance and Constructors
In this section, we explain how the constructors of a
class affected by inheritance. Unlike members of a
superclass, constructors of a superclass are not
inherited by its subclasses. This means that you must
define a constructor for a class or use the default
constructor added by the compiler. As we mentioned
in Chapter 4, a default constructor is added to a class if
you do not declare any constructor for the class. A class
definition such as
class Person {
public Person ( ) {
Automatically
This statement calls the
added to the class super ( );
superclass’s constructor
by the compiler
}
public void sayHello ( ) {
System.out.println (“Well, hello”);
}
}
The statement super (); calls the superclass’s
constructor. Every class has a superclass. If the class
declaration does not explicitly designate the superclass
with the extends clause. Then the class’s superclass in
the Object class.
If you declare a constructor, then no default constructor
is added to the class. For example, if you define a class
as
class MyClass {
public MyClass ( int x ) {
…………………….. }
}
Invalid because
then a statement such as MyClass has no
MyClass test = new MyClass(); matching constructor
If the constructor you define does not contain an
explicit call to a superclass constructor, then the
compiler adds the statement super ( ) as the first
statement of the constructor. For example
class MyClass {
private int myInt ;
public MyClass ( ) {
myInt = 10; } public MyCalss ( ) {
super ( );
} myInt = 10;
}