<p>damn, APCS exam is tomorrow… getting pretty nervous/excited; good luck to whoever else is taking it!</p>
<p>hugeeee wall of text btw:</p>
<p>@Matthew5
assuming i’m not wrong, I’ll tell you what I know.
Here are a few things you should already know though.</p>
<p>(just adding onto what @cagg333 stated)</p>
<ul>
<li>Abstract classes or interfaces can NOT be instantiated. This doesn’t mean that it the object can’t be a type of the abstract class.
EX:
// assume there are some methods/variables inside, too lazy to implement them.
public abstract class AbstractClass{
}</li>
</ul>
<p>public class SubClassOfAbstractClass extends AbstractClass{
}
AbstractClass obj = new AbstractClass(); // illegal, you CANNOT create an instance of an
abstract class.
AbstractClass obj = new SubClassOfAbstractClass(); // works fine. (of course, if you wish to access certain methods within SubClassOfAbstractClass, it will be required to downcast the type back to SubClassOfAbstractClass)</p>
<ul>
<li>Abstract classes can provide implementations( or definitions) of its methods unlike interfaces.
EX:
// as you can see, this abstract class has both 1) a method that requires its subclasses to define, and 2) 2 methods that are already defined in the class.</li>
</ul>
<p>public abstract class ClassOne{
public void someMethod(){
System.out.println(“Wheee”);
}
public int OnePlusOne(){
return 1+1;
}
public abstract void someMethod2(); // requires implementation in its subclass. Abstract
// methods can be defined within the class where it
// has been declared abstract.
}</p>
<p>public interface someInterface{
public void someMethod(){ // wrong. an interface CANNOT have implementations within
System.out.println(“Wheee”); // its self.</p>
<p>public void someMethod2(); // both of these are perfectly acceptable. By implementing
public void someMethod3(); // interfaces, it is required that the methods from the interface
// will be defined within the ‘subclass’. (class that implements
// the interface)
}
}</p>
<ul>
<li>Abstract classes can contain instance variables, while interfaces cannot.
ex:
An abstract class can contain any variables, while interfaces can’t.
private String someString; // allowed in abstract class, not in interface.
private int iSomeInt; // allowed in abstract, not in interface.
private boolean bSomeBool // read above statement
private double dSomeDouble // read above statements</li>
</ul>
<p>There are times of when you should contemplate when using an interface is more appropriate than using an abstract class. Here are some things to keep in mind when deciding which to use: (Definitions via Barron’s book)
- use abstract class for an object that is application-specific, but incomplete without its subclasses.
- Consider using an interface when its methods are suitable for your program but could be equally applicable in a variety of programs.</p>
<p>if you need any clarifications, just leave a message.</p>