LAB 5 Flashcards
(39 cards)
is an important feature of object-oriented programming in Java. It allows for one class (child class) to inherit the fields and methods of another class (parent class). For instance, we might want a child class Dog to inherent traits from a more general parent class Animal.
Inheritance
In simple Java programs, you may work with just one class and one file. However, as your programs become more complex you will work with multiple classes, each of which requires its own file. Only one of these files in the Java package requires a ______ method, and this is the file that will be run in the package.
main()
For example, say we have two files in our Java package for two different classes:
Shape, the is the ______ class
Square, the ______ class.
parent class.
child class.
In Java, a child class inherits its parent’s fields and methods, meaning it also inherits the parent’s constructor. Sometimes we may want to modify the constructor, in which case we can use the _____ method, which acts like the parent constructor inside the child class constructor.
super() method
keeps a parent class member accessible to its child classes, to files within its own package, and by subclasses of this class in another package.
protected
Adding ____ before a parent class method’s access modifier makes it so that any child classes cannot modify that method - it is immutable.
final
Java incorporates the object-oriented programming principle of ____
_______ allows a child class to share the information and behavior of its parent class while also incorporating its own functionality. This allows for the benefits of simplified syntax and reduced cognitive overload for developers.
polymorphism.
In Java, we can easily ______ parent class methods in a child class.
______ a method is useful when we want our child class method to have the same name as a parent class method but behave a bit differently.
override / overriding
In order to override a parent class method in a child class, we need to make sure that the child class method has the following in common with its parent class method:
-Method name
-Return type
-Number and type of parameters
In Java, polymorphism allows us to put instances of different classes that share a parent class together in an __________
For example, if we have an Animal parent class with child classes Cat, Dog, and Pig we can set up an ____ with instances of each animal and then iterate through the list of animals to perform the same action on each.
array or ArrayList.
is when a child class inherits from a parent class.
Single Inheritance
is when a child class inherits from a parent class which in turn inherits from another parent class.
Multilevel Inheritance
is when a child class inherits from a parent class and there is another sibling class that also inherits from the same parent class.
Hierarchical Inheritance
are reusable pieces of code in classes.
Methods
The difference between a method and a function is that
methods are always related to a class or an object
Methods consist of at least the following elements:
Return type: The type of the value that is returned from the method
Name: The name of the method
The ____ of a method should describe as much as possible what the method is doing.
Name
is described by a data type and a name.
can be used to access the value inside the method body. A method without any ______ must have empty parenthesis () after the method name.
Parameters
can change how a method is allowed to be called (public, protected, private, package private), if the method is working on object state or should be executable without creating an object out of a class (static), or if the method is allowed to be replaced by inherited classes (final).
Modifier
(4) Visibility Modifiers
public
private
package private
protected
Methods declared as ____ can be called from everywhere, inside and outside of the object or the class.
public
Methods declared as _____ can only be called from inside the object or the class.
private
Methods declared as ______ can only be called from classes within the same package.
package private
Methods declared as _______ can only be called from inside the class or from inside classes inherited from that class.
protected