Java SE 11 Programmer I Exam - Chapter IX

Ace your homework & exams now with Quizwiz!

List the 7 abstract class definition rules.

(1) Abstract classes cannot be instantiated. (2) All top-level types, including abstract classes, cannot be marked protected or private. (3) Abstract classes cannot be marked final. (4) Abstract classes may include zero or more abstract and nonabstract methods. (5) An abstract class that extends another abstract class inherits all of its abstract methods. (6) The first concrete class that extends an abstract class must provide an implementation for all of the inherited abstract methods. Even from 'grand parent' abstract classes. (7) Abstract class constructors follow the same rules for initialization as regular constructors, except they can be called only as part of the initialization of a subclass. Meaning you cannot do: MyAbstractClass myAbstractClass = new MyAbstractClass();

What are the 4 abstract method definition rules?

(1) Abstract methods can be defined only in abstract classes or interfaces. (2) Abstract methods cannot be declared private or final. (3) Abstract methods must not provide a method body/implementation in the abstract class in which they are declared. (4) Implementing an abstract method in a subclass follows the same rules for overriding a method, including covariant return types, exception declarations, etc.

How do you instantiate an abstract class?

An abstract class can be initialized, but only as part of the instantiation of a nonabstract subclass. For example: public abstract Mammal {} public Elephant extends Mammal {} Mammal elephant = new Elephant();

What's an enum or enumeration?

An enum is a specialized type that defines a set of fixed values. public enum Color { RED, YELLOW, BLUE, GREEN, ORANGE, PURPLE }

Can a method be marked both static and abstract?

No. A static method cannot be overridden. It is defined as belonging to the class, not an instance of the class. If a static method cannot be overridden, then it follows that it also cannot be marked abstract since it can never be implemented.

Are interfaces required to define any variables or methods?

No. This compiles: public abstract interface WalksOnTwoLegs {}

Do interfaces inherit java.lang.Object?

No. if they did, Java would support true multiple inheritance.

Which methods does an abstract class have to implement if it implements an interface?

None. An abstract class is not required to implement the methods inherited from an interface. A regular class would be required to do that.

What access modifiers need to be in front of the methods in the child classes that implement an interface?

Public. Because abstract methods in an interface are considered public by default (even if they don't have an access modifier written out).

Why does this not compile? Integer tickets = 6; if(tickets instanceof List) {}

The compiler rejects this code because the Integer class is marked final and does not inherit List . Therefore, it is not possible to create a subclass of Integer that inherits the List interface.

What does this print? List a = new ArrayList(); System.out.println(a instanceof List);

True.

List the 5 abstract interface method rules.

(1) Abstract methods can be defined only in abstract classes or interfaces. (2) Abstract methods cannot be declared private or final. (3) Abstract methods must not provide a method body/implementation in the abstract class in which is it declared. (4) Implementing an abstract method in a subclass follows the same rules for overriding a method, including covariant return types, exception declarations, etc. (5) Interface methods without a body are assumed to be abstract and public.

What are the three ways of inheriting an interface?

(1) An interface can extend another interface. (2) A class can implement an interface. (3) A class can extend another class whose ancestor implements an interface.

How do these differ? abstract class Husky { abstract void play(); } interface Poodle { void play(); }

(1) Both of these method definitions are considered abstract. That said, the Husky class will not compile if the play() method is not marked abstract , whereas the method in the Poodle interface will compile with or without the abstract modifier. (2) Even though neither has an access modifier, they do not have the same access level. The play() method in Husky class is considered default (package-private), whereas the method in the Poodle interface is assumed to be public . This is especially important when you create classes that inherit these definitions.

What are the 2 main rules for interface variables.

(1) Interface variables are assumed to be public, static, and final. (2) Because interface variables are marked final, they must be initialized with a value when they are declared.

List the three places the JVM will insert an implicit modifier for you regarding interfaces.

(1) Interfaces are assumed to be abstract. (2) Interface variables are assumed to be public, static, and final. (3) Interface methods without a body are assumed to be abstract and public.

List the 8 interface definition rules.

(1) Interfaces cannot be instantiated. (2) All top-level types, including interfaces, cannot be marked protected or private. (3) Interfaces are assumed to be abstract and cannot be marked final. (4) Interfaces may include zero or more abstract methods. (5) An interface can extend any number of interfaces. (6) An interface reference may be cast to any reference that inherits the interface, although this may produce an exception at runtime if the classes aren't related. (7) The compiler will only report an unrelated type error for an instanceof operation with an interface on the right side if the reference on the left side is a final class that does not inherit the interface. (8) An interface method with a body must be marked default , private , static , or private static (covered when studying for the 1Z0-816 exam).

What are the two rules that valid for both class and interface declarations?

- A Java file may have at most one public top-level class or interface, and it must match the name of the file. - A top-level class or interface can only be declared with public or package-private access.

What's an abstract class and an abstract method?

- An abstract class is a class that cannot be instantiated and may contain abstract methods. - An abstract method is a method that does not define an implementation when it is declared. - Both abstract classes and abstract methods are denoted with the 'abstract' modifier.

What is an interface?

- An interface is a class that only includes method signatures. The methods are not implemented in the interface. Another class must be created to supply the implementation. - An interface can also include constant variables. - Both abstract methods and constant variables included with an interface are implicitly assumed to be public .

Why does this compile? Number tickets = 5; if(tickets instanceof List) {}

- Even though Number does not inherit List , it's possible the tickets variable may be a reference to a subclass of Number that does inherit List. - As an example, the tickets variable could be assigned to an instance of the following MyNumber class: public class MyNumber extends Number implements List

Why is this correct? abstract class Mammal { abstract void showHorn(); abstract void eatLeaf(); } abstract class Rhino extends Mammal { void showHorn() {} } public class BlackRhino extends Rhino { void eatLeaf() {} }

- In this example, the BlackRhino class is the first concrete subclass, while the Mammal and Rhino classes are abstract. The BlackRhino class inherits the eatLeaf() method as abstract and is therefore required to provide an implementation, which it does. - Since the parent class, Rhino , provides an implementation of showHorn() , the method is inherited in the BlackRhino as a nonabstract method. - For this reason, the BlackRhino class is permitted but not required to override the showHorn() method. - Since Rhino is ALSO an abstract class that extends and abstract class, then Rhino is NOT required to implement showHorn() and eatLeaf(). However, if it was a regular class, it would have to implement both.

Why are interface variables public, static, and final?

- Interface variables are static because java interfaces cannot be instantiated on their own. The value of the variable must be assigned in a static context in which no instance exists. - The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned. In other words, interfaces can declare only constants, not instance variables.

What's a default method?

- Interfaces can include default methods. - A default method is one in which the interface method has a body and is not marked abstract. - Default methods were included for backward compability.

Is an abstract class required to include abstract methods?

- No. - It can hold all the same members as any other class, and does not need to have any abstract methods in order for it to compile. - However, and abstract method can ONLY be defined in an abstract class.

What if two inherited interfaces define exactly the same abstract method but the parameters are different - which one do you have to implement?

- The class will still just have to implement both. - If the method name is the same but the input parameters are different, there is no conflict because this is considered a method overload. - Meaning the class will then have two methods of the same name but with different parameters (i.e. correct overloading).

How does the compiler interpret a class method without an access modifier vs an interface method without an access modifier?

- When working with class members, omitting the access modifier indicates default (package-private) access. - When working with interface members, though, the lack of access modifier always indicates public access.

What access modifiers are allowed for inner classes?

A member inner class can be declared with all of the same access modifiers as a class member, such as public , protected , default (package-private), or private .

What class members are allowed for an inner class?

A member inner class can contain many of the same methods and variables as a top-level class. Some members are disallowed in member inner classes, such as static members, although you don't need to know that for the 1Z0-815 exam.

What's a concrete class?

A non-abstract subclass that usually extends and abstract class.

What interface members are included in this exam?

Abstract methods and constant variables.

What are the rules around extending an abstract class?

All abstract methods in the abstract class MUST BE overridden in the subclass. Otherwise you get a compile error.

What's an implicit modifier?

An implicit modifier is a modifier that the compiler automatically adds to a class, interface, method, or variable declaration. For example, an interface is always considered to be abstract , even if it is not marked so. These two are identical: public abstract interface WalksOnTwoLegs {} public interface WalksOnTwoLegs {}

Are are the two key differences between an abstract class and an interface?

An interface does not follow the same rules for single inheritance and instance initialization with constructors, as a class does.

What if two inherited interfaces define exactly the same abstract method but the return types are different - which one do you have to implement?

As long as the two return types are covariant and the return type in the child class is a covariant of BOTH methods in the two interfaces, then it's okay. For example: - If methodA in interface A returns a String, and - methodA in interface B returns a CharSequence, then - in the child class that implements interfaces A and B, the methodA MUST return a String, because String is a covariant of String and CharSequence.

Why can you not mark an interface final?

Because then it means no class could implement it.

Why are interface variables referred to as constants?

Because they are assumed to be public, static, and final.

Can abstract class methods be private, package-private, or protected?

Can't be private, can be any of the other modifiers.

What's the difference between an interface extending another interface vs a class implementing an interface?

Class 'implements' an interface but an interface 'extends' an interface.

What if two inherited interfaces define exactly the same abstract method but the return types are different and not covariant of one another - which one do you have to implement?

Compile error because there is no way to implement a method that would support both return types of the two interface methods.

What does this print? List a = new ArrayList(); System.out.println(a instanceof String);

Compile error. Java recognizes that the variable 'a' and String are incompatible types.

What does this print? List a = new List(); System.out.println(a instanceof List);

Compile error. List is an abstract class and cannot be instantiated.

Are abstract methods in an abstract class allowed to have a body?

No.

Is this a valid cast here on line 8? 1: interface Canine {} 2: class Dog implements Canine {} 3: class Wolf implements Canine {} 4: 5: public class BadCasts { 6: public static void main(String[] args) { 7: Canine canine = new Wolf(); 8: Canine badDog = (Dog)canine; 9: } }

In this program, a Wolf object is created and then assigned to a Canine reference type on line 7. Because of polymorphism, Java cannot be sure which specific class type the canine instance on line 8 is. Therefore, it allows the invalid cast to the Dog reference type, even though Dog and Wolf are not related. The code compiles but throws a ClassCastException at runtime.

What are covariant return types?

It is possible to have different return type for a overriding method in child class, but child's return type should be sub-type of parent's return type.

What does it mean to declare a class final?

It means that class cannot be extended.

What does it mean to implement an abstract method?

It means that in the subclass we are basically overriding the method of the parent class.

What's an inner class?

It's a class defined inside another class. I.e. it's on the same level as class variables and methods.

Can a class be marked both final and abstract?

No - because 'final' prevents a subclass from implementing it, while 'abstract' requires it.

Can a class be marked both private and abstract?

No - because 'private' prevents a subclass from implementing it, while 'abstract' requires it.

What is the order of the access and final/abstract modifiers?

No order - can be either or. All these are correct: public abstract Mammal {} abstract private Mammal {}

Can abstract classes or interfaces be instantiated?

No.

How can interface variables be accessed?

Since they are static, they can be used outside the interface declaration without requiring an instance of the interface.

What's the benefit of using an inner class?

The advantage of using a member inner class is that the outer class can manage the complete life cycle of the inner class.

What if two inherited interfaces define exactly the same abstract method - which one do you have to implement?

The class will still just have to implement one (assuming that both abstract methods in the two interfaces are actually identical).

When does the compiler not allow a cast from an interface reference to an object reference?

The compiler does not allow a cast from an interface reference to an object reference if the object type does not implement the interface.

What is the difference between an abstract class and non-abstract class constructors?

The primary difference between a constructor in an abstract class and a nonabstract class is that a constructor in abstract class can be called only when it is being initialized by a nonabstract subclass. This makes sense, as abstract classes cannot be instantiated.

What are the 4 primary differences between an interface and an abstract class?

The primary differences between the two are that: - interfaces include implicit modifiers, - interfaces do not contain constructors, - interfaces do not participate in the instance initialization process, and - interfaces support multiple inheritance.

When overriding a method - what do you need to remember about access modifiers?

The subclass cannot reduce the visibility of the parent method. Meaning the access modifier cannot be narrower in scope in the subclass then it is in the parent class.

A top level type like a class, interface, or enum can use which access modifiers?

They can only be marked public or default (i.e. package-private).

Can a non-abstract method in an abstract class call an abstract method?

Yes.

Can an abstract class extend a non-abstract class?

Yes.

If a class extends an abstract class, can the object created from that class later be cast to the abstract class?

Yes.

Does a class also have to implement methods from the interface that its parent extends? For example here: Class A extends abstract Class B Interface B extends Interface C

Yes. Class A will have to implement abstract methods from Interface C as well.

What if two inherited interfaces define exactly the same abstract method but the return types are different and not covariant of one another - what do you implement in a child interface that extends both of them?

You can't, there's a compile error. Same thing if a class implemented these two interfaces.

What happens when a method is overridden in a subclass? How can you call the method in the super class?

You can't. This program chooses the subclass method at runtime. Even though the implementation of the overridden method is defined in the super class, the fact that it is overridden in the subclass means it is replaced everywhere, even in the parent class. So regardless how you call the method - Java will always choose the subclass one: SuperClass.myMethod() <== can't be accessed if overridden. SubClass.myMethod() <== even if you call SuperClass.myMethod(), Java chooses this one. Or unless you have a method in your subclass that calls super.myMethod().

What happens regarding method access if you implicitly cast a class to it's parent class? I.e: Whale whale = new Orca() ==> where Orca extends Whale

You won't have access to Orca's methods or variables.

The rules covered for this exam about inner classes apply to both class and interface types.

ok.

How do you declare/write an abstract method?

public abstract String myMethodName(); No need for braces {}. But semicolon is needed.

How do you declare an interface?

public abstract interface WalksOnTwoLegs {}


Related study sets

APUSH unit 1 assessment study guide.

View Set

Soc. Psych first two reviews ch. 1-8

View Set

Карпюк 9 клас, U1 "Who are you?", L1 "Vital statistics"

View Set

Rotter, Internal vs external locus of control

View Set

Introduction to Supply Chain Management Test 3 Review

View Set