Chapter 10 - Improving structure with inheritance Flashcards
What is the
principle of inheritance
in Java?
this in Java is an abstraction technique that allows us to categorize things based on shared common elements and unique elements.
from the following classes create an example
Inheritance hierarchy
Classes: animal, mammal, dog, cat, poodle, dalmation
example:
animal is the superclass, mammal is a subclass, and dog and cat are subclasses of mammal, and poodle and dalmatian are subclasses of dog.
describe the 6 steps that take place when an object is initialised
1.Sufficient memory for the new object is allocated.
2.The object’s fields are given default ‘zero’ values: Reference type fields are assignednull, numerical type primitive fields are assigned0, floating point primitives are assigned0.0andbooleanfields are assignedfalse. Achartype field is also assigned a value of0that is equivalent to the null Unicode character.
3.The constructor is called. This might involve also calling another constructor in the same class as the first line of code in the constructor, usingthis().
4.A parent class constructor is called (except for classObject), either explicitly usingsuper()or implicitly.
5.Any initialisation of fields that takes place outside of the constructor (e.g. static constant initialisation) is performed.
6.The rest of the constructor code is executed.
What is a
subtype
in Java?
In Java, this is a type that has a supertype. This may be substituted in places where its supertype has been declared or is expected
this in Java is a variable that can reference different object types at runtime, including its declared type and any subtypes of the declared type.
What is a
polymorphic variable
in Java?
How is
code duplication
a waste of time?
this is a waste of time because creating duplicate code for a new feature that has common code to an existing feature is inefficient.
With inheritance in Java, what are subtypes?
these in Java are the subclasses of a superclass.
What is a
class diagram
used for and what information will it illustrate?
this diagram is used to illustrate the structure of a system by showing the classes in the system
information included would be:
1. Class name
2. Class fields
3. Class methods
this would be a wise decision if we have two constructors that have identical code.
in this case we can have one constructor that sets the duplicate code while the other can set its unique values and call the other constructor for any duplicate code
when would it be wise to
reuse constructor code
What is
code duplication
this is where similar data or operations are stored in multiple parts of the source code.
this is a constructor placed within the bytecode by java if the class is inheriting and the programmer has not explicitly called its superclass constructor
note:
the call is a zero argument call to the superclass constructor i.e. super()
describe the
The default constructor
give 3 notes about calling
this() within a constructor
notes include:
1. must be the first line of code in a constructor
2. It will find and call a constructor with the matching signature in the same class
3. may not be used in the same constructor calling super()
these include:
1. Avoids code duplication - identical source code is avoided
2. Code reuse - existing code can be reused. E.g. if a class already exists that contains similar functionality we may be able to create a subclass of this instead of rewriting the code again
3. Easier maintenance - maintenance is easier changes can be made in one place that may be shared by many classes
4. Extendibility - adding new funcionality can be much easier when inheritance has been implemented. E.g. we could add a new feature as a subclass of an already existing and similar superclass
what are 4 advantages that come from using
inheritance
describe the
this() keyword
when this is used within a constructor it will search for a constructor in the same class with a matching signature
this in Java is a structure where many classes are linked through an inheritance relationship, and subclasses are themselves superclasses for other subclasses.
What is an
inheritance hierarchy
in Java?
What is a
**ClassCastException **
in Java?
this in Java occurs when the java runtime system checks whether or not a variable really holds an object of a certain type, and it turns out that the variable actually holds a different subtype other than the one specified in the cast operator.
This can occur if the type specified in the cast operator is incorrect or if the variable actually holds a different subtype.
describe the
general rule for variables VS the correct rule
- The general rule for variables in Java is that their type must match the type of the object they are initialized with.
- The correct rule for variables in java is that a variable declared with a supertype may be initialised with any subtype of the declared supertype variable
What does the
‘extends’ keyword
in Java do?
this keyword in Java is used to specify that a class is inheriting from a superclass.
3 methods include:
1. toString() - which returns a string representation of the object
2. equals() - which compares the object to another object for equality
3. hashCode() - which returns a hash code value for the object.
name 3 methods provided by the Object class in Java?
Can Java automatically call a superclass constructor for us if we don’t use the ‘super’ keyword?
Yes, Java will automatically call a superclass constructor for us if we don’t use the ‘super’ keyword.
However, this will result in a compile-time error if there is no zero-argument superclass constructor.
What is
inheritance
in Java?
this is a feature in Java that allows us to define a class that extends another class, known as the superclass.
- The general rule for variables in Java is that their type must match the type of the object they are initialized with.
- The correct rule for variables in java is that a variable declared with a supertype may be initialised with any subtype of the declared supertype variable
describe the
general rule for variables VS the correct rule
When might casting be necessary in Java?
this is sometimes necessary when the compiler experiences what is known as “type loss”
What is a
superclass
in Java?
this is a class that is extended by another class and can contain common code that is inherited by its subclasses.
How is
**code duplication **
inflexible?
this is inflexible because changes must be made to every piece of duplicate code if a change is made.
In Java, this refers to the ability to pass a subtype object where a supertype is expected, provided that the subtype given is a subtype of the declared supertype.
What does the concept of
**substitution **
refer to in Java?
this is where similar data or operations are stored in multiple parts of the source code.
What is
code duplication
What is the general rule of
substitution
in Java?
The general rule of this in Java is that you cannot assign a supertype to a subtype.
It may be necessary to perform this if a new subclass is added but it differs from its sibling subclasses in that it does not require or should not have some of the fields that it will inherit from its superclass.
When might it be necessary to
**refactor an inheritance hierarchy **
in Java?
Can public methods of a superclass be accessed by a subclass in Java?
Yes, any public methods of a superclass can be accessed from a subclass using the simple name, as if it were in the subclass itself.
We don’t need to use dot notation and specify the fully qualified name.
The disadvantages of this include:
- inflexibility
- error-proneness
- waste of time
What are the disadvantages of
code duplication?
these in Java are the subclasses of a superclass.
With inheritance in Java, what are subtypes?
notes include:
1. must be the first line of code in a constructor
2. It will find and call a constructor with the matching signature in the same class
3. may not be used in the same constructor calling super()
give 3 notes about calling
this() within a constructor
In Java, when can substitution be used for formal and actual parameters?
In Java, substitution can be used for:
1. formal parameter - can be declared with a supertype
2. actual parameter - can accept the supertype as an argument or any subtype of the expected supertype.
this is a class that is extended by another class and can contain common code that is inherited by its subclasses.
What is a
superclass
in Java?
What are the members of a class in Java?
Members of a class in Java include fields and methods.
What is a
subclass
in Java?
this is a class that extends another class (its superclass) and inherits all of its fields and methods.
this is a class that extends another class (its superclass) and inherits all of its fields and methods.
What is a
subclass
in Java?
describe the
The default constructor
this is a constructor placed within the bytecode by java if the class is inheriting and the programmer has not explicitly called its superclass constructor
note:
the call is a zero argument call to the superclass constructor i.e. super()
What is an
inheritance hierarchy
in Java?
this in Java is a structure where many classes are linked through an inheritance relationship, and subclasses are themselves superclasses for other subclasses.
this is a feature in Java that allows us to define a class that extends another class, known as the superclass.
What is
inheritance
in Java?
How is
code duplication
error-prone?
this is error-prone because someone else making changes later may not realize that there is duplicate code that also must carry the same changes.