L10 - Interface and Inner Classes Flashcards
What is an interface in Java?
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!
}
}
Multiple Interfaces Example Code
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 do you define an interface in Java?
public interface MyInterface {
void myMethod(); // Abstract method
}
What are the key characteristics of interfaces?
-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 does a class implement an interface?
Use ‘implements’ and define all methods. Example:
public class MyClass implements MyInterface {
public void myMethod() {
System.out.println(“Implemented method”);
}
}
Can a class implement multiple interfaces?
Yes, use commas to separate them.
Example:
public class MyClass implements Interface1, Interface2 { }
What is the difference between an interface and abstract class (Methods)
Interface - Only abstract methods
Abstract class - Can have abstract and concrete methods
What is the difference between an interface and abstract class? (Variables)
Interface - public, static, final
Abstract class - Can have instance variables
What’s the difference between an interface and an abstract class? (constructor)
Interface - No constructors
Abstract class - Can have constructors
What’s the difference between an interface and an abstract class? (Inheritance)
Interface - multiple inheritance allowed
Abstract class - Single inheritance only
Can abstract class implement an interface?
Yes. The abstract class does not need to implement all methods, but a concrete subclass must.
Example:
public abstract class AbstractClass implements MyInterface { }
Can interfaces extend other interfaces?
Yes, use ‘extends’
Example:
public interface AdvancedInterface extends BasicInterface { }
What is the purpose of the ‘Comparable’ interface?
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 should ‘compareTo()’ behave?
- 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>
What is the ‘Serializable’ interface?
A marker interface with no methods, used to indicate that a class can be serialized.
What is the ‘Cloneable’ interface used for?
Allows objects to be cloned using ‘Object.clone()’
Example:
public class Person implements Cloneable {
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Can interfaces have constants?
Yes, all interface variables are public, static, and final by default
What is an inner class in Java?
A class defined within another class.
Why use an inner class?
- Helps group related classes together
- Can access private members of the outer class.
- Used in GUI programming (event handling)
How do you define an inner class?
public class Outer {
class Inner {
void display() { System.out.println(“Inner class”); }
}
}
How do you create an instance of an inner class?
public class Outer {
class Inner {
void display() { System.out.println(“Inner class”); }
}
}
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
Can an inner class access private members of the outer class?
Yes.
public class Outer {
private int value = 10;
class Inner {
void show() { System.out.println(value); } // Accesses private variable
}
}
What is a static inner class?
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();
Can inner classes be public?
Yes, but must be instantiated using the outer class.
OuterClass.InnerClass obj = new OuterClass().new InnerClass();