0% found this document useful (0 votes)
2 views6 pages

Abstract Classes and Interfaces

The document explains the concepts of abstract classes and interfaces in Java, highlighting that abstract classes cannot be instantiated and must have constructors, while interfaces only contain constants and abstract methods. It details the requirements for subclasses to implement abstract methods and the rules for class inheritance and interface implementation. Additionally, it covers the relationship between classes and interfaces, including method signatures and casting between them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Abstract Classes and Interfaces

The document explains the concepts of abstract classes and interfaces in Java, highlighting that abstract classes cannot be instantiated and must have constructors, while interfaces only contain constants and abstract methods. It details the requirements for subclasses to implement abstract methods and the rules for class inheritance and interface implementation. Additionally, it covers the relationship between classes and interfaces, including method signatures and casting between them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Abstract Classes

●​ No creating instances of abstract classes with new but they still have
constructors (even with parameters)
○​ Note in java if a superclass ONLY has a constructor that takes arguments
(meaning no default constructor is made), the subclass must call
Super(arguments) in the subclass constructor (even if the subclass
constructor takes no arguments). But if both a default constructor and a
constructor that takes arguments OR there is no constructor declared (in
which case Java makes a default constructor) then there is no need to
explicitly type Super(...) since Java will automatically do it.

●​ Syntax of of abstract methods:

public abstract class Shape {


...
}
public class Circle extends Shape {
...
}

●​ If a subclass of an abstract superclass does not implement all abstract methods it


must be declared as abstract
●​ A non-abstract class cant have abstract methods
○​ An Abstract class can have no abstract methods
●​ An instance of an abstract class is not possible but you can use it as a data type:

Shape[ ] circles = new Shape[10];


circles[0] = new Circle(5);

●​ Not that ONLY abstract methods can have no bodies. Any other method must
have a body.
●​ Abstract methods are not static
●​ A subclass method can override a superclass method and make it abstract
●​ You cannot cast siblings classes
●​ This throws an error because Number does not implement
Comparable<Number>”:

Number x = new Integer(10);


system.out.println(x.compareTo(new Integer(5)));

●​ An abstract class can implement an interface

Interfaces

●​ An interface only has constants and abstract methods


●​ No making instances but for an interface A you can declare:

A x; //It must be assigned to an instance of a class that extends A

public interface Edible {


}

class chicken extends Animal implements Edible {


}

●​ When a class implements an interface it must implement all the methods


declared in the interface with the SAME signatures and return types
○​ Also interface methods are public by default thus when implementing them
in subclasses they MUST be set to public
●​ If the class implementing an interface does not implement all its methods it must
be declared abstract
●​ Let n be an Integer object. The following are true:
1.​ n instanceof Integer
2.​ n instanceof Object
3.​ n instanceof Comparable
//Note Comparable is an interface implemented by Integer
●​ A class that implements Comparable expects its compareTo to take an argument
the same as the class’ type. This would be wrong:
public int compareTo (Object o) { //should be String o
}
●​

●​ A Java class can extend only one superclass but can implement multiple
interfaces

public class dog extends Animal implements notEdible, animalToys, furry {


​ ….
}

●​ An interface can extend (NOT implement) multiple interface

public interface newInterface extends Inter1, inter2, inter3 {


​ ​ ​ ...
}

●​ Note an interface extending another interface simply inherits the interfaces


methods, no implementing them.
●​
●​ Unlike classes which share a root, the Object class, interfaces dont have this.
●​ A variable can be of an interface type and refer to an instance of any class that
implements it.

Interface Animal {
void makeSound();
}

public class Dog implements Animal {

​ public void makeSound() {


​ ​ system.out.println(“Woof”);
}

…..main {

​ Animal o = new Dog();


​ o.makeSound();

●​ You can cast interface objects and classes that implement it the same way as
other classes:

Animal a = new Dog();


Dog d = (Dog)a;

You might also like