midterm_package Flashcards
What is the difference between a class and an object?
Object is a class, and all other classes are subclasses of the Object class (implicitly inherit). So we can say that any instance of a class IS AN object.
What does “Java is a strongly typed language” mean?
Java is a strongly typed language because every ‘thing’ has a type, and certain operations can only be performed for certain types.
What are the four visibility modifiers used in Java, and which of the four access levels(class, package, subclass, everyone) are permitted by each modifier?
private
: can be accessed within the class only. default
: can be accessed by other classes in the same packages. can’t be accessed by subclasses in different packagesprotected
: can be accessed by other classes in the same package, and subclasses in different packagespublic
: can be accessed anywhere
How must we use visibility modifiers to hide information and enforce encapsulation?
To hide information and enforce encapsulation, we use ‘private’ for members we want to hide from other classes, and provide public getter and setter methods to access them. we use ‘protected’ when we want to share it with subclasses and within the same package. Default(no modifier) access is limited to the same package.
Data conversion in Java can be wodening or narrowing. What does that mean?
Widening occurs when data is converted from a smaller to a larger data type, and it happens automatically.
Narrowing occurs when data is converted from a larger to a smaller data type. This requires explicit casting by a programmer since data loss could occur.
Java makes a distinction between identity and equality. what does this mean?
Identity referes to the uniqueness of an object instance at a memory location. Two references have the same identity if they point to the same memory location, meaning they references to the same object ==
operator is used to check identity.
Equality is a logical condition that checks if two objects have the same value or state but can reside at different memory location. .equals()
methods is used to check equality.
What is pass by value in Java?
- if the argument is a primitive, a copy of the value is passed to the method.
- if the argument is an object, a copy of the reference to the object is passed.
As a result, the original value or the object that the reference points to is not affected by what happens within the method.
What is the difference between overriding and overloading?
Overriding is when a subclass redefines a method from its superclass with the same signature to provide a specific implementation. It enables runtime polymorphism, allowing Java to determine which method to invoke based on the object type at runtime.
Overloading involves defining multiple methods with the same name but different parameter lists (different types or numbers of parameters) within the same class. It enables compile-time(static) polymorphism, allowing multiple methods to do similar tasks with different types of inputs.
what are four ways we can use the final
keyword?
- When applied to a variable, final makes the variable unchangeable after it has been assigned
- When applied to a method, final prevents from being overridden in subclasses
- When applied to a class, final makes the class cannot have subclasses
- When applied to a parameter of a method, final prevents from being modified within the method.
Programmers like to make a distinction between an API’s ”interface” and an API’s “implementation”. What do they mean?
An API’s “interface” refers to the exposed methods and properties that define how to use it, while “implementation” is the underlying code that actually performs the tasks, hidden from the user.
What is the difference between an abstract class and an interface? when should you use an abstract class, and when should you use an interface?
You “extends” an abstract class while you “implements” an interface. Any variables in an interface are public, static, and final. Methods in an interface can only be private or public.
Use an abstract class to define behaviours and properties that are closely related to other classes. Use an interface when you don’t care who implements it.
what is a static type?
Static type refers to the declared type of a variable. It is determined at compile time based on the variable’s declaration. The static type dictates what methods and properties are accessible through the variable in your code.
What is a dynamic type?
Dynamic type refers to the actual type of the object that a variable points to at runtime, which can be an instance of the declared type or any of its subclasses. The dynamic type can differ from the static type if the variable points to an instance of a subclass.
What is polymorphic method dispatch?
When a class’s method is overriden by some subclass, the subclass’s version is invoked instead. Human bob = new Baby(), the dynamic type (Baby) determines which version of the method to invoke during runtime, given Baby has overriden a method in the Human class
What is the difference between encapsulation and information hiding?
Encapsulation is grouping related data together in one place. Data is only accessible through setter and getter.
Information hiding is keeping data as private as possible by using visibility modifiers(e.g. private)