L10 - Interface and Inner Classes Flashcards

1
Q

What is an interface in Java?

A

An interface is a type that specifies method headings that any implementing class must define. It contains no instance variables or complete method implementations.

Example:

interface Animal {
void makeSound(); // Abstract method (no implementation)
}

class Dog implements Animal {
public void makeSound() { // Must implement the method
System.out.println(“Bark!”);
}
}

public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound(); // Output: Bark!
}
}

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

Multiple Interfaces Example Code

A

interface Flyable {
void fly();
}

interface Swimmable {
void swim();
}

class Duck implements Flyable, Swimmable {
public void fly() {
System.out.println(“Duck is flying.”);
}

public void swim() {
    System.out.println("Duck is swimming.");
} }

public class Main {
public static void main(String[] args) {
Duck d = new Duck();
d.fly(); // Output: Duck is flying.
d.swim(); // Output: Duck is swimming.
}
}

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

How do you define an interface in Java?

A

public interface MyInterface {
void myMethod(); // Abstract method
}

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

What are the key characteristics of interfaces?

A

-All methods are public and abstract

  • All variables are public, static, and final
  • Interfaces cannot contain instance variables
  • A class can implement multiple interfaces
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does a class implement an interface?

A

Use ‘implements’ and define all methods. Example:

public class MyClass implements MyInterface {
public void myMethod() {
System.out.println(“Implemented method”);
}
}

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

Can a class implement multiple interfaces?

A

Yes, use commas to separate them.
Example:

public class MyClass implements Interface1, Interface2 { }

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

What is the difference between an interface and abstract class (Methods)

A

Interface - Only abstract methods

Abstract class - Can have abstract and concrete methods

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

What is the difference between an interface and abstract class? (Variables)

A

Interface - public, static, final

Abstract class - Can have instance variables

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

What’s the difference between an interface and an abstract class? (constructor)

A

Interface - No constructors
Abstract class - Can have constructors

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

What’s the difference between an interface and an abstract class? (Inheritance)

A

Interface - multiple inheritance allowed

Abstract class - Single inheritance only

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

Can abstract class implement an interface?

A

Yes. The abstract class does not need to implement all methods, but a concrete subclass must.

Example:

public abstract class AbstractClass implements MyInterface { }

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

Can interfaces extend other interfaces?

A

Yes, use ‘extends’
Example:

public interface AdvancedInterface extends BasicInterface { }

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

What is the purpose of the ‘Comparable’ interface?

A

Allows objects to be compared for sorting.
Example:
public interface Comparable<T> {
int compareTo(T other);
}</T>

  • Used in sorting algorithms like ‘Arrays.sort()’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How should ‘compareTo()’ behave?

A
  • Returns negative if this < other.
  • Returns zero if this == other.
  • Returns positive if this > other.

Example:
public class Car implements Comparable<Car> {
public int compareTo(Car other) {
return this.speed - other.speed;
}
}</Car>

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

What is the ‘Serializable’ interface?

A

A marker interface with no methods, used to indicate that a class can be serialized.

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

What is the ‘Cloneable’ interface used for?

A

Allows objects to be cloned using ‘Object.clone()’

Example:

public class Person implements Cloneable {
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

17
Q

Can interfaces have constants?

A

Yes, all interface variables are public, static, and final by default

18
Q

What is an inner class in Java?

A

A class defined within another class.

19
Q

Why use an inner class?

A
  • Helps group related classes together
  • Can access private members of the outer class.
  • Used in GUI programming (event handling)
20
Q

How do you define an inner class?

A

public class Outer {
class Inner {
void display() { System.out.println(“Inner class”); }
}
}

21
Q

How do you create an instance of an inner class?

A

public class Outer {
class Inner {
void display() { System.out.println(“Inner class”); }
}
}

Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();

22
Q

Can an inner class access private members of the outer class?

A

Yes.

public class Outer {
private int value = 10;
class Inner {
void show() { System.out.println(value); } // Accesses private variable
}
}

23
Q

What is a static inner class?

A

A nested class that does not require an instance of the outer class.

Example:
public class Outer {
static class StaticInner {
void show() { System.out.println(“Static inner class”); }
}
}

Accessed as:
Outer.StaticInner obj = new Outer.StaticInner();

24
Q

Can inner classes be public?

A

Yes, but must be instantiated using the outer class.

OuterClass.InnerClass obj = new OuterClass().new InnerClass();

25
What is an anonymous inner class?
A one-time-use class with no name, usually used in event handling. Example: Button b = new Button("Click me"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Button clicked"); } });
26
Can inner classes be nested?
Yes, inner classes can be inside other inner classes Example: class A { class B { class C { } } }
27
What happens when a class inherits from a class with inner classes?
The inner class is also inherited but cannot be overidden.
28