Chapter 10: Inheritance

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Why do we have interfaces and abstract classes if they're so similar to each other?

A class can only extend 1 superclass, but a class can implement multiple interfaces

What is an interface?

A class that contains only abstract methods; implemented by other classes

T/F: By default, al members of an interface are public

True

Define an abstract method

a method that appears in a superclass, but expects to be overridden in a subclass; has only a header and no body Format: AccessSpecifier abstract ReturnType MethodName(Parameter List); Ex: public abstract void setValue(int value);

How do you write an instanceof expression and what does it return?

- refVar instanceof ClassName - Dog instanceof Animal - returns a boolean; true if refVar is an instance of ClassName, false if not

Explain: IntCalculator square = new IntCalculator() { public int calculate(int number) { return number * number; } };

- square is referencing an anonymous class - The anonymous class is implementing the IntCalculator class

Write a lambda expression that accepts 1 int variable x, assigns the product of (2*x) to int a, and returns a

(x) -> { int a = x * 2; return a; };

Explain: IntCalculator square = x -> x * x;

- (IntCalculator) object automatically implements the IntCalculator interface (functional interface - only has 1 abstract method) - (square) reference variable - (x) parameter ; don't need to specify parameter type - (x*x) the value that is returned from the calculate method

Explain the rules of referencing objects in chain of inheritance

- An object can only reference classes that extend it - A subclass cannot reference its superclass - A subclass cannot reference another subclass

- The GradedActivity class is a superclass - score is declared as a protected double variable within the GradedActivity class What has access to the score variable?

- Any methods declared within the GradedActivity class - Any class that inherits from the GradedActivity class

How do you use an interface?

- Use the implements key word (instead of extend) - class ClassName implements InterfaceName class Chihuahua implements DogInterface

What is the difference between interfaces and abstract classes?

- You can have a list of implemented methods AND abstract methods in an abstract class - You can ONLY have abstract methods in an interface

Superclass: Animal Subclasses: Dog and Cat Explain why these statements are illegal: 1) Dog myDog = new Animal() 2) Cat myCat = new Dog()

1) A subclass cannot reference a superclass (an animal is not necessarily a dog) 2) A subclass cannot reference another subclass (a cat is not a dog)

Explain 4 requirements & restrictions of anonymous inner classes

1) Must either implement an interface or extend another class 2) If it extends a superclass, the superclass's no-arg constructer is called when the object is created 3) Must override all of the abstract methods specified by the interface/superclass it's implementing/extending 4) Class's definition is written inside a method, so all of that method's local variables must be final

Define an abstract class

Serves as a superclass for other classes; not instantiated itself; cannot be directly called

What is a superclass and what is a subclass?

Superclass: the general class Subclass: the specialized class that inherits fields & methods from the superclass and has it's own additional characteristics that make it special

Superclass: Animal Subclass: Dog (Both have a showSpecies method) Animal myAnimal = new Dog() myAnimal.showAnimal(); Which class's showSpecies method will execute?

The Dog class's because the myAnimal reference variable is referencing a Dog object

How does the JVM determine which overridden method to call?

The JVM determines which method to call based on the type of the object that is being referred to NOT the type of the reference variable Animal myAnimal = new Dog(); - JVM will execute the overridden method from the Dog class

Write a lambda expression with no parameters that prints out 1 space line

() -> System.out.println();

Write a lambda expression with 3 parameters (int a, int b, int c) that returns the sum of all the parameters

(a, b, c) -> a + b + c;

What is a lambda expression and what is it's general format?

- An anonymous method - parameter -> expression

Which methods of the interface are required to be instantiated in a class that implements an interface?

All of them. All methods of an interface are abstract, meaning they must be instantiated

Superclass: Animal Subclass: Dog and Cat public static void animalInfo(Animal creature) Dog myDog = new Dog(); Cat myCat = new Cat(); True/False: These statements are not legal because the animalInfo method only accepts objects of type Animal animalInfo(Cat); animalInfo(Dog);

False: The Dog and Cat classes extend the Animal class, meaning a Dog/Cat object is also an Animal object, therefore the animalInfo method can accept Dog and Cat objects

True/False: Only classes that don't extend another class extend from the Object class

False: all classes directly or indirectly extend from the Object class

True/False: the super statement that calls the superclass constructor can be written in any method

False: it can only be written in the subclasses's constructor

Where should the super statement that calls the superclass constructor be written?

In the 1st statement in the subclass's constructor

Is this legal? Why/why not public interface Doable{ int FIELD1; int FIELD2; (Method headers...) }

No. Doable is an interface, so any of it's fields must be initialized

Does a default method also need to be overridden when a class implements an interface containing a default method?

No. It can be, but it doesn't have to

When does dynamic binding occur

Occurs when you use a reference variable that refers to an object and you use that reference variable to call an overridden method

What subclass methods are accessible to a superclass

Only the methods that the superclass knows about; overridden methods or inherited methods

Define overriding

Overriding allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses

How do you implement a functional interface?

Using a lambda expression

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

Using the instanceof operator

Superclass: Animal Methods: showSpecies, makeSound Subclass: Dog Methods showSpecies, makeSound, doTrick Animal myAnimal = new Dog(); Which statement causes an error? a) myAnimal.showSpecies() b) myAnimal.makeSound() c) myAnimal.doTrick() d) none of the above

c) myAnimal.doTrick - myAnimal is an Animal object that references a Dog object - Since myAnimal is an Animal object, it only has access to the shared methods and inherited methods

How do you implement multiple interfaces in a class?

list the names of the interfaces, separated by commas, after the implements key word public class MyClass implements Interface1, Interface2, Interface3

What is the general format for instantiating and defining an anonymous inner class?

new ClassOrInterfaceName() { (Fields & methods of class) }

What is the general format for an interface?

public interface InterFaceName{ (Method headers...) }

How do you call a superclass constructor?

super( ) or super(argument)

What is a default method? How do you write one?

An interface method that has a body - begins with key word default followed by the return type and then the method name default void display()

What is a functional interface?

An interface with exactly one abstract method

What has access to a variable with the access specifier default?

Any method in the same package

How can you prevent a method from being overridden?

Declare the method with the final modifier public final void message( )

What is the difference between an inner class and an anonymous inner class?

Inner class: a class that's defined inside another class Anonymous inner class: an inner class that has no name; must implement an interface, or extend another class

What happens if a subclass constructor doesn't explicitly call a superclass constructor?

Java will automatically call the superclass's default constructor, just before the code in the subclass's constructor executes

What is the difference between overloading and overriding?

Overloading: when 2 methods have the same name, but different signatures (different parameter lists) Overriding: when a method in a subclass has the same signature as a method in the superclass

When does polymorphism occur?

When a superclass reference variable references objects of a subclass - Animal = superclass - Dog = subclass Animal myAnimal = new Dog()

Can an interface contain field declarations?

Yes, but all fields are treated as final and static, and you must provide an initialization value


संबंधित स्टडी सेट्स

Unit 2: Physiology Mastering Ch 7, 8, 9, 10

View Set

ECON 4210 Final Exam Review: Exercise Questions

View Set

Introduction to Business Chapter 2

View Set

MSPA I Gustar: Pregunta, pregunta, cambia

View Set

BECOMING HUMAN: Nature vs. nurture

View Set