Java Classes and Objects Flashcards

1
Q

Restricting Generics to a super class of a particular class

A

In declaration of the class, we specified a constraint “T super Number”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Subclass of Generics

A

class ColorPrinter<T> extends Printer<T> or Class ColorPrinter extends Printer<String> in case 2 you need to extend a specific type of printer rather than generic type</String></T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Restricting Generics to Subclass

A

MyListRestricted<T> - We can use the class MyListRestricted with any class extending (any sub class of) Number - Float, Integer, Double etc.</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Type Erasure

A

Type erasure means that generic type information is not available to the JVM at runtime, only compile time. The reasoning behind major implementation choice is simple – preserving backward compatibility with older versions of Java. When a generic code is compiled into bytecode, it will be as if the generic type never existed. This means that the compilation will: Replace generic types with objects Replace bounded types (More on these in a later question) with the first bound class Insert the equivalent of casts when retrieving generic objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Child Constructor Rules

A

Order of Initialization: Always call the parent constructor before initializing the child class’s own members. Constructor Visibility: Ensure the parent constructor is accessible from the child class. If the parent constructor is not public or protected, it might not be accessible. No Implicit Super Call: If the parent class does not have a default constructor (no args constructor), you must explicitly call one of its constructors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Generic Method

A

static <X> X doSomething(X number){ X result = number; //do something with result return result; } Integer i = 5; Integer k = doSomething(i);</X>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Constructor Rules

A

A constructor must have the same name as the class in which it is defined. It does not have a return type, not even void. If no constructor is explicitly defined, the Java compiler provides a default constructor with no arguments. A class can have multiple constructors with different parameter lists (constructor overloading). A constructor can call another constructor in the same class using this(). This must be the first statement in the constructor A constructor can call a constructor from the superclass using super(). This must also be the first statement in the constructor. If a constructor does not explicitly call a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass (super()). If the superclass does not have a no-argument constructor, you must explicitly call a superclass constructor with arguments using super(arguments).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Nested Classes

A

There are 4 types of Nested classes (LISA)
Static Nested Classes: Defined with the static keyword.
Inner Classes: Non-static nested classes.
Local Classes: Defined within a method Anonymous Classes: Classes without a name, defined and instantiated in a single statement.

public class OuterClass {
private int outerValue = 10;
public class InnerClass {
public void display()
{
System.out.println(“Outer value: “ + outerValue);
}
}
public static class StaticNestedClass
{

public void display() {

System.out.println(“Static nested class”);
}
}
public void methodWithLocalClass() {
class LocalClass { public void display() {
System.out.println(“Local class inside method”);
}
}
LocalClass local = new LocalClass(); local.display();
}
public void methodWithAnonymousClass() { Runnable r = new Runnable() { @Override public void run() { System.out.println(“Anonymous class”); } }; new Thread(r).start(); } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Multiple Classes in same file

A

Yes, multiple Java classes can be defined in the same file, but there are some rules and best practices to follow: Only One Public Class: If you have a public class, there can be only one public class in the file, and the file name must match the name of the public class. Additional Classes: Any additional classes in the same file must not be public. They can be package-private (default), protected, or private. While it’s technically possible to include multiple classes in one file, it’s generally considered good practice to have each class in its own file. This makes the code easier to find, read, and maintain. public class Main { public static void main(String[] args) { Helper helper = new Helper(); System.out.println(helper.getMessage()); } } class Helper { String getMessage() { return “Hello, World!”; } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly