Chapter 11

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

When can a object of a subclass always be used

Whenever a superclass object is used

What do classes define

types

What is the order JVM searches for a implementation when a method is invoked

From most specific to least specific aka the last subclass to the first parent class

What is o's actual type? Object o = newGeometricObject( ); System.out.println (o.toString( ));

Geometric Object

When does the @Override produce an error

If the annotated part does not actually override

Does the actual or declared type of the reference table decide which method to bind

actual

what relationship do subclasses and superclasses form

is - a

what class is every object in java descended from

java.lang.Object

Code for turning this array list into an array String[ ] array1 = new String[list.size( )]

list.toArray(array1);

How do you prevent a method or class from being extended

make it final

What are two other names for the super class

parent/ base class

what is the notation for explicit casting

(SubclassName)

What is the output of running the class C in (a)? (a) class A { public A() { System.out.println("A's no-arg constructor is invoked"); } } class B extends A { } public class C { public static void main(String[] args) { B b = new B(); } }

(a) The output is A's no-arg constructor is invoked

Write a condensed equivalent code Object o = new Student(); m(o);

m(new Student( ));

signature of the toString method

public String toString( )

What is the signature of Object's equal method

public boolean equals (Object o)

What is the structure for creating an extension

public class "subclass" extends "superclass"

Constructor's may invoke overloaded constructors or super classes constructors. However if neither is invoked explicitly what does the compiler automatically do

puts super( ) as first statement

you can always pass an instance of a ___________ to a parameter of its _______________. _______

subclass superclass type

whats upcasting

subclass to superclass

The subclass defines the

subtype

How can you invoke a superclass's constructor

super() super(arguments)

whats downcasting

superclass to subclass

The Superclass defines the

supertype

is == or equals stronger

==

What is dynamic binding?

A method may be implemented in several classes along the inheritance chain. The JVM decides which method is invoked at runtime. This is known as dynamic binding.

What happens if the object that's casted isn't an instance of the subclass

A runtime ClassCastException

How does a subclass invoke its superclass's constructor?

A subclass can explicitly invoke a suplerclass's constructor using the super keyword.

Define polymorphism

A variable of a supertype can refer to a subtype object

Indicate true or false for the following statements:(a) You can always successfully cast an instance of a subclass to a superclass.(b) You can always successfully cast an instance of a superclass to a subclass.

A) True B) False

How does the JVM find a matching method

According to parameter type, # of parameters, and order

if circle is a subclass of geometricObjects what does circle inherit from it

All accessible data fields and methods

Whats protected

Allows subclasses to access methods and data fields from the superclass, but does not allow other classes

Code for turning this array into an array list String [ ] array = { "red", "green", "blue"}

ArrayList<String> list = new ArrayList<> (Arrays.asList(array)

What is constructor chaining

Basically an order of invoking constructors from parents to child. The Parents are invoked first when the childs are invoked

What is the return types public class TestOverriding{ public static void main(String [] args){ A a = new A(); a.p (10); a.p(10.0); } } class B { public void p (double i){ System.out.println(i *2); } } class A extends B { public void p (double i){ System.out.println(i); } }

Both a.p(10) and a.p(10.0) invoke the method in class A so they both return 10.0

rue or false? You can override a private method defined in a superclass.

False.You can only override accessible instance methods.

What are the two functions of ==

Comparing two primitive data types or to see if two objects have the same reference type

What is inheritance

Defining new classes from existing classes

What are the three pillars of object-oriented programming? What is polymorphism?

Encapsulation, inheritance, and polymorphism. In simple terms, polymorphism means that a variable of a supertype can refer to a subtype object.

True or false? A subclass is a subset of a superclass.

False.A subclass is an extension of a superclass and normally contains more details information than its superclass.

True or false? When invoking a constructor from a subclass, its superclass's no-arg constructor is always invoked.

False.If a subclass's constructor explicitly invoke a superclass's constructor, the superclass's no-arg constructor is not invoked.

True or false? You can override a static method defined in a superclass.

False.You can only override accessible instance methods.

What is the benefit of using the @Override annotation?

It forces the compiler to check the signature of the overridden method to ensure that the method is defined correctly.

If a method in a subclass has the same name as a method in its superclass with different parameter types, is the method overridden or overloaded?

It is method overloading.

If a method in a subclass has the same signature as a method in its superclass with the same return type, is the method overridden or overloaded?

It is method overridden.

What is a compatible return type

It means the return type is a subtype of the overridden methods return type

What does @Override do?

It tells you that the annotated method below is required to override a method in the superclass

If a method in a subclass has the same signature as a method in its superclass with a different return type, will this be a problem?

It will be a problem if the return type is not a compatible return type.

Describe the difference between method matching and method binding.

Matching a method signature and binding a method implementation are two separate issues. The declared type of the reference variable decides which method to match at compile time. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compile time. A method may be implemented in several subclasses. The JVM dynamically binds the implementation of the method at runtime, decided by the actual class of the object referenced by the variable.

Explain the difference between method overloading and method overriding.

Method overloading defines methods of the same name in a class. Method overriding modifies the methods that are defined in the superclasses.

What do the subclasses inherit from the superclass

Methods and properties

Are constructors inherited by the subclass?

No

Can you put primitive type data in an array list

No

What is casting object

One object reference can be typecast into another object reference

When can instance methods be overridden

Only if its accessible

How can you use private data fields from a super class in a subclass

Only if mutators and accessors are defined in the superclass

Where can access private members

Only within that class

Do overloaded or overridden methods have the same signature?

Overridden

Least visible to most visible

Private Default Protected Public

What is equal intended to do

See if two objects have the same contents

What is single inheritance? What is multiple inheritance? Does Java support multiple inheritance?

Single inheritance allows a subclass to extend only one superclass. Multiple inheritance allows a subclass to extend multiple classes. Java does not allow multiple inheritance.

Why would you override a method

Sometimes you want to modify the implementation of a method defined in the superclass

what is ArrayList used for

Storing a list of objects

What is the more extended class called

Subclasses

What happens when no inheritance is specified when a class is defiend

The superclass is object by default

What is the return type of the following code public class TestOverriding{ public static void main(String [] args){ A a = new A(); a.p (10); a.p(10,0); } } class B { public void p (double i){ System.out.println(i *2); } } class A extends B{ public void p (int i){ System.out.println(i); } }

These invoke the method who matches their signature. Therefore a.p(10) returns 10 and a.p(10.0) returns 20.0

Are overloaded methods in the same or different classes?

They can be in either. If they are in different classes they are related by inheritance

What does overloading mean

To define multiple methods with the same name but different signatures

Is upcasting or downcasting always possible

Upcasting

Does every object have a toString method and an equals method? Where do they come from? How are they used? Is it appropriate to override these methods?

Yes, because these two methods are defined in the Object class; therefore, they are available to all Java classes. The subclasses usually override these methods to provide specific information for these methods.The toString() method returns a string representation of the object; the equals() method compares the contents of two objects to determine whether they are the same.

public String toString( ) What does this return

a string that describes the object

Can the subclass circle have its own datafields and methods

yes

Can super be used to access methods other than constructors

yes super.method(arguments)

What problem arises in compiling the program in (b)? (b) class A { public A(int x) { } } class B extends A { public B() { } } public class C { public static void main(String[] args) { B b = new B(); } }

(b) The default constructor of B attempts to invoke the default of constructor of A, but class A's default constructor is not defined. Hide AnswerRead Answer

What are the two notations for creating an array list

1) ArrayList<AConcreteType> list = new ArrayList < AConcreteType> 2) ) ArrayList<AConcreteType> list = new ArrayList <>( )

What are the three pillars of OPP

1) encapsulation 2) inheritance 3) polymorphism

What are the two ways to use the word super

1) to call a superclass constructor 2) To call a superclass method

Are private data fields in a super class able to be used directly in a subclass

No because they aren't accessible outside of its own class.

Is a subclass a subset of the superclass

No, because it also contains its own methods and data fields

Can java do multiple inheritance

No, only single

What is the more general class called

Super class

How can you invoke the hidden method in the super class

SuperClassName.staticMethodName

What keyword do you use to define a subclass?

The extends keyword is used to define a subclass that extends a superclass.

If you are using super to invoke a superclass's constructor where must the line using super be

The first line of the subclasses constructor

What requirements are needed to override a method

The method must be defined in the subclass and use the same signature as in the superclass the return type must be the same or compatible

Once again, what is needed to override a method?

The method must be defined in the subclass using the same signature and have the same or compatible return type

public class TestOverriding{ public static void main(String [] args){ A a = new A(); a.p (10); a.p(10.0); } } class B { public void p (double i){ System.out.println(i *2); } } class A extends B { public void p (double i){ System.out.println(i); } }

This code is overriding. Class A is overriding method p because its the same signature and a compatible return type.

public class TestOverriding{ public static void main(String [] args){ A a = new A(); a.p (10); a.p(10,0); } } class B { public void p (double i){ System.out.println(i *2); } } class A extends B{ public void p (int i){ System.out.println(i); } }

This shows overloading. Class A uses the same method name but changes the signature.

What does overriding mean

To provide a new implementation for a method in the subclass

Overriden methods are in _____________ classes related by _____________________

classes inheritance

Does the actual or declared type of the reference table decide which method to match

declared type

What type of casting needs explicit casting

downcasting

If a static method is redefined in a subclass the method in the super class is

hidden

A static method is _________ but cant be _________________

inheritable overridden

The object to be cast needs to be an ______________ of the subclass

instance

Whats a good way to check if an object is an instance of another object

instanceof operator

Overloaded methods have the same ___________ but different ________________

name parameter lists

Can you assign new int[50], new Integer[50], new String[50], or new Object[50], into a variable of Object[] type?

new int[50] cannot be assigned to into a variable of Object[] type, but new Integer[50], new String[50], or new Object[50] are fin

What is o's declared type Object o = newGeometricObjecy( ); System.out.println (o.toString( ));

object

What is one of the main differences between an array and an arraylist

once an array is created the size is fixed

Where can access public members

That class or any other class

How do you explicitly invoke a superclass's constructor from a subclass?

Use super() or super(args). This statement must be the first in the constructor in the subclass.

How do you invoke an overridden superclass method from a subclass?

Use super.method() or super.method(args).


Ensembles d'études connexes

PA Fundamentals of Information Security

View Set

Chapter 1 Review (Sapling). Bio 1450

View Set

Unit 6: Individuals with Attention Deficit Hyperactivity Disorder

View Set

Story Elements & Growing Together

View Set

AP2 blood vessels and circulation

View Set

Module 09: Conservation of Energy

View Set

Nutrition and Health Final Exam UIOWA

View Set

Volleyball (Playing area, net height, vocab, and ball, serve, scoring, and rotation)

View Set