B. General Questions about Java Flashcards
What are wrapper classes?
A wrapper class converts java primitives into objects. So a primitive wrapper class is a wrapper class that encapsulates, hides, or wraps data types from the eight primitive data types so that these can be used to create instantiated objects with methods in another class or in other classes. The primitive wrapper classes are found in the Java API.
What is casting? and the types of casting with details?
Type casting is when you assign a value of one primitive data type to another type.
Widening Casting (automatically) - converting a smaller type to a larger type size byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) - converting a larger type to a smaller size type double -> float -> long -> int -> char -> short -> byte
Can you use a primitive type when using Arraylist? Why or why not?
No. Because the list can only store objects and primitive types are not objects. Unless we cast them using the wrapper class.
What is the difference between valueOf() and parseFloat()?
Float.parseFloat(String) converts the string to primitive float.
valueOf(String) - returns the Float object initialized with the value provided.
Does Java support multiple inheritance?
No, Java does not support multiple inheritance. Each class can extend only one class but is able to implement more than one interfaces.
Do you use “implement” or “extend” from one class to another?
Extend
If the parentclass has a protected variable, can the subclass access it? Why or why not?
Yes. Because subclasses can acess public and protected variables, but not private.
Why and when would you use inheritance?
It is useful for code reusability. To reuse attributes and methods of an existing class when creating a new class.
What is inheritance?
Inheritance can be defined as the process where one class (called the subclass or child class) acquires the properties (methods and fields) of another class (called the superclass or parent class).
Inheritance provides reusability of code and can be used to add additional features to an existing class, without modifying it.
What does the word Polymorphism mean?
Polymorphism means “many forms”.
What is Polymorphism?
Polymorphism is the ability of programming languages to present the same inferface for different underlying data types. A plymorphic type is a type whose operation can also be applied to values of some other type.
In other words, when using inheritance, we inherit attributes and methods from the superclass/parent class. Polymorphism uses those attribues and/or methods to perform different tasks. This allows the developer to perform a single action in different ways.
What are the types of Polymorphism?
There are two types of Polymorphism in Java:
• Compile-time polymorphism (Static binding) – Method overloading
• Runtime polymorphism (Dynamic binding) – Method overriding
We can perform polymorphism by Method Overloading and Method Overriding.
What is the keyword used to allow one class to inherit the properties of another class?
extends
Are constructors members of a class?
No. Constructors are not members, so they are not inherited by subclasses. But the subclass automatically acquires the default constructor of the superclass.
What keyword do you use to call a parameterized constructor of the superclass?
super(values);
What word do you use when a class inherits the properties of an interface?
Implements
Can interfaces be extended by a class?
No.
public interface Animal {
}
public class Mammal implements Animal { }
public class Dog extends Mammal { }
In this example, what keyword can you use to check wheter Mammal is actually an Animal, and dog is actually an Animal?
instancef
public static void main(String args[]) { Mammal m = new Mammal(); Dog d = new Dog();
System.out.println(m instanceof Animal); System.out.println(d instanceof Mammal); System.out.println(d instanceof Animal); }
Output:
true
true
true
HAS-A relationship
These relationships are mainly based on the _____.
Usage. This determines whether a certain class HAS-A certain thing
Name all the members that a subclass inherits from its superclass?
Fields, methods and nested classes.
Extra info: Constructors are not members, but can be invoked from the subclass.
In Java, an Is-A relationship depends on _____
Inheritance.
In Java, a Has-A relationship is also known as ______
Composition
Can a class extend more than one class? Why or why not?
No.
However, a class can implement one or more interfaces.
How would you define polymorphisim in simple words?
We can define polymorphism as the ability of a message to be displayed in more than one form.