قراءة
عرض

Constructors in Subclasses

Constructors are not inherited. That is, if you extend an existing class to make a subclass, the constructors in the superclass do not become a part of the subclass.
If you want constructors in the subclass, you have to define new ones from scratch.
If you don’t define any constructors in the subclass, then the computer will make up a default constructor, with no parameters, for you. This could be a problem, if there is a constructor in the superclass that does a lot of necessary work. It looks like you might have to repeat all that work in the subclass!
The constructor of the super class could be called by involves the special variable, super.
At the beginning statement in a constructor, we should use super to call a constructor from the superclass. The notation for this is a bit ugly and misleading, and it can only be used in this one particular circumstance: It looks like you are calling super as a method (even though super is not a method and you can’t call constructors the same way you call other methods anyway). As an example, assume that the One class has a constructor and consider a subclass Two :

Example1

public class One {
protected int x,y;
public One(int a,int b){
x=a;y=b;
System.out.println("One"+x+" "+y);}
public One() {
System.out.println("One One");}

}


public class Two extends One{
public Two(){
System.out.println("Two");}
}

public class Main {

public static void main(String[] args){

Two t=new Two ( );
} }

The output will be :

One One
Two

If we delete the empty constructor of the One class ….the compiler will detect an error …….why? If we delete both constructors what will happen? Why? If we delete the One(int , int) constructor what will be the output ? why?

If we call the super constructor as shown:

public class Two extends One {
public Two(){
super (3,4);
System.out.println("Two");}

}


The output will be:
One 3 4
Two

The statement “super (3,4);” calls the constructor from the superclass. This call must be the first line of the constructor in the subclass. Note that if you don’t explicitly (واضح) call a constructor from the superclass in this way, then the constructor from the superclass, the one with no parameters, will be called automatically. If there is no constructor with no parameters … the compiler will detect error. Repeat the same cases which discussed previously but with calling super(3,4)…and compare the results of these three cases.

Example2

Another example considers a class that describes an employee for a company. The class should, at the very least, contain some form of reference to an individual employee, such as the name of a person being employed and the name of the department in which the person works. This class also contains the length of service, in years, the employee has worked for the company.

public class Employee {

protected final static float holidayEntitlement = 20.0f;
protected String employeeName;
protected String employeeDept;
protected int lengthOfService;

public Employee(String name, String department, int yearsService) {

employeeName = name; employeeDept = department; lengthOfService = yearsService;}

public String getName(){

return employeeName;}


public String getDepartment(){
return employeeDept;}

public int getLengthOfService() {

return lengthOfService;}

public float getHolidays() {

return holidayEntitlement;} }

Notice that the instance variables employeeName, employeeDept, and

lengthOfService are not declared as private but declared as protected.
Normally, variables are declared as private to prevent access from outside of the class. However, although private variables are inherited by subclass objects (in that each object has its own copy of that variable with its own value), such variables cannot be accessed directly by the subclass objects themselves. They can only be accessed through any protected or public access method of the superclass. A protected variable can be accessed from any method of any class in the same package. Recall that a package is a group of related classes. Thus, protected variables are safe from access from outside a controlled set of classes, yet can still be easily accessed from within the set, that is, from classes in the same package.
The Employee class is given a constructor for initializing the name of the
employee, the name of the department in which the employee works, and the length of time spent working for the company. Instance methods are included that return the name of the employee, the name of the department in which the employee works, the length of service, and the holiday entitlement. Suppose there is a special type of employee, which we will call a technician, who has many of the characteristics of a standard employee but also has some additional characteristics, or a few characteristics that differ from the standard employee.
Another class, Technician, may be defined that inherits all the characteristics of the class Employee.
For example, the statement class Technician extends Employee permits all the variables and methods of the class Employee to be inherited by the class Technician. Failure to use inheritance would mean that all the variables and methods that are common to both an Employee and a Technician would need to be recorded as part of the definition of the class Technician.


Lec 13




public class Technician extends Employee {
protected float holidays;

public Technician(String name, String department, int yearsService) {

super(name, department, yearsService); }}

What has the class Technician inherited from the class Employee?

The answer is that it has inherited the constant holidayEntitlement, the
instance variables employeeName, employeeDept, and lengthOfService, and the instance methods:
getName(), getDepartment(), getLengthOfService, and getHolidays. Inheritance allows both the variables and the instance methods to be used for objects of type Technician despite both the variables and methods not being explicitly defined in this class.
The following figure illustrates a more detailed hierarchy class diagram between the Employee and Technician classes. All the protected constants and instance variables, and public instance methods are inherited by the Technician class.

Lec 13

Example LAB :

The following figure is a class hierarchy of shapes. Shape is a generalized class of Circle and Square. All shapes have a name and a measurement by which the area of the shape is calculated. The attribute name and method getName() are defined as properties of Shape. Circle and Square, being subclasses of Shape, inherit these properties (highlighted in bold in the following figure).use constructors to set the values of attributes.


Lec 13


public class Shape {

private String name;
public Shape(String aName) {name=aName;}
public String getName() {return name;}
}
public class Circle extends Shape{
private int redius;
Circle(String aName) {
super(aName);
radius = 3;
}
public float calculateArea() {
float area;
area = (float) (3.14 * radius * radius);
return area;
}
}

class Square extends Shape {

private int side;
Square(String aName) {
super(aName);
side = 3; }
public float calculateArea() {
int area;
area = side * side;
return area;
} }


Complete the program and execute it in the Lab



رفعت المحاضرة من قبل: HS Alqaicy
المشاهدات: لقد قام 0 عضواً و 116 زائراً بقراءة هذه المحاضرة








تسجيل دخول

أو
عبر الحساب الاعتيادي
الرجاء كتابة البريد الالكتروني بشكل صحيح
الرجاء كتابة كلمة المرور
لست عضواً في موقع محاضراتي؟
اضغط هنا للتسجيل