Mala Gupta OCA Java SE7 Cert QA - Chapter 6 - Working with Inheritance

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Example of casting an interface type to a class type that implements the interface so that the interface is able to access methods from the class that it normally would not be able to

((HRExecutive)interviewer).specialization = new String[] {"Staffing"}; HRExecutive - class interviewer - interface reference variable Casting is another method of telling Java: "Look, I know that the actual object being referred to is HRExecutive, even though I'm using a reference variable of type Interviewer."

What objects can a reference variable point, assuming that not all objects the reference variable points to will be a type of its own?

1. Its own type - An object of a class HRExecutive can be referred to using an object reference variable of type HRExecutive 2. Its base class - If the class HRExecutive inherits the class Employee, an object of the class HRExecutive can be refered to using a variable of type Employee. If the class Employee inherits the class Person, an object of the class HRExecutive can also be reffed to using a variable of type Person 3. Implemented interfaces - If the class HRExecutive implements the interface Interviewer, an object of the class HRExecutive can be referred using a variable of type Interviewer.

What is a base class?

A class inherited by another class.

What is a derived class?

A class that inherits from another class.

Besides inheriting properties and behaviors of the superclass, what else is a derived class able to do in terms of its own definition?

A derived class can also define additional properties and behaviors.

What is a "IS-A" relationship?

A relationship shared by base and derived classes. Because a derived class represents a specialized type of a base class, a derived IS-A class is kind of base class.

When do you instantiate an abstract class?

Abstract classes are not instantiated.

What does a derived class actually inherit when it extends the superclass?

All non-private members

Why must an abstract class have only abstract methods?

An abstract class doesn't have to have just abstract methods. It can define abstract or non-abstract methods. A concrete class, however, cannot have a single abstract method. If the concrete class has one, the method needs to be removed, made non-abstract, or the class needs to be made into an abstract class.

What is an interface able to define?

An interface can define only abstract methods and constants.

What is a parent class?

Another name for a base class

What is a super class?

Another name for a base class

What is an extended class?

Another name for a derived class

What is a child class?

Another name for a derived class.

What is a subclass?

Another name for derived class.

Why are you unable to extend multiple classes from one class in Java?

Because a derived class may inherit different implementations for the same method signature from multiple base classes, multiple inheritance is not allowed in Java

How would you call the default constructor with the "this" keyword?

By using "this()": class Employee { ----String name;//1 ----String address;//1 ----Employee() {//2 --------name = "NoName"; --------address = "NoAddress"; ----} ----Employee(String name, String address) {//3 --------this();//4 --------if (name != null) this.name = name;//5 --------if (address != null) this.address = address;//5 ----} } 1. Instance variables are name and address 2. Constructor that doesn't accept any arguments 3. Constructor that accepts name and address 4. Call constructor that doesn't accept any arguments. Must be the first statement in this method. 5. Assigns value of not null method parameters

What is casting?

Casting is the process of forcefully making a variable behave as a variable of another type.

What are the access modifiers that allow a subclass to inherit members from the superclass?

Default - accessible within the same package protected - accessible to all derived classes, regardless of the packages in which the base and derived classes are defined. public - visible to all other classes.

If a class shares a(n)____ or _____ relationship with another class or interface, their variables can be cast to each others type.

IS-A, inheritance

What makes code extensible?

Inheritance

Exam Tip: Inheritance

Inheritance enables you to reuse code that has already been defined by a class. Inheritance can be implemented by extending the class.

If the keyword "this" is used to call a constructor from another constructor, where must it be located?

Inside the constructor, and it must be the first statement.

Where must the call to a superclasses constructor using the "super()" keyword be located?

Inside the derived classes constructor, and it must be the first statement. Otherwise, a call to super();(the no-arg constructor) is inserted automatically by the compiler.

What does the "super" reference/keyword do?

It refers to the superclass of a class. The variable reference super can be used to access a variable or method from the base class if there's a clash between these names. A common example occurs when a derived class defines variables and methods with the same name as the base class.

Can an interface implement a class?

No

Can you create objects of an abstract class?

No

Can you extend multiple classes from one class in Java?

No

When using a reference variable that points to an object of a different type, is the reference variable able to access all of that objects methods?

No, the only objects that a reference variable that is pointing to a different object is able to use are methods that are defined in the reference variables type. Employee emp = new HRExecutive();//1 1. emp can only access methods in the HRExecutive object that are also defined in the Employee class, which is the type of emp.

Can you use a reference variable of a subclass to refer to the superclass?

No. Because all members of a derived class can't be accessed using an object of the base class, it isn't allowed.

What variable types are can you declare in an interface?

Only constants

When declaring a reference variable of an interface type that points to an object that implements the interface, what can the interface access from the object?

Only methods that are accessible from the object to the interface are implemented methods that are also defined in the interface. Any methods wrote specifically in the class that is implementing the interface cannot be accessed.

What makes polymorphism useful?

The ability of an object to behave in its own specific manner when the same action is passed to it.

What can an abstract class force a class that inherits from it to do?

The abstract class forces its concrete subclasses to implement methods that are defined in the abstract class.

What must the first concrete class that implements a hierarchy of abstract classes do?

The concrete class must supply method implementations for all unique abstract methods within the entirety of the abstract hierarchy.

What is "extends"?

The keyword used by a class to inherit another class and by an interface to inherit another interface.

What is "implements"?

The keyword used by class to implement an interface

Can the keywords "super" and "this" be used in static methods?

The keywords super and this are implicit object references. Because static methods belong to a class, not to objects of a class, you can't use this and super in static methods: class Employee { ----String name;//1 } class Programmer extends Employee { ----String name;//2 ----static void setNames() { --------this.name = "Programmer";//3 --------super.name = "Employee";//4 ----} } 1. Instance variable-name, inEmployee 2. Instance variable-name, in Programmer 3. Won't compile - can't use "this" in static method 4. Won't compile - can't use "super" in static method

Exam Tip: Interface Method Signatures

The method signatures of a method defined in an interface and the classes that implement the interface must match, or the classes won't compile

Exam Note: Interchangeable Names

The terms base class, superclass, and parent class are used interchangeably. Similarly, the terms derived class and subclass are also used interchangeably.

What does the "this" reference/keyword do?

The this reference always point to an object's own instance.

Assuming this code: HRExecutive hr = new Interviewer(); with HRExecutive being a class and Interviewer being an interface, what methods can the reference variable hr access?

This code will not work. Being that Interviewer is an interface, you cannot create an object, or instantiate, an interface. Therefore, the code will not compile

What is a common reason to use the "this" keyword in a method?

To differentiate between two variable names that are the same: class Employee{ ----String name; ----Employee(String name) --------this.name = name;//1 } 1. The "this.name" aspect is referring to the instance variable "name". The "name" aspect is referring to the method parameter variable "name". Using "this" will make sure that its understood that the instance variable is being referred to. If "this" wasn't used, the local variable would take precedence.

What is basic polymorphism definition?

When class inherits another class and both the base and the derived classes define methods with the same method signature(the same method name and method parameters).

Why are you able to implement multiple interfaces but not extend multiple classes?

When implementing interfaces, the methods defined within are bare-bone. Assuming that 2 interfaces that you implement have the exact same method signature, implementing it once in a concrete class will satisfy both implementation contracts from the interface and not creating any conflicts.

Are you able to use the "this" reference/keyword to refer to all methods and variables that are accessible to a class?

Yes

Can a concrete class extend an abstract class?

Yes

Can a derived class have static variables?

Yes

Can an abstract class extend a concrete class?

Yes

Can an abstract class extend another abstract class?

Yes

Can an abstract class implement interfaces?

Yes

Can an interface extend multiple interfaces?

Yes

Can any object use the "this" reference/keyword to refer to its own instance?

Yes

Can derived classes define their own constructors?

Yes

Can derived classes have static methods?

Yes

Can you use a reference variable of an interface to refer to an object of a class that implements it?

Yes

Can you use a reference variable to refer to an object of its derived class?

Yes

Can you use variables of an abstract base class to refer to objects of its derived class?

Yes

In certain circumstances, can the type of the reference variable and the actual type of the object be different?

Yes

Do the variables of an interface have an implicit aspect to their declaration?

Yes, all variables declared in an interface are implicitly public, final, and abstract: interface MyInterface{ int age = 10 //1 public static final int AGE = 10;//2 } 1. The declaration here is identical to the definition for line 2, except for the variable identifier, which was used to show the "constant" look.

Can an interface inherit an interface?

Yes, an interface can inherit zero or more interfaces.

Is there anyway around the fact that you are not able to access methods from a subclass or interface that are not defined in the subclass or interface?

Yes, by casting a reference variable of a base class or an interface to the exact type of the object they refer to.

Can you implement multiple interfaces in Java?

Yes.

Must a derived class implement all the abstract methods of its parent class, assuming the parent class is abstract?

Yes. A derived class should implement all the abstract methods of its base class. If it doesn't, it must be defined as an abstract derived class.

Does a change in the signature of a method in an interface have any impact on the definition of this method in the classes that implement it?

Yes. If the signature of a method is changed in an interface, all classes that implement the interface will fail to compile.

Exam Tip: Reference Variables

You may see multiple questions in the exam that try to assign an object of a base class to a reference variable of a derived class. Note that a derived class can be referred to using a reference variable of its base class. The reverse is not allowed and won't compile.

Example of accessing inherited members of an inherited class.

class Employee { ----String name; ----String address; ----String phoneNumber; ----float experience; } class Manager extends Employee { ----int teamSize; ----void reportProjectStatus() {} } class Programmer extends Employee { ----String[] programmingLanguages; ----void writeCode() {} ----void accessBaseClassMembers() { --------name = "Programmer";//1 ----} } 1. Derived class Programmer can directly access members of its base class.

Code demonstrating using a derived class reference to point to an object of the same derived class and using the reference variable to access interface and superclass variables.

class Employee { ----String name; ----String address; ----String phoneNumber; ----float experience; } interface Interviewer { ----public void conductInterview(); } class HRExecutive extends Employee implements Interviewer {//1 ----String[] specialization; ----public void conductInterview() { ---------System.out.println("HRExecutive - conducting interview"); ----} } class Office { ----public static void main(String args[]) { --------HRExecutive hr = new HRExecutive();//2 ----} } ------------------------------------------------------------ ------------------------------------------------------------ class Office { ----public static void main(String args[]) { --------HRExecutive hr = new HRExecutive(); --------hr.specialization = new String[] {"Staffing"};//3 --------System.out.println(hr.specialization[0]);//3 --------hr.name = "Pavni Gupta";//4 --------System.out.println(hr.name);//4 --------hr.conductInterview();//5 ----} } 1. Class HRExecutive inherits class Employee and implements interface Interview 2. A variable of type HRExecutive can be used to refer to its object 3. Access variable defined in class HRExecutive 4. Access variable defined in class Employee 5. Access method defined in interface Interviewer

Example of using the "this" keyword

class Employee { ----String name; } class Programmer extends Employee { ----void accessEmployeeVariables() { --------name = "Programmer";//1 ----} } class Programmer extends Employee { ----void accessEmployeeVariables() { --------this.name = "Programmer";//2 ----} } 1. This statement is identical to the statement on line 2.

Example of using "this" with constructors

class Employee { ----String name;//1 ----String address;//1 ----Employee(String name) {//2 --------this.name = name; ----} ----Employee(String name, String address) {//3 --------this(name);//4 --------this.address = address;//5 ----} } 1. Instance variables are name and address 2. Constructor that accepts only name 3. Constructor that accepts name and address. 4. Calls constructor that accepts only name 5. Assigns value of method parameter address to instance variable

Using "super" to access constructors of a base class

class Employee { ----String name;//1 ----String address;//1 ----Employee(String name, String address) {//2 --------this.name = name; --------this.address = address; ----} } class Programmer extends Employee { ----String progLanguage;//3 ----Programmer(String name, String address, String//4 progLang) { --------super(name, address);//5 --------this.progLanguage = progLang; ----} } 1. Instance variables - name and address 2. Constructor that accepts name and address 3. Instance variable - progLanguage 4. Constructor that accepts values for super class variables also 5. Calls Employee constructor - This code calls the superclass constructor by passing it the reference variables, name and address, which it accepts itself

Example of using the "super" keyword to access a method that is also in the subclass to avoid name clashing

class Employee { ----String name;//1 } class Programmer extends Employee { ----String name;//2 ----void setNames() { ---------this.name = "Programmer";//3 ---------super.name = "Employee";//4 ----} ----void printNames() { --------System.out.println(super.name);//5 --------System.out.println(this.name);//6 ----} } class UsingThisAndSuper { ----public static void main(String[] args) { --------Programmer programmer = new Programmer();//7 --------programmer.setNames(); --------programmer.printNames(); ----} } Output: Employee Programmer 1. Instance variable - name, in Employee 2. Instance variable - name, in Programmer 3. Assign value to instance variable-name, defined in Programmer 4. Assign value to instance variable-name, defined in Employee 5. Print value of instance variable-name, defined in Employee 6. Print value of instance variable-name, defined in Programmer 7. Create an object of class Programmer

Example of Polymorphism

class Manager extends Employee { ----public void startProjectWork() { --------meetingWithCustomer(); --------defineProjectSchedule(); --------assignRespToTeam(); ----} ----private void meetingWithCustomer() { --------System.out.println("meet Customer"); ----} ----private void defineProjectSchedule() { --------System.out.println("Project Schedule"); ----} ----private void assignRespToTeam() { --------System.out.println("team work starts"); ----} } class PolymorphismWithClasses { ----public static void main(String[] args) { ---------Employee emp1 = new Programmer();//1 ---------Employee emp2 = new Manager();//2 ---------emp1.reachOffice();//3 ---------emp2.reachOffice();//3 ---------emp1.startProjectWork();//4 ---------emp2.startProjectWork()//5 ----} } Output: reached office - Guragaon, India reached office - Guragaon, India define classes unit test code meet Customer Project Schedule team work starts 1. emp1 refers to Programmer 2. emp2 refers to Manager 3. No confusion here because reachOffice is defined only in class Employee 4. Method from Programmer 5. Method from Programmer

Example of using a reference variable of the base class to access an object of a derived class.

class Office { ----public static void main(String args[]) { --------Employee emp = new HRExecutive();//1 ----} } class Office { ----public static void main(String args[]) { --------Employee emp = new HRExecutive();//2 --------emp.specialization = new String[] {"Staffing"};//3 --------System.out.println(emp.specialization[0]);//3 --------emp.name = "Pavni Gupta";//4 --------System.out.println(emp.name);//4 --------emp.conductInterview();//5 ----} } 1. Variable of type Employee can also be used to refer to an object of class HRExecutive because class HRExecutive extends Employee. 2. Type or variable emp is Employee 3. Variable emp can't access member specialization defined in class HRExecutive. This causes a compiler error. 4. Variable emp can access member name defined in class Employee 5. Variable emp can't access method conductInterview() defined in interface Interviewer The reference variable emp can see only the Employee object. Hence, it can access only the variables and methods defined in the class Employee

Example of casting in code

class OfficeWhyCasting { ----public static void main(String args[]) { --------Interviewer[] interviewers = new Interviewer[2];//1 --------interviewers[0] = new Manager();//2 --------interviewers[1] = new HRExecutive();//3 --------for (Interviewer interviewer : interviewers) {//4 ------------if (interviewer instanceof Manager) {//5 -----------------int teamSize =((Manager)interviewer).teamSize;//5 ---------------if (teamSize > 10) {//6 ---------------------interviewer.conductInterview();//6 ---------------}//6 ---------------else {//7 --------------------System.out.println("Mgr can't " +//7 ---------------------"interview with team size less than 10");/7 --------------} --------} ---------------else if (interviewer instanceof HRExecutive) {/8 --------------------interviewer.conductInterview();//8 ---------------} --------} ----} } 1. Array to store objects of classes that implement interface Interviewer 2. Store object of Manager at array position 0 3. Store object of HRExecutive at array position 1 4. Loop through values of array interviewers 5. If object referred to by interviewer is of class Manager, use casting to retrieve value for its teamSize 6. If interviewer's teamSize > 10, call conductInterview 7. If interviewer's teamSize <= 10, print message 8. Otherwise, if object stored is of class HRExecutive, call conductInterview method on the object; no casting is required in this case

Code that works with the base class in a hierarchy tree can work with all ________ that are added using _________ later.

classes, inheritance

You can implement inheritance by using either a______class or a(n)________class as a base class.

concrete, abstract

What keyword does a class use to inherit a class?

extends

What keyword does an interface use to inherit from another interface?

extends

What keyword is used for a class to inherit a class?

extends

A derived class can also____ or____its base class's members.

hide, override

What keyword does a class use to implement an interface?

implements

"this" and "super" are____object references

implicit

You don't need to reinvent the wheel. With_____ in place, subclasses can use tried-and-tested code from a base class.

inheritance

Unacceptable declarations within an interface and corrections that will let them be correct declarations.

interface Relocatable{ ----void move();//1 } interface CEO implements Relocatable{ ----void move();//2 } 1. Implicitly public 2. Won't compile. Can't assign weaker access(default access) to public method move in class CEO. interface Relocatable { ----void move();//1 } class CEO implements Relocatable { ----public void move() {}//2 } 1. Implicitly public 2. Will compile

Example of interface usage

interface Trainable { ----public void attendTraining(); } interface Interviewer { ----public void conductInterview(); } class Employee { ----String name; ----String address; ----String phoneNumber; ----float experience; } class Manager extends Employee implements Interviewer, Trainable {//1 ----int teamSize; ----void reportProjectStatus() {} ----public void conductInterview() { --------System.out.println("Mgr - conductInterview"); ----} ----public void attendTraining() { --------System.out.println("Mgr - attendTraining"); ----} } //2 class Programmer extends Employee implements Trainable{ ----String[] programmingLanguages; ----void writeCode() {} ----public void attendTraining() { ---------System.out.println("Prog - attendTraining"); ----} } 1. Manager implements Interviewer and Trainable 2. Programmer implements only Trainable.

When multiple classes inherit a base class, it creates a _______

logical group

How many interfaces can a class implement?

multiple

What CAN'T a derived class inherit?

private members of a class Base class members with default access, if the base class and derived classes exist in separate packages. Constructors of the base class. A derived class can call a base class's constructors, but it doesn't inherit them.

When implementing an interface, you must implement all its methods using what access modifier?

public

There are differences, however, when you try to access an object using a ____ of its own type, its base class, or an implemented interface.

reference variable

A class that implements an interface cannot make the interface's methods more_____

restrictive

What are the common names of a class that is the inheritor?

subclass, derived class, extended class, or child class

What is another term for inheritance?

subclassing

What are common names for the class that is inherited from?

superclass, base class, or parent class.

Exam Tip: "this"

this refers to the instance of the class in which it's used. this can be used to access the inherited members of a base class in the derived class.

It's interesting to note that these variables can't access all the____ and ____ defined in the derived class or the class that implements the interface.

variables, methods

Inheriting a class enables you to concentrate on the________ and ______ that define the special behavior of your class.______ lets you make use of existing code from a base class without having to define it yourself.

variables, methods, Inheritance

Can an abstract class be defined as a base class, even if it doesn't define any abstract methods?

yes

How many classes can a class inherit?

zero or one


Kaugnay na mga set ng pag-aaral

Foundations of American Democracy

View Set

Patho Ch. 14 Alterations in nutrition

View Set