Java Questions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What are bounded type parameters?

If you want a method or class to limit the kinds of parameters that can be used, you can use <T extends Class>. This also lets you invoke the methods defined by the bounds (of the class Class).

What is type erasure?

In Java, generics are basically just syntactic sugar. When code is compiled and fed to the JVM, parameterized types are removed and stuff is cast and shit.

What does the "final" keyword mean for variables?

Variable cannot be changed once initialized.

How can functions use generic types?

Appears before function's return type. Not really sure why it's needed. Type inference usually precludes the need to spell out method generic types.

What is Object Reflection?

??? I don't understand ???

What is a generic type?

A generic class or interface that is parameterized over types.

What does the "final" keyword mean for classes?

Class cannot be subclassed.

What are generics?

They are a way to make more bugs detectable at compile time, rather than run time. https://docs.oracle.com/javase/tutorial/java/generics/why.html

What is encapsulation?

Wrapping data for a class in the same unit as the methods acting on the data. Getters and setters are examples of encapsulation and offer a way to provide read/write-ony access to data. This also means users don't need to know how exactly the object stores its data.

What is static, protected, etc.?

Static methods belong to the class itself, rather than instances of the class. Thus, they don't operate on instance variables and can't call instance methods. They cannot use the "this" keyword as there is no instance.

What does the "finalize" method do?

The finalize method is called by the automatic garbage collector right before an object is destroyed. This Object method can be overridden by a class to define custom behavior during garbage collection.

What does "abstract" mean in general?

Abstract classes are like interfaces. They cannot be instantiated and can implement some methods and leave others to be filled in.

"Abstract" vs "Interfaces"?

Abstract classes can have non-constant fields (not static and final). They can also define public, private, and protected methods. Interfaces can only have constant fields (final static variables). They are all automatically public. Also, all methods declared or defined as default methods are public. You can only extend one abstract class, whereas you can implement any number of interfaces. Also, an abstract class can actually implement an Interface without filling out all of the Interface's methods.

In what situations would you use interfaces or abstract classes?

Abstract classes: share code among closely related classes. Interfaces: unrelated classes will implement interface. For example, Comparable and Cloneable are implemented by many unrelated classes.

What does the "abstract" keyword mean for classes?

An abstract class cannot be instantiated — it can only be sub classed (extended). If a class has any abstract methods, it must be declared abstract. Not all methods of an abstract class are necessarily abstract, however.

What does the "abstract" keyword mean for methods?

An abstract method is declared without implementation.

What does the "finally" keyword do?

Executes a block of code after the try/catch statements.

Are private methods of a super class inherited by its subclasses?

No. However, public or protected methods of the super class that are inherited can be used the sub class to access private fields of the super class.

What does "extends" do?

It basically says that a class is inheriting from a super class.

What is a raw type?

It is an instantiation of a generic class without a generic type specified. However, it is not a non-generic class. Should not use raw types.

What is an "Interface"?

It's basically a way to create a sort of standard set of rules for everything that follows that Interface. So, specifically, in Java, an Interface defines constants, method signatures, default methods, static methods, and nested types. Interfaces can only be implemented by classes or extended by other Interfaces. When a class implements an Interface, it defines the method body for all the method signatures written in the Interface. This is also a way to provide APIs for people who want to use a standard. Methods in an interface are implicitly abstract, if they are not default or static.

How do bounded type parameters help implement generic algorithms?

Look at this https://docs.oracle.com/javase/tutorial/java/generics/boundedTypeParams.html

What does the protected keyword do?

Means that a method/field can only be accessed within a package or by a subclass of the class in another package.

What are the "members" of a class and how are they involved in inheritance?

Members of a class are its fields, methods, and nested classes.

What does the "final" keyword mean for methods?

Method cannot be overridden by a subclass.

What is overloading? What is overriding?

Overloading is when you define more than one method of the same name, but give them a different number or type of arguments. Overriding is when a method you write for a class has the same method signature (same name and parameter list) as that of a super class.

Explain how polymorphism works.

Polymorphism is a way of creating subclasses of a class (called the super class) that share some of the same methods and variables as the super class, but also define some of their own unique behaviors. (link - https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html). This is actually closely related to inheritance. But polymorphism is primarily about how the JVM decides which method to actually use - it calls the method related to the sub class, and not the super class, for example. For example, if you initiative a Bike b = new MountainBike(), then call b.printDescription(), it will call the printDescription() method defined in the MountainBike sub class, rather than in the Bike class.

What is the Object class?

The only class in Java with no super class. All classes thereafter inherit methods and fields from the Object class.


Kaugnay na mga set ng pag-aaral

Vocabulario de el Principe y el Mendigo.

View Set

Chapter 19 Industrial Revolution

View Set

Chapter 19- Vibrations and Waves

View Set