Chapter 10

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

Why do we need both interfaces and abstract classes?

A class can only extend one superclass but an interface can be implemented by multiple classes.

Default methods

A default method is an interface method that has a body. The method header begins with the key word default if the interface method has a body.

What is an abstract method?

A method that appears in a superclass, but expects it to be overridden in a subclass. An abstract method has only a header and no body. If a subclas fails to override the method, an error will result.

What is a protected member?

A protected member of a class may be directly accessed by methods of th same class or subclass. In addition, they may be accessed by methods of any class that are in the same package.

What is an abstract class?

An abstract class is a class designed with implementation gaps for subclasses to fill in and is deliberately incomplete.

What is an anonymous inner class?

An anonymous inner class is a class that has no name. It is called an inner class because it is defined inside another class.

What is a lambda expression?

An anonymous method, or a method with no name tha creates an object that implements the interface and overrides its abstract method.

Interface in UML

An interface is drawn like a class, except the interface name and the method names are itallicized and the <<interface>> tag is shown above the interface name.

What is a functional interface?

An interface that has one abstract method..

Why use the override annotation?

Because it lets the compiler know that you were trying to override a method, so if you make a mistake with the syntax it will print an error.

Why do you override superclass methods?

Because the subclass is more specialized than the superclass, it is sometimes necessary for the subclass to replace inadequate superclass methods with more suitable ones.

What is inheritance?

Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends.

What happens when a class does not use the extends key word to inherit from another class?

Java automatically extends it from the Object class in the java.lang package

Overloading vs Overriding

Overloading: Two methods with the same name but different parameter types -Compiler decides Overriding: Two parameters with same name AND same parameter types -Runtime system decides

What happens because an anonymous inner class is written inside a method?

The class can access the method's local variables, but only if they are final or effectively final.

What does the extends keyword indicate?

The class extends another class ( superclass) the superclass name is listed after the word extends

Explain the properties of fields in interfaces

The fields in an interface are treated as final and static. And because they are final, you must immediately provide an initialization value.

If you implement an interface what happens to the object of the class?

The objects of these classes may be referenced by a Interface1 reference variable.

What is the super keyword?

The super keyword refers to an objects superclass and can be used to access members of the super class.

Relationship between superclass constructor and subclass constructor

The superclass constructor always executes first

What happens if an anonymous inner class extends a superclass?

The superclass's no arg constructor is called when the object is created.

Abstract classes in UML

They are the same as regular classes except the abstract method and classes are italicized.

Define anonymous inner class

new ClassOrInterfaceName() { fields and methods of anonymous class }

Format for implementing multiple interfaces

public class MyClass implements Interface1, Interface2, Interface3

How to write a lambda expression?

x -> x*x

Three guidelines about calling a superclass constructor

- The super statement can only be called from the subclass constructor - The super statement must be the first statement in the sub constructor because the superclass constructor must execute before the sub constructor - If a subclass constructor does not explicitly call a super constructor, Java will automatically call the superclass's default or no-arg constructor

What is an interface?

Essentially a class that contains only abstract methods. An interface cannot be instantiated, instead it is implemented by other classes. When a class implements an interface the class must override the methods that are specified by the interface. Similar to class in declaration but uses interface instead of class keyword.

How to prevent a method from being overriden

If a method is declared with a final modifier.

Why would you use the super keyword?

If the superclass does not have a default constructor or a no=arg constructor. Or if the superclass has multiple overloaded constructors and you want to make sure a specific one is called.

What are the two most helpful methods in the object class?

ToString method returns a reference to a String containing the objects class name, followed by the @ sign, followed by the object's hash code, which is a hexadecimal number. The equals method accepts a reference to an objects as its argument. It returns true if the argument references the calling object.

True or False: The private members of the superclass cannot be accessed by the subclass

True, they cannot, only the public members can be accessed.

Why is it said that an interface is a contract?

When a class implements an interface, it is agreeing to provide all of the methods that are specified by the interface.

Limitations to interface reference variables.

You cannot create an instance of an interface If an interface variable references an object, you can use the interface variable to call only the method that are specified in the inerface.

new ClassOrInterfaceName() { fields and methods of anonymous class }

You use the new operator to both define and anonymous inner class and create an instance of it. The expression creates an objects that is an instance of a class that either extends the specified superclass or implement the specified interface. A reference to the object is returned.

An anonymous inner class must override...

all of the abstract methods specified by the interface it is implementing, or the superclass it is extending.


Set pelajaran terkait

Chapter 14 - Transportation in a Supply Chain

View Set

Human Nutrition and Diet Final Exam Review

View Set

pharmacology NCLEX questions chapters 3-49

View Set

Music Appreciation - Late Midterm #2

View Set

Chapter 19: Family-Centered Care COMBO (may contain duplicates)

View Set

QUIZThe Peripheral Nervous System, Chapter 13

View Set