Lecture 9 Flashcards
What is inheritance?
Inheritance allows us to create classes that are subclasses of other classes.
What are three different ways of describing: “A inherits B”
A is a subclass of B, A is derived from B, A is the child of B.
What are two different ways of describing: “B is inherited by A”
B is a superclass of A, B is the parent of A.
What two things are true about an object of class A, if class A inherits class B?
Every object, that is an instance of the subclass has all the properties from the superclass On such an object, you can call all methods of the superclass.
What is the: “Liskov substitution principle”?
If A is a superclass of B, then everywhere in the program where an object of Class A is requested, an object of class B is also accepted.
How do you define in Java that A is a subclass of B?
In the signature of A add “extends B”
What can access something that is ‘public’?
everything
What can access something that is ‘private’?
Only things inside the class
What can access something that has no access type specified?
The class and the package
What can access something that is ‘protected’?
The class, the package, and subclasses.
How can you call the constructor of a parent?
super()
What is the concept of Polymorphism
Everywhere where a class X is allowed, a subclass Y of x is also allowed, but NOT the other way around.
When can you get a ‘ClassCastException’?
When you try to cast something to a class, which it can’t be cast to.
What is method overriding?
Creating a method with the same signature as an already existing method in the inheritance-architecture but in a different class of the inheritance-architecture.
What is method overloading?
Creating a method with the same name as another method inside the class (or inheritance-architecture), but with a different parameter list (different amount, types or order).
What is dynamic binding?
Dynamic binding means that an object can be an instance of multiple classes (of a certain inheritance-architecture), but that it is unknown to the compiler (a.k.a. depending on circumstances) of what class this object is an instance.
If there is dynamic binding, who “decides” what class an object is an instance of?
the JVM
What is the superclass of all classes in java?
Object
What does the final keyword do if added to an attribute?
It turns the attribute into a constant
What does the final keyword do if added to a class?
It prevents you from making subclasses
What does the final keyword do if added to a method?
It prevents you from redefining the method in subclasses
What do you use an abstract class for?
To “combine” classes, by creating a class for a category to which they all belong, even though this category itself can’t “exist” in the real world.
What does the abstract keyword do if added to a class?
It prevents you from creating instances of this class
What does the abstract keyword do if added to a method?
It forces you to implement this method, when creating a subclass.
When can you use the abstract keyword on methods? How do you define these methods?
You can only use it on methods in abstract classes. You don’t define them at all. abstract exampleMethod(). NO {}
What keyword do you add to a class signature to signify that it uses an interface?
implements InterfaceNameHere
What are the two differences between an interface and an abstract class?
an interface cannot have attributes, an abstract class can. an interface cannot have implementations for methods, an abstract class can.
Why would we use an interface and not an abstract class?
A class can only inherit from one other class, (so if it already inherits something else, it can’t inherit an extra abstract class), but they can implement as many interfaces as you’d like.
What keyword is “invisibly” added to all methods in an interface?
abstract, (YOU DON”T ADD THIS YOURSELF!!!)