Part 3 Exam 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

public

allows all to access directly

protected

allows class in which defined and all its subclasses to access it directly

private

allows only class in which defined to access it directly

Method Overloading

Changes the header AND the body of the parent method in the subclass This you start from scratch, no super. give it the name of the parent but put things to call in it ex: public void print(String fileName) {

Which of the following classes should not inherit a class that defines an employee? a. Secretary c. SalaryWorkerb. Manager d. Person

Person, since person is not subset of employee, whereas the other three are different types of employees

Abstract class

A class that cannot be instantiated and defines instance fields and methods for subclasses. This is useful when we do not need objects of a generic class ex: public abstract double computeTotalScore(); so we put abstract after public, and end with a semicolon instead of brackets The compiler now forces you to use the class unless you override it in child classes

Which of the following statements are true. Select all that apply. Subclass inherits all the attributes from the superclass, including the private attributes Subclass inherits only the public attributes from the superclass Subclass inherits all the methods from the superclass, including the private methods Subclass inherits only the public methods from the superclass Subclass inherits all the public get and set methods from the superclass Subclass inherits the public constructors from the superclass

A, D, E Subclasses inherit parent class private attributes, public methods, and public get/set methods. Subclasses do not inherit any constructors or private methods.

Inheritance

Allows us to create a new class based on an existing class so we can reuse code. For example, if we want HS students and undergrad students, we could make two totally different classes for them, OR we could make a parent student class and then use inheritance to modify it for our needs

Characteristics of abstract class

An abstract class cannot be instantiated A class that contains abstract methods must be declared as abstract. However, it is possible to declare a class that contains no abstract methods as an abstract class All derived classes of an abstract class will have to provide body for all its abstract method(s), or they will have to be declared as abstract A subclass can be abstract even if its superclass is concrete.

Which of the following method declarations overrides this method? public double calculateMilesPerGallon(double speed){...} a. public double calculateMilesPerGallon(int speed){...} b. public double calculateMilesPerGallon(double s){...} c. public double calculateMilesPerGallon(double speed, int r){...} d. public int calculateMilesPerGallon(double speed){...}

B public double calculateMilesPerGallon(double s){...} Since it (presumably) uses super() then adds the double to it, instead of rewriting it altogether. it calls and builds, but does not replace and rewrite

What are the synonyms for parent class?

Base class, parent class, and super class

Which of the following method declarations overloads this method? Select all that apply. public double calculateMilesPerGallon(double speed){...} a. public double calculateMilesPerGallon(int speed){...} b. public double calculateMilesPerGallon(double s){...} c. public double calculateMilesPerGallon(double speed, int r){...} d. public int calculateMilesPerGallon(double speed){...}

C and A both overload it because it is rewriting it by including a new int that will be used in calculation, a new parameter. Overriding keeps the existing parameters

Method Overriding

Change method body in subclass without changing method header. This alternative version of the method replaces the one in the parent class. This gets the original info then you add to it Do this by naming the class the same as what you want to override, then doing the super ex: public void print(){ super.print() then replace}

If a class contains an abstract method: A. you cannot create an instance of the class B. the abstract method will have only a header, but not a body, and end with a semicolon C. The class must be declared as abstract D. All of the above

D. All of the above are true. A class must be declared abstract, an abstract method will have only a header but not body and ends with a semicolon, and you cannot create an instance of the class

What are the synonyms for child class?

Derived class, child class, and sub class

An abstract class must have at least one abstract method.

False, abstract classes do not need abstract methods but abstract methods need abstract classes. Also, child classes can be abstract with non-abstract parents and vice-verse

All methods in an abstract class are abstract methods.

False, there doesn't even have to be abstract methods in it.

The following code gives compiler error. What modification would you have to make to the Student class code for it to compile without errors? public abstract class Person { private String name; public void setName(String name){ this.name = name} public abstract String m(); } public class Student extends Person { private double gpa; }

Have to override the abstract String method from the parent class to compile OR declare it to be abstract

The following code gives compiler error. What modification would you have to make to this code for it to compile without errors? public class Person { private String name; public void setName(String name){ this.name = name } public abstract String m(); }

class needs to be abstract to use an abstract method fixed code: public abstract class Person { private String name; public void setName(String name){ this.name = name } public abstract String m(); }

super

first statement in a method to call the parent class constructor. It automatically passes "name" from the parent class into the new method!

When a concrete i.e. a non-abstarct subclass extends an abstract class: it must override all the methods it has inherited from the abstract class it must override all the abstract methods inherited from the abstract class it must override all the non-abstract methods inherited from the abstract class it can not override any of the the methods inherited fron the abstract

it must override all the abstract methods inherited from the abstract class

Subclasses can...

just override an inherited method just overload an inherited method both overload and override an inherited method neither override nor overload an inherited method

Which of the following statements declares Salaried as a subclass of PayType? public class Salaried extends PayType public class Salaried inherits PayType public class PayType extends Salaried public class PayType inherits Salaried

public class Salaried extends PayType

extends

put at the top of the subclass to join it to the parent class. with it, we can now call code from the parent.

subclass

the child of the parent class, declared with extends and super. subclasses inherit all attributes, including private attributes, as well as all public methods EXCEPT FOR CONSTRUCTORS. private methods are not inherited


Conjuntos de estudio relacionados

IB biology super review for paper 1 + 2

View Set

PSY 2 Chapter 26, 25, 17, 13, 35

View Set

AP US History Vocabulary (Presidential Highlights and Study Guides)

View Set

Chapter 14: Care of a patient with a Neurologic disorder exam

View Set

Writing a Persuasive E-mail Assignment

View Set

The Lean StartUp, Identify Your Niche, Finding Your Audience Quiz, Finding Your Passion

View Set

13.1: Polyhedra and Other Solid Shapes

View Set

AP GOV unit 3 mcq (3.1, 3.2, 3.4, 3.5, 3.7, 3.9- 3.13)

View Set

Diagnostic Testing HESI prep- Adult Care

View Set

50 Sophisticated Words You Should Start Using Instead

View Set