Java Interfaces and Inheritance

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

public, protected

A subclass inherits all of the _______ and _______ members of its parent, no matter what package the subclass is in.

abstract methods, default methods, and static methods

An interface body can contain what 3 types of methods?

instance methods over default methods

Are instance methods preferred over interface default methods, or default methods preferred over instance methods?

No

Are static methods in interfaces inherited?

no; only subclassed

Can abstract methods be instantiated?

create another interface that implements the interface you want to add a method to then other classes could upgrade later, while you use the interface public interface DoItPlus extends DoIt { }

Consider a lot of classes rely on an interface, but you want to add a method. What could you do?

by casting an object as an interface. consider the interface Rockstar: Rockstar cat = (Rockstar)obj1; understand?

Describe how an interface can be used as a type

no. but you can redefine it.

Does a default method from an interface have to be defined within a class the implements it?

DONE

Go write an anonymous class without creating a variable, perhaps invoking a method

Subclass.super.field;

How do you access a hidden field (because of a same-named field in the subclass) in a superclass?

if (obj instanceof Class){ System.out.println(); }

How do you check if an object is an instance of a class?

by creating a field with the same name as its superclass

How do you hide a field in a subclass?

by creating a static method with the same name as its superclass

How do you hide a static method in a subclass?

super(parameter list); where the parameter matches a constructor in the superclass

How do you invoke a constructor from a superclass?

create a method in the subclass with the same name and signature as a superclass

How do you override a method in a subclass?

by using the same signature (name, plus the number and the type of its parameters) in the sublcass

How do you override a method in a superclass?

it must be declared abstract

If a class includes abstract methods, what must be true about the class?

the ability of a class to implement more than one interface

In Java, what is multiple inheritance of type?

modifier, interface keyword, name, parent interfaces, body

In order, an interface declaration consists of: (M. 'I'. N. PI. B)

Abstract Class

Interface or Abstract Class: 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).

Interface

Interface or Abstract Class: You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.

Interface

Interface or Abstract Class: You want to take advantage of multiple inheritance of type.

contract

Interfaces are a ________ that a given class will have certain methods, and constants.

this is allowable, only if object1 and object2 are instances of a class that implements Relatable

Object object1; Object object2; Relatable obj1 = (Relatable)object1; Relatable obj2 = (Relatable)object2; (obj1).isLargerThan(obj2) == 0

yes, if the object is instantiated from a class that implements that interface

Once you've cast an Interface type within a method to an Object type, can you invoke its methods?

reference

Similar to a class, an interface is a _______ type, that can contain only constants, method signatures, default methods, static methods, and nested types.

it would break all the classes that had it; methods would have to be made in all those classes

Sometimes you have a set number of methods in an interface, that a lot of classes already rely on. What would happen if you just added another method to your interface?

FALSE

TRUE or FALSE: a subclass inherits its superclass' private methods and fields

the more specialized they are

The lower down on the tree you make classes-

checks if the references are equal; to see if they're the same object

What does Object's default equals() do?

allows methods defined in the interface to be called through Interface type reference, but not instance variables

What does a static method do for an interface?

all the fields, methods, and nested classes

What does a subclass inherit from its superclass?

A class that implements an interface must implement all the methods declared in the interface.

What does an interface mean for a class?

it means the method cannot be overridden by subclasses

What does declaring a method final do, regarding inheritance?

calls the default constructor of the superclass

What does the keyword super() do?

it will be treated like a nondefault (non-static) method, like normal. classes that implement it will require it

What happens if you extend an interface with a default method, then redefine the method?

the new interface inherits that default method

What happens if you extend an interface with a default method?

that class cannot be subclassed

What happens if you use final on a class?

constructors are not inherited, but can be called within the subclass

What happens to constructors when a subclass extends a superclass?

compiler error; change name of method

What happens when a class inherits two independently (not inherited from each other) defined default methods, with the same name and signature? What's the solution?

subclass overrides

What happens when a subclass has the same signature as a superclass' method?

compile-time error

What happens when a subclass' static method has the same signature as a superclass' instance method?

hides superclass' static method so it gets invoked whether its from the superclass or subclass

What happens when a subclass' static method has the same signature as a superclass' static method?

a superclass

What is a class from which a subclass is derived called?

a subclass

What is a class that is derived from another class called?

a subtype of the return type of the method being overridden

What is a covariant return type?

a method defined (using the default keyword) that predefines a method in an interface

What is a default method?

a method associated with the class in which it is defined rather than any object

What is a static method?

MountainBike myBike = (MountainBike)obj; or (float)4 or (char)5 or (Class)object or (Type)variable

What is explicit casting?

creating an Object object, making it a new Class() it is a widening conversion; it gains attributes, doesn't lose them

What is implicit casting?

subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class

What is polymorphism in Java in a nutshell?

interfaces

What is the best way to implement same-named methods from multiple sources?

interfaces can only have public final and public static fields

What is the difference regarding fields, with classes and interfaces?

Object

What is the one class that all other classes are derived from?

the subclass must provide all abstract methods; or else be declared abstract too

What is true for abstract methods of an abstract class, when the class is extended?

the class it's based on must implement Interface Cloneable

What must be true in order to clone an existing object?

use inheritance! derive your new class from the existing class

What should you do if you want to create a new class, but an existing class has some code that you want?

@Override

What special thing signals to the compiler to override a method?

y's overridden method

When Interface x has a default method, and Interface y extends and rewrites the method, and Interface z extends but doesn't rewrite the method, what does a class that inherits x and y use when invoking the default method?

whenever a type can be used. think about it.

When can an interface name be used?

when equality of two objects is not based on reference, but perhaps some value

When might you want to override equals()?

invoke methods from that interface, assuming the object passed is an instance of a class that implements the Interface

When you cast an Object object to an Interface type, what can you do?

calling a constructor of a superclass

When you create an instance of a subclass, what else are you doing?

a principle in biology in which an organism or species can have many different forms or stages

Where does the term polymorphism come from?

java.lang package

Where is Object located?

the issue of multiple inheritance of state: an object will inherit fields from all that classes' superclass'

Why can't you extend more than one class to a class?

it still works. the java compiler provides rules to determine which default method to use.

With a default method, what happens if you inherit multiple of them into a class?

its package

With a private modifier, what is an interface only accessible by?

static initialization block static{ code; }

besides from constructors, how could you execute a block of code right as a class is instantiated?

no

can abstract classes be instantiated?

yes; it doesn't have to have have all its methods; unless you extend that abstract class, then it has to contain its interface's methods

can an abstract class implement an interface? what happens?

yes; and classes that extend the abstract class will get those methods, but there needs to be an abstract method in the abstract class

can you define methods in an abstract class?

yes; if it's static

can you invoke a method from an abstract class right off the bat?

MyClass is derived from a superclass, that has a shout() method

explain MyClass.super.shout();

creates and returns a copy of this obect

protected Object clone() throws CloneNotSupportedException

Returns a string representation of the object.

public String toString()

Indicates whether some other object is "equal to" this one.

public boolean equals(Object obj)

Returns the runtime class of an object.

public final Class getClass()

Returns a hash code value for the object.

public int hashCode()

an abstract class can have fields that are non-static and non-final

regarding fields, what's the difference between an interface and an abstract class?

when they only have static or constants, or there are no shared fields in the classes implementing it

regarding fields, when would you implement an interface?

the ability to inherit method definitions from multiple classes

what is 'multiple inheritance of implementation'?

a class that contains one or more abstract methods

what is an abstract class?

a method that is declared, but contains no implementation; they require subclasses to provide implementations

what is an abstract method?

The static method in Animal The instance method in Cat

what is the output?

public

what level of inheritance do all automatically interfaces have for their methods?

static and final

what sorts of fields can you declare in an interface?


Set pelajaran terkait

CISR Insuring Commerical Property

View Set

The Changing Environments of Organization Chapter 2

View Set

SOC 350 Exam 1 Key Concepts Chapter 2

View Set

Chapter 5: Adult Health and Nutritional Assessment Prep-U

View Set

Experiment 12: The Dehydration of Cyclohexanol

View Set