Java Inheritance

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

subclass - inherit

-A subclass inherits all the members (fields, methods, and nested classes) from its superclass. -Constructors are not members, so they are not inherited by subclasses -the constructor of the superclass can be invoked from the subclass.

What is the relationship between objects and abstract classes?

-You cannot construct an object of an abstract class -BUT you can still have an object reference whose type is an abstract class (must be an inherited concrete subclass)

when would you consider using interfaces over abstract methods?

-You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes. -You want to specify the behavior of a particular data type, but not concerned about who implements its behavior. -You want to take advantage of multiple inheritance of type.

when would you consider using abstract methods over interfaces?

-You want to share code among several closely related classes. -You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private). -You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

HAS-A relationship

-are mainly based on the usage -determines whether a certain class HAS-A certain thing. -helps to reduce duplication of code as well as bugs.

Superclass reference variable

-can hold the subclass object -but using that variable you can access only the members of the superclass -access the members of both classes it is recommended to always create reference variable to the subclass.

superclass

-the class whose properties are inherited -(base class, parent class)

The ______ method compares the data in objects

.equals()

Chapter 8 Objectives:

1) Explore the derivation of new classes from existing ones. 2) Define the concept and purpose of method overriding. 3) Discuss the design of class hierarchies 4)Examine the purpose and use of abstract classes. 5) Discuss the issue of visibility as it relates to inheritance. 6) Discuss object-oriented design in the context of inheritance.

Steps to developing an Inheritance Hierarchy:

1) List the classes that are part of the hierarchy 2) Organize classes into an inheritance hierarchy 3) Determine common responsibilities 4) Decide which methods are overridden in the subclass 5) Declare the public interface of each subclass 6) Identify instance variables 7) Implement constructors and methods 8) Construct objects of different subclasses and process them

Object has ___ methods, some of which can, and should, be __________.

11; overridden

Describe the relationship between a parent class and a child class.

A child class is derived from a parent class using inheritance. The methods and variables of the parent class automatically become a part of the child class, subject to the rules of the visibility modifiers used to declare them.

False

A child class may have more than one parent class in java (T/F).

Why would a child class override one or more of the methods of its parent class?

A child class may prefer its own definition of a method in favor of the definition provided for it by its parent. In this case, the child overrides (redefines) the parent's definition with its own.

Class Hierarchy

A child of one class can be the parent of another, creating a...

one, many

A class can have exactly ___ parent class, but it might have ____ child classes.

False

A class declared as abstract must contain abstract methods (T/F).

child class

A class derived from a parent class. Also called a subclass.

subclass

A class derived from a superclass. Also called a child class.

False

A class reserves memory space for variables (T/F).

abstract class

A class used to represent a generic concept in a class hierarchy. An abstract class cannot be instantiated.

False

A class with abstract methods does not necessarily have to be declared 'abstract' (T/F).

abstract method

A method header without a body, used to establish the existence of an operation before the implementation is available. A class that contains an abstract method is inherently abstract.

Super Reference

A parent's constructor can be invoked using the...

Object, Reference

A polymorphic reference uses the type of the ... , not the type of the ... , to determine which version of a method to invoke.

True

A reference variable can refer to any object created from any class related to it by inheritance (T/F).

Polymorphic Reference

A reference variable that can refer to different types of objects at different times.

Placeholders

Abstract classes act as .... in a class hierarchy.

Abstract classes are declared with the ______ modifier in the header.

Abstract.

What is the keyword must be used to inherit a class?

"extends"

toString

-method that returns a string representation for each object, used for debugging example: Rectangle box = new Rectangle(5, 10, 20, 30); String s = box.toString(); // Sets s to "java.awt.Rectangle[x=5,y=10,width=20,height=30]"

superclass

A class from which another is derived. Also called a parent class or base class.

Are all members of a parent class inherited by the child? Explain.

A class member is not inherited if it has private visibility, meaning that it cannot be referenced by name in teh child class. However, such members do exist for the child and can be referenced indirectly.

Object Class

All classes are derived directly or indirectly from the...

Why are private methods essentially final?

Because they cannot be overridden

step 4 of equals

Cast the other object to your class type

overload

Does public boolean equals(Employee otherEmp) override or overload the Object's equals method?

Inheritance

How a new class is created from an existing one.

mechanism for extending existing classes by adding methods and fields

Inheritance

multiple inheritance

Inheritance in which a subclass can be derived from multiple parent classes. Java does not support multiple inheritance.

single inheritance

Inheritance in which a subclass can have only one parent. Java supports only single inheritance.

True

Inherited variables and methods can be used in the derived class if they had been declared locally (T/F).

A ______ cannot be instantiated

Interface

Inheritance can be applied to _________ so that one _______ can be derived from another. (not classes)

Interfaces.

Compile time, Runtime, Late Binding, Dynamic Binding

Most binding occurs at ... , but for polymorphic references binding is done at ... (Called ... or ...)

extends

Notice the keyword ______ which tells the compiler that Salesperson is a subclass of Employee.

All classes are derived ultimately from the ______ class.

Object

All classes are subclasses of the _________ class.

Object

In Java, all classes inherit from the ______ class by default

Object

The .equals() method is inherited from the ______ class

Object

What is the difference between package access and protected access?

Package access - subclasses must be inside of the packages for them to be able to access members of superclasses. Protected access - subclasses must simply inherit from a superclass to gain access to protected members

The point of an abstract class is:

Placeholder in the class hierarchy, may contain partial description that is inherited by all it's descendants.

A subclass constructor must always class on the ______________.

Superclass constructor

step 1 of equals

Test whether the objects refer to same place: if (this == otherObj) return true;

step 2 of equals

Test whether the other object is null: if (otherObj == null) return false;

Class Hierarchy

The "family tree" of classes.

fields

The Object class contains no ___

methods

The Object class contains several ____

super

The Salesperson class also needs a name and salary, but it doesn't need to define them, since it inherits them from its _____ class.

hierarchy

The _______ (sub-class/superclass relationships) of classes can go many levels.

A _____ object generates action events at regular intervals and can be used to control an animation.

Timer

Why should you call setters in the constructor instead of directly modifying data members?

To make input cleaner and to filer it.

How do you access the initialized variables of the superclass in the subclass?

Using the superclass constructor, which can be called through the subclass's constructor AS LONG AS it's the FIRST STATEMENT in the subclass constructor

False

We can create an object from an abstract class (T/F).

compiler error, parent does not have that method

What will happen if you create a reference of a child object to a parent and try to access the child's methods?

Shadowing Variables

When a child class declares a variable with the same name as one that is inherited from the parent.

Overrides

When a child class defines a method with the same name/signature as a method of the parent class, the child class... the parent.

True

When changes are made to the parent class, the children are affect automatically (T/F).

Subclasses

a class that is a special case of an existing class

abstract methods

a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

in enumeration classes, the toString method returns

a string that equals the objects name

parent

a subclass inherits fields and methods from its

protected features can be accessed by

all subclasses and all classes in the same package

cannot

an abstract class ___ be instantiated

reflexive, symmetric, transitive, consistent, and not null

an equals methods must be

The subclass inherits these from the superclass

behavior and state

extends syntax

class Super { ..... ..... } class Sub extends Super { ..... ..... }

makes a new object with the same state as an existing object

clone method

One important reason got inheritance

code reuse

if an object is null, instanceOf will return

false!

Inheritance is a mechanism in which one class can inherit the ______ and methods of another "parent" class

fields

a class can implement one or more interfaces

helped Java get rid of the impossibility of multiple inheritance.

Common features should be located as ______ in a class hierarchy as possible.

high, maximizes the potential to reuse classes, facilitates maintenance activities.

How to successfully cast obj to be of type Question, using instanceOf:

if (obj instanceof Question) { Question q = (Question) obj; }

testing whether two objects belong to the same class

if (otherObject == null) return false; if (getClass() != otherObject.getClass()) return false;

Finished House

if classes are the floor plans, an object is the...

cast

if you implement a generic Comparable interface, you do not have to ___ to use the class's methods

overriding a method

if you specify a method with the same signature (same name and same parameter types) it overrides the method of the same name in the superclass

Interfaces can be ______ by other classes or abstract classes

implemented

The keyword used to inherit an Interface is...

implements

If no superclass constructor reference exists in the subclass constructor, then the superclass _________________ a default superclass constructor _________ before any code is run in the constructor.

implicitly calls; super()

failing to invoke the superclass method results in

infinite recursion

sets of classes can form complex

inheritance hierarchies

An abstract class cannot be ______

instantiated

The instanceof Keyword : Example

interface Animal{} class Mammal implements Animal{} public class Dog extends Mammal { public static void main(String args[]) { Mammal m = new Mammal(); Dog d = new Dog(); System.out.println(m instanceof Animal); System.out.println(d instanceof Mammal); System.out.println(d instanceof Animal); } } output : true true true

polymorphism

is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

extends

is the keyword used to inherit the properties of a class

The Object class is defined in the ________ package.

java.lang

A ______ class can be created by deriving it from an event adapter class.

listener

An alternate technique for creating a _____ class is to extend an event adapter class.

listener

polymorphism allows us to:

manipulate objects that share a set of tasks even though the tasks are executed in different ways

______ methods and variables of the parent cannot be referenced in the child class through an object.

private

can only be accessed by the methods of their own class

private features

a subclass has no access to

private fields of its superclass

______ are not inherited

private members

four levels of controlling access to fields

public access, private access, protected access, package access

IS-A Relationship : implements keyword - Example

public interface Animal { } public class Mammal implements Animal { } public class Dog extends Mammal { }

overriding

redefining a method in a subclass when we want a method in the child class to behave differently than in its parent class

In Java, every class that does not specifically extend another class extends this

the class Obect

==

the equals method of the Object class does the same thing as

inheritance

the process of deriving one class from another.

What happens if you omit the superclass constructor call?

the superclass constructor with no arguments is called

Via inheritance, the new class contains the _____ and the ______ of the original class.

variables, methods

What is the keyword used to refer to member of base class from a sub class?

"super" (used to refer to its immediate superclass)

tests whether two references are to the same object

== method

Class

A blueprint for an object.

any

A variable of type Object can reference ___ object

What is the significance of the Object class?

All classes in Java are derived, directly or indirectly, from the Object class. Therefore, all public methods of the Object class, such as equals and toString, are available to every object.

False

An interface reference can refer to one object of any class that implements the interface (T/F).

Multiple Inheritance

Child is derived from multiple classes.

What relationship should every class derivation represent?

Each inheritance derivation should represent an is-a relationship: the child is-a more specific version of the parent. If this relationship does not hold, then inheritance is being used improperly.

No visibility issues when dealing with inheritance between ________.

Interfaces.

Single inheritance

Java's approach to inheritance is called...

Software Reuse

One purpose of inheritance.

Subclasses can _________ methods defined in a superclass.

Override

Abstract Class

Represents a concept on which other classes can build their definitions.

Siblings

The children of the same parent class.

multiple

a class can extend just one parent class, but can have ___- interfaces

IS-A Relationship

is a way of saying: -This object is a type of that object

things you can do to methods in a subclass

override, inherit, define new

The default access modifier is ______

package-private

IS-A Relationship : extends keyword is used to achieve inheritance. - Example

public class Animal { } public class Mammal extends Animal { } public class Reptile extends Animal { } public class Dog extends Mammal { }

can be accessed by methods of all classes

public features

super keyword

refers to the parent's class

If you want your toString method to be usable by a subclass

return getClass().getName() + desired print info

interface

set of requirements for a class

An abstract method only has a method ______

signature

Denotes inheritance

solid line with hollow triangle tip

can be converted into superclass references

subclass references

A protected member of a class is visible to ______

subclasses

method call are always determined by ...

the type of the actual object

How do you get around a method parameter (default: object) not knowing anything about the actual object it's being compared to? (for the equals method)

you need to cast it to make it read the right parameter variables

all

you should use an abstract class when you don't want to implement ___ the methods for a class that's going to be used as a super class

What does the protected access class modifier do?

Allows subclasses to have access to superclass data member and methods marked as such.

False

An abstract class can be declared 'static' (T/F).

True

An abstract class cannot be instantiated (T/F).

What is the role of an abstract class?

An abstract class is a representation of a general concept. Common characteristics and method signatures can be defined in an abstract class so that they are inherited by child classes derived from it.

shadow variable

An instance variable defined in a derived class that has the same name as a variable in the parent class.

True

An interface name can be used to declare an object reference variable (T/F).

Protected Visibility

Any class in the same package or any class derived can reference it (public would violate encapsulation). Provides the best possible encapsulation that permits inheritance.

The bad way and the better way to write toString methods:

BAD WAY: public class BankAccount { ... public String toString() { return "BankAccount[balance=" + balance + "]"; } } public String toString() } . { } return getClass().getName() + "[balance=" + balance + "]"; The

How does inheritance support software reuse?

Because a new class can be derived from an existing class, the characteristics of the parent class can be reused without the error-prone process of copying and modifying code.

Why should you make data members private, even if a subclass needs to be able to modify it?

Because it allows filtration of the input and can make it cleaner.

Why should you never use == to compare the attributes of objects?

Because it compares whether or not two references point to the same object in memory, not if they're attributes are the same. equals() can be overridden. == cannot.

When overriding equals(), what must you do to the Object reference before using it to access attributes of the specific class instance you are passing to equals()?

Cast them - without casting, you can only access the 11 Object methods.

step 5 of equals

Compare all the fields of the class, depending on your definition of equals, using the equals method for objects.

step 3 of equals

Compare the classes using instanceof or getClass() depending on your definitions:

If your subclass tries to call an instance variable or method in the superclass that is private, what happens?

Compilation Error - cannot be inherited by the subclass and does not have access to it

Bind

Computers ... a method invocation to a method definition.

False

Constructors are usable in a child class (T/F).

@Override is used to ________?

Declare that a superclass method is being overriden. It is optional, and placed directly above the method that is doing the overriding. It is also a safety mechanism and will return an error if no superclass method exists in the same format as the overridden method.

True or False: In Java, you can only have one layer of inheritance, but you can use multiple class inheritance.

False - Java inheritance can be multilayered, but is unfortunately only single inheritance.

True or False: A subclass cannot call the methods or fields of its *indirect* superclasses.

False. Just because it is an indirect inheritance doesn't mean that the subclass doesn't inherit its fields and methods.

True

If a child of an abstract class does not give a definition for every abstract class it inherits, it is also considered to be abstract (T/F).

no

If the call to the parent constructor is missing, the compiler will add one that calls the parent constructor with __ arguments.

False

If there are no current uses for them, do not override general methods such as 'toString' and 'equals' (T/F).

True

If two objects are equal according to the 'equals' method, then calling the 'hashCode' method on each should produce the same result (T/F).

Salesperson specific methods

In the example on top, even though emp actually points to a SalesPerson, the reference is of type Employee, so you can't use it to access _____ _____ _____ .

If you call the method of a superclass that the superclass itself inherited but did not implement, then the subclass will run the implementation in the _________ superclass.

Indirect

What does the "super" reference do?

It can be used to refer to superclass methods, and is used to refer to the superclass constructor.

Why is protected problematic?

It could put the integrity of information at risk because input rules can be surpassed.

True

It is bad to redefine inherited variables (T/F).

toString() can be overridden at multiple _______ to meet class implementations and outputs.

Levels

Inheritance

Making a new class from an existing one.

To override equals(), what must the reference type of the parameter be? What happens if that is not the same?

Object; equals() is overloaded rather than overridden.

descended

One way this is very useful is that I can write a method that acts on a parent class, and can send it any object that is ______ from that class.

superclass and base class

Other names for the parent of a subclass are ______ and ______ .

child, derived

Other names for the sub-class of a parent are ___ class and ___ class.

If a method that you are attempting to override doesn't have the same access modifier, name, return type, and parameters, than you __________ the method instead of overriding it.

Overload

In order to concatenate an object and a string, without the result being messed up, what do you have to do?

Override the method and supply your own version -yields a string that describes the object's state

______ is when a subclass redefines a method of it's parent class

Overriding

________ members are inherited by the child class, but cannot be referenced directly by name. may be used indirectly by calling methods.

Private.

Polymorphism

Provides a means to create versatility in our software.

overriding

Redefining a method that has been inherited from a parent class.

By default, what does toString() return?

Reference information about the instance of the class that you are using the method on.

compiler error

SalesPerson sp = new SalesPerson(...); Employee emp = sp; // legal because a SalesPerson is an Employee SalesPerson sp2 = emp; // what does the last statement generate?

commission

Salespeople are employees, so they share all the qualities of employees, but they also make _____ which other employees don't

False

Sibling classes are related by inheritance (T/F).

What useful String method can be used when returning a String in toString()?

String.format().

public, private

The child class can use the ... variables, but not the ... variables of the parent class.

A private member of a class is visible only to ______

The defining class

polymorphism

The fact an object variable can refer to objects of different types is called _______.

How can the final modifier be used to restrict inheritance?

The final modifier can be applied to a particular method, which keeps that method from being overridden in a child class. It can also be applied to an entire class, which keeps that class from being extended at all.

Unlimited

The number of children a class can have/the number of levels there can be in a class hierarchy:

What does the protected modifier accomplish?

The protected modifier establishes a visibility level (such as public and private) that takes inheritance into account. A variable or method declared with protected visibility can be referenced by name in the derived class, while retaining some level of encapsulation. Protected visibility allows access from any class in the same package.

is-a

The relationship between a child and parent class.

is-a relationship

The relationship between two classes related by inheritance. The superclass is-a more specific version of the subclass.

Super

The reserved word that can be used in a class to refer it to its parent class.

Extends

The reserved word that indicates that a new class is being created from the original class.

Why is the super reference important to a child class?

The super reference can be used to call the parent's constructor, which cannot be invoked directly by name. It can also be used to invoke the parent's version of an overridden method.

only

This does not work the other way. An Employee object can't call methods defined _____ in Salesperson.

doesn't

This will cause an error if the parent class ______ have a constructor with no arguments defined.

Siblings

Two child classes of the same parent.

Inheritance and Interfaces

Two ways to create polymorphic references:

hashCode

Uniquely identify each commit.

Interfaces

Use ... to create a class that serves multiple roles.

How do you use inheritance in a class?

Use the extends keyword after the class name: public class Car extends Vehicle

inherits

We don't need to declare name and salary in the Salesperson class because it ______ them from its parent.

new

We don't want to rewrite all the employee information for the ___ class, so we make Salesperson a subclass of Employee.

Example of Polymorphism

When a superclass reference is expected (the method is presentQuestion(Question q) and a subclass reference is passed in -- presentQuestion(ChoiceQuestion cq)

The Principle of Encapsulation

When variables of the parent class are declared public so that the child class can access them, the ... is violated.

When do you use "super"

When you're writing a method in the subcass that needs to directly call the method inside the superclass (ex. if you're calling method display, inside display in the subclass, you need to reference the superclass so that it's not calling itself over and over." Super is NOT an object refernece

What is the difference between single inheritance and multiple inheritance?

With single inheritance, a class is derived from only one parent, whereas with multiple inheritance, a class can be derived from multiple parents, inheriting the properties of each. The problem with multiple inheritance is that collisions must be resolved in the cases when two or more parents contribute an attribute or method with the same name. Java supports only single inheritance.

inherited class type

You can use a cast to tell the compiler a class reference is really an _____

What are one of the limitations of using abstract methods?

You cannot construct objects of classes with abstract methods. For example, once the Account class has an abstract method, the compiler will flag an attempt to create a new Account() as an error.

unrelated

You cannot use a cast to try to cast _____ types

not

You may ____ assign a parent object to a reference to its child.

reference

You may assign a child object to a _____ to its parent

BankAccount acct = new Checking(); How would you turn acct into a Checking object?

You would have to cast acct (which is a BankAccount reference) into an actual Checking object to use Checking specific methods Checking chAcct = (Checking) acct;

concrete class

a class for which you can create an object (basically anything that isn't abstract)

abstract class

a class for which you cannot create an object

base class

a class from which another is derived. Also called a parent class or super class.

parent class

a class from which another is derived. Also called a superclass or base class.

An ______ cannot be instantiated

abstract class

class for which you cannot create obects

abstract class

how do abstract classes differ from interfaces

abstract classes can have instance fields and they can have concrete methods and constructors

An ______ does not have a body

abstract method

An ______ only has a method signature

abstract method

What is a drawback of protected variables?

accessible by subclasses AND by other classes in the same package (i.e. not that protected)

BankAccount acct = new Checking(); what is the object reference what is the object what methods can you call on these?

acct is a BankAccount reference to a (new) Checking object -can only call BankAccount methods on "acct"

public

all methods in an interface are automatically ___

instantiated

an interface cannot be

implement

an interface gives a list of methods that the class must ___ to satisfy the interface

instanceOf will return true if:

an object can be cast to the new Type

instanceOf is a what...

an operator (not a method) **DOES NOT OPERATE ON NUMBERS

Abstract Class

cannot be instantiated, usually contains one or more abstract methods, but can contain methods that aren't abstract, and can make data declarations.

when you call the clone method you must use a

cast ex. BankAccount clonedAccount = (BankAccount) account.clone();

how to wrtie equals method

cast the otherObject parameter to the type of your class, and then compare the fields of the implicit parameter and the other parameter

inheritance chain

casting a reference to an object down in the ____ will cause a compiler error

When overriding a method, you are not allowed to

change the type of parameter variable

equals Method

checks whether two objects have the same CONTENTS differs from == operators, which only checks if the two references are referring to the asme object

a class which you can create objects for

concrete class

Object clone()

creates a returns a copy of this object

makes it so that no one can extend the class

declaring the class final (you can also declare methods as final so that they can't be overridden)

a type with a finite number of values

enumerated type (enum)

tests whether two objects have the same contents

equals method

Object

every class in Java is a descendent of the ___ class

A public member of a class is visible to ______

everything

In Java, a class can ______ up to one other class to inherit from it

extend

Classes use ______in the header to create child classes.

extends

Subclasses inherit all

fields from the superclass

A ____ class cannot be extended at all.

final

A child class cannot override a ______ method.

final

The ______ modifier can be used to restrict inheritance.

final

inheriting methods

if you do not explicitly override a superclass method you automatically inherit it; the superclass methods can be applied to the subclass obects

You can never override

instance fields

The ______ operator can be used to return a boolean indicating if the first argument is a type equal to the second argument

instanceof

An Interface cannot be ______

instantiated

Relationship between parent and child class

is-a

What happens if you try to extend more than one class to a subclass?

it won't work and you'll get a compile time error

An Interface consists solely of ______

method signatures

must be defined in the subclass

new methods and instance fields

The _____ that is used to invoke the method determines which version of the method is executed.

object

protected access

object that can be accessed by the methods of the object's class and all its subclasses Access is restricted to the containing class and to any class that is derived directly or indirectly from the containing class

methods, constructors

only _____ and ___ constructors can be declared abstract

A child class can ______ the parent's definition of an inherited class.

override

A class derived from an abstract parent must _______ all of it's parent's abstract methods.

override , or the derived class will also be considered abstract.

private members cannot be ______

overriden

all methods of classes in the same package can access the feature

package access

the default access when no access modifier is given

package access

How do you cast an object?

public boolean equals(Object otherObject) { Stamp other = (Stamp) otherObject; return color.equals(other.color) && value == other.value; }

more specialized class that inherits from the superclass

subclass

Any new instance fields that you define in the subclass are present only in

subclass objects

use this keyword to call a method of the superclass

super ex. super.deposit(amount);

constructor

super can be used to call the _____ of the class

How do you call upon the constructor of a superclass?

super();

more general class that forms the basis for inheritance

superclass

instanceOf

tests whether an object belongs to a particular type

instanceof operator

tests whether an object belongs to a particular type

first

the call using super must be the ____ statement in the constructor

in enumeration classes, the clone method returns

the given object WITHOUT making a copy

class hierarchy

the hierarchy formed by inheritance among multiple classes.

If you define a method that does not exist in the superclass then,

the new method can only be applied to subclass obects

What happens if a subclass extends an abstract superclass?

the subclass is not itself abstract and can implement the abstract method -overrides the abstract methods

IS-A Relationship : extends keyword

the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass.

What happens when you concatenate a string with an object?

the toString method is called. Example "box=" + box; becomes "box=java.awt.Rectangle[x=5,y=10,width=20,height=30]" **DOES NOT WORK W NUMBERS bc they're not objects

method calls are always determined by

the type of the actual object (it doesn't matter if the object reference is stored in a different field type)

dynamic method lookup

the virtual machine calls an instance method, and it located the method of the implicit parameter's class

The instanceof Keyword

to check determine whether Mammal is actually an Animal, and dog is actually an Animal.

What are the general methods defined in the (automatically extended) class Object?

toString equals hashCode

The _______ and _______ methods are inherited by every class in every Java program.

toString, equals

calling a superclass constructor

use the super keyword in the first statement of the subclass constructor super(initialBalance)

Super Reference

used in a class to refer to it's parent class. can invoke parent's constructor. can also be used to reference parent's variables and methods.

IS-A Relationship : implements keyword

used with classes to inherit the properties of an interface. Interfaces can never be extended by a class.

implements

what keyword do you use for interfaces

when you do not have access to private fields

when do you need to call the constructor of the parent class?

instanceof, cast

when trying to call child class specific methods on a group of child and parent objects, you should use ____ then ___

Is it legal to store a subclass reference in a superclass variable? like such: Question q = cq;

yes

define the toString method to

yield a string that describes the object state

interface

you can have reference variables with ___ types

why would someone use a "final" method / class?

you can prevent other programmers from creating subclasses or from overriding certain methods -- no one can extend 'data type'. When you have a reference of type String, it must contain a string object and never an object of a subclass

What do you do when you need to convert a superclass reference to a subclass reference?

you cast it, but in order to protect against bad casts, you need to use "instanceOf"

salespeople

you have an employee class and you want to have a special class for employees who are ______.

reference variable

you may have an abstract class _____

If you're trying to use the "equals" method, and comparing two objects, you will get an error if you enter one object type and another entry that is not the object type. How do you prevent this?

you test whether the two objects belong to the same class use if statement if (getClass() != otherObject.getClass()) { return false; }

is a

• Informally, a subclass satisfies the _____ criteria. A salesperson _____ employee.

salesperson, parent

• Suppose now we have a reference to a Salesperson object: SalesPerson p = . . . • We can call any method defined in ______: double r = p.getRate(); // Salesperson method returns rate • Or any method from the _____ class:

Why do we use inheritance?

-Minimizes redundant code for common traits -Changes in code have to be made once and is passed onto all classes that inherit that code. -Makes similar classes less disjointed by giving them a foundation of similar code -Leads to less time spent coding, less code, easier debugging, and easier re-usability of code

subclass

-The class which inherits the properties of other -(derived class, child class)

Invoking Superclass Constructor

-f a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. -But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below. super(values);

super

-similar to this keyword Following are the scenarios where the super keyword is used. -used to differentiate the members of superclass from the members of subclass, if they have same names. -used to invoke the superclass constructor from subclass.

Data members define a _______ relationship with the class they are defined in.

HAS-A

In Java, aggregation is a _____ relationship, and is the use of a reference type inside of a class.

HAS-A Ex. If Employee has a reference to Address, then Employee HAS-A Address.

Inheritance uses a ______ relationship.

IS-A

What is the difference between an is-a and a has-a relationship?

IS-A: defines that a class IS another class HAS-A: defines that a class HAS an attribute

super : differentiating the Members

If a class is inheriting the properties of another class. And if the members of the superclass have the names same as the sub class, to differentiate these variables we use super keyword as shown below. super.variable super.method();

What is single inheritance and how does it differ from multiple inheritance?

In single inheritance, a child class can only have one *direct* superclass; in multiple inheritance, a child class can have multiple *direct* superclasses.

What does single-inheritance do in Java?

It solves ambiguity problems - the most direct superclass is used when a identifier name is ambiguous.

If no direct superclass is specified, than a class will inherit from the __________ class by default.

Object

The .hashCode() method is defined on the ______ class

Object

The .toString() method is defined on the ______ class

Object

equals() can/should be overridden because it compares objects by default instead of object attributes.

SHOULD

What is inheritance?

The action of taking methods and fields from a parent class and allowing a child or subclass to inherit and use those methods and fields through an IS-A relationship between the parent child.

What is the difference between a superclass and a subclass?

The superclass is a generalized class containing all methods and fields in common in its child classes. The subclass is the child class that adds methods and fields that are unique to it.

What does the default Object implementation of equals() compare?

Whether or not two references point to the same object in memory.

public & private are examples of...

access modifiers

An abstract method does not have a ______

body

A class ______ extend multiple classes

cannot

Invoking Superclass Constructor : Invoking Superclass Constructor

class Superclass { int age; Superclass(int age) { this.age = age; } public void getAge() { System.out.println("The value of the variable named age in super class is: " +age); } } public class Subclass extends Superclass { Subclass(int age) { super(age); } public static void main(String argd[]) { Subclass s = new Subclass(24); s.getAge(); } }

The .equals() method compares the ______ in objects

data

In Java 8, an interface can contain method bodies if it uses the ______ keyword

default

Abstract classes can be ______ by other classes or abstract classes

extended

Interfaces can be ______ by other interfaces

extended

The keyword use to inherit an abstract class is...

extends

The keyword used to inherit a class is...

extends

Inheritance is a mechanism in which one class can inherit the fields and ______ of another "parent" class

methods

A package-private member of a class is visible to the class and to other classes in the same ______

package

A protected member of a class is visible to members of the same ______

package

Inheritance is a mechanism in which one class can inherit the fields and methods of another "______" class

parent

single inheritance B -> A

public class A { ..... } public class B extends A { ..... }

hierarchical inheritance: B ->A C->A

public class A { ..... } public class B extends A { ..... } public class C extends A { ..... }

multi level inheritance C -> B -> A

public class A { ..... } public class B extends A { ..... } public class C extends B { ..... }

multiple inheritance C ->A C->B

public class A { ..... } public class B { ..... } public class C extends A,B{ ...} ***A very important fact to remember is that Java does not support multiple inheritance. This means that a class cannot extend more than one class.

HAS-A relationship : example

public class Vehicle{} public class Speed{} public class Van extends Vehicle { private Speed sp; } This shows that class Van HAS-A Speed. By having a separate class for Speed, we do not have to put the entire code that belongs to speed inside the Van class, which makes it possible to reuse the Speed class in multiple applications.

Types of Inheritance

single inheritance multi level inheritance hierarchical inheritance multiple inheritance

______ cannot be overriden

static methods

To call the constructor of a parent class, you must use the ______ keyword

super

Inheritance

the process where one class acquires the properties (methods and fields) of another. -manageable in a hierarchical order.


Conjuntos de estudio relacionados

Art Appreciation Quiz #7 - Themes

View Set

Cognition, Perception, and Emotion-PSYCH

View Set

Chapter 2-2 (Economic Conditions Change)

View Set

Chapter 12 - Managing Workforce Flow

View Set

Gero midterm CH4 (21,1,2)// CH8//

View Set

Taxes, Retirement, and Other Insurance Concepts Quizzes

View Set

Corporate Finance MGMT 332 Chapter 16

View Set

Bus of Retail: Questions for Study

View Set