Java - Inheritance

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

what is the difference between method overloading and method overriding?

1) overloading is when two methods have the same name but different parameters - this is even the case between child and parent classes 2) overriding is when the method has the same name and same parameters but different implementation details - happens when the subclass changes the implementation of an inherited method

what are the modifiers that can be used on methods and what do they do?

1) private, only the defining class can access it 2) static, it exists only in the class where it is defined and is not inherited 3) final, it can't be reassigned or overwritten

what kinds of methods are statically bound (bond at compile time)?

1) private, static, or final methods (because they cannot be overridden in the subclass) 2) overloaded methods 3) a regular method that is not overridden

what are the modifiers that can be used on a class and what do they do?

1) public, all other classes can access it 2) final, instances can access it but you can't make a subclass of it 3) protected, not really sure and doesn't matter for the moment, just know it exists

what is the java reflection API?

API to manipulate classes and everything in a class

what are two names for the class that is inheriting from the other class?

child class subclass

true or false: you cannot assign a subclass object to a superclass variable

false if Employee is the superclass and Manager is the subclass, then every manager is an Employee the following works: Employee e; e = new Employee(...); e = new Manager(...);

true or false: if a subclass of an abstract class does not define all of the inherited abstract methods, but just some of them, you can leave off the abstract keyword from the class declaration?

false, a class must be declared as abstract until all of its methods, inherited or otherwise, are defined

what are the two main ways of declaring an ArrayList?

import java.util.ArrayList; ArrayList<ClassName> = new ArrayList<>(); var staff = new ArrayList<Employee>();

what is an abstract class and what is its point?

it's a class where certain or all methods are declared but not defined Uses: 1) organization so that you can list out all of the (abstract) methods that other inheriting classes must have, but it's up to them to determine the implementation 2) encountered inside interfaces (don't worry about interfaces for the moment but just know the its relation to abstract classes)

what is the source of polymorphism in java?

method overloading and overriding

what are two names for the class that the other class is inheriting from?

parent class superclass

*NOTE* step by step of a method call, taking into account both static and dynamically bound methods

pg. 172

what is the "type" of an object?

the class it is an instance of

how could you prevent the ability to create a subclass from a superclass?

by adding the final modifier to the superclass public final class Executive extends Manager {...}

what method would you use to see the number of elements in an ArrayList?

size() staff.size()

what happens if you don't call the superclass constructor in the subclass?

the no-argument constructor of the superclass is invoked, if one doesn't exist then the java compiler reports an error

when using an array list you can't add primitive types, so what must you do if you want to add something like integers to your array list?

you must use the class counterparts of the primitive type that you want to add. These are called wrapper classes (Integer, Long, Float, Double, Short, Byte, Character, Boolean) Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects ex: var list = new ArrayList<Integer>();

what is the ultimate ancestor object in Java? (similar to global or window in js)

Object every class implicitly extends this class in the background

in the following... Manager boss = new Manager(...); Employee [ ] staff = new Employee[3]; staff[0] = boss; ...what is the difference between boss and staff[0]?

They both refer to the same object, but staff[0] is considered to be only an Employee object by the compiler because it's an element of an Employee array

what is static (method) binding?

a method binding that can be resolved at compile time happens when method is static, final, or private, because these methods cannot be overridden and therefore overriding can't occur eliminating the need to search for the appropriate method overloaded methods are also statically bound

NOTE on efficiency

add and removing from the middle of an array list is not very efficient and if you're going to need to do it often consider using a linked list instead

in Java which of the following types are objects?: primitive types array types

array types, they extend the Object class

when defining a subclass by extending its superclass, you only need to indicate the ___ between the two classes?

differences

if you have a good idea of how many elements you'll be storing in the ArrayList, but still want to take advantage of the dynamic nature of the ArrayList because there might be more or less elements than predicted, what method should you use?

ensureCapacity() staff.ensureCapacity(100) this initializes the ArrayList CAPACITY (not size) to 100 but can still grow or shrink as needed *NOTE* at the beginning, even with an initial allocation, an ArrayList holds no elements at all, unlike an array that is initialized with x number of empty slots

what keyword is needed for a class to inherit from another class?

extends

what two methods do you use in order to access and modify elements of an ArrayList?

get() and set() Employee e = staff.get(i); staff.set(i, bindle); *NOTE* the set method modifies elements that are already in the ArrayList so don't use to try and add elements

what happens if a class is declared without the public, private, or protected keywords?

it defaults to "package protected" meaning that all other classes in the same package can access it's publicly accessible methods and properties

what is an ArrayList?

it's a class that acts as a resizable array, unlike the standard arrays in Java that have a constant size the size of the ArrayList changes dynamically both when adding or removing elements from the book: ArrayList is a GENERIC CLASS with a type parameter

*NOTE* there are a number of methods in the Object superclass that all classes and reference types inherit from (classes, arrays, etc.)

methods include: clone equals finalize getClass hashCode toString concurrency related methods: notify notifyAll wait -> there are three overloaded forms of this method check out the docs about these methods if needed: https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html

can you create an instance of an abstract class? (don't confuse instance with a subclass)

no

if staff is an Employee array, would the following work: Manager m = staff[i];

no, a subclass object can be assigned to a superclass variable but the reverse does not work not all employees are managers but all managers are employees

can you have multiple public classes per java file?

no, only one

is it possible to directly access private variables in the superclass from inside of a subclass?

no. you have to instead use accessor methods in the superclass

what kinds of methods are dynamically bound?

overridden methods

what is dynamic (method) binding?

overridden methods in a subclass are dynamically bound at runtime because, the specific method that ends up being executed depends on the arguments that are passed into the method

when writing the constructor for the subclass, what is the syntax for inheriting the properties from the superclass?

something like the following... super(name, salary, year, month, day); this is calling the constructor of the superclass so that it can initialize the private fields of the super class so that they are then present in the subclass

what keyword allows you to access public methods and variables in the parent class?

super example (in subclass): double baseSalary = super.getSalary()

what must be the first statement in the constructor for the subclass?

the call to the superclass

what is binding?

the linking of a method call to the place where the method is defined - resolving which method to actually execute for a particular method call

you only want what kinds of information in the superclass?

the most general so that you can be more fine grained in the subclasses

if you are satisfied with the current number of elements in the ArrayList, what method should you call to fix the size to the current size?

trimToSize() staff.trimToSize() the memory block is adjusted to use exactly as much storage space as is required to hold the current number of elements, and the garbage collector will reclaim any excess memory NOTE you can still add elements after using this method but you shouldn't because it defeats the purpose

how would you add an element somewhere in the middle of an array list?

use the add() method but also provide an index staff.add(i, e);

how do you define a method that can receive a variable number of parameters

when specifying the parameters include the type followed by ... followed by the name of the paramter: public static double max(double... values) {...}

how would you remove an element from the middle of an array list?

with the remove() method that takes an index as an argument staff.remove(i)

how would you convert an array list to an array?

with the toArray() method useful if you want to dynamically create an array of an unknown size that, after being created will not change in size and can therefore benefit from being turned into a standard array


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

Chapter 2- Life Insurance Basics

View Set

Social Media Marketing Exam 2 (Ch 7, 8)

View Set

NUR 168: CONCEPTS 3: CHAPTER 10: LEADING, MANAGING, DELEGATING:

View Set

Personal Finance Chapter 1 and 2 TEST

View Set

SHRM 2022 Managing a Global Workforce

View Set

Adult 1 - Oncology - Unit 2 - Ch. 12

View Set