0% found this document useful (0 votes)
32 views14 pages

Classes That Can Be Instantiated: Ghoul Class

The document discusses abstract classes in Java. It states that abstract classes can define abstract methods that subclasses must implement, but abstract classes cannot be instantiated directly and must be extended by subclasses. It also compares abstract classes to interfaces, noting that abstract classes can contain fields and implemented methods while interfaces contain only public static final fields and public abstract methods. The document provides an example of an abstract class AbstractMap that is extended by HashMap and TreeMap subclasses. It also discusses nested classes in Java and the differences between inner and static nested classes.

Uploaded by

Shrikant Sharma
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)
32 views14 pages

Classes That Can Be Instantiated: Ghoul Class

The document discusses abstract classes in Java. It states that abstract classes can define abstract methods that subclasses must implement, but abstract classes cannot be instantiated directly and must be extended by subclasses. It also compares abstract classes to interfaces, noting that abstract classes can contain fields and implemented methods while interfaces contain only public static final fields and public abstract methods. The document provides an example of an abstract class AbstractMap that is extended by HashMap and TreeMap subclasses. It also discusses nested classes in Java and the differences between inner and static nested classes.

Uploaded by

Shrikant Sharma
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/ 14

Classes that can be instantiated

Ghoul  class  
Declaring  a class as abstract
•  creates  “abstract  types”  whose  
implementa6ons  are  incomplete  or  non-­‐
existent  
•  define  &  enforce  a  protocol  that  must  be  
supported  by  subclasses  
package java.util;

public abstract class AbstractMap<K,V> ... {


...
}

public class HashMap<K,V> extends AbstractMap<K,V>


{ ... }

public class TreeMap<K,V> extends AbstractMap<K,V>


{ ... }

AbstractMap is the super class for Map implementations


Declaring an abstract method
•  An  abstract  method  is  a  method  declared  
without  a  body  
•  Subclasses  must  override  these  abstract  
methods  and  provide  implementa6on  details  

public abstract void revive();


package java.util;

public abstract class AbstractMap<K,V> ... {


...
public abstract Set<Entry<K,V>> entrySet();
...
}

public class HashMap<K,V> extends AbstractMap<K,V>


{
public abstract Set<Entry<K,V>> entrySet(){
...
Abstract classes can not instantiate objects
•  An  abstract  class  can  be  extended,  but  not  instan6ated  
// Fails 

AbstractMap<Integer,String> map1 = new AbstractMap<>();

•  A  subclass  that  implements  all  abstract  methods  can  be  instan6ated  


// Works 

AbstractMap<Integer, String> map2 = new HashMap<>();
Comparing Abstract Classes & Interfaces
Abstract  classes  are  similar  to  interfaces  
•  They  can’t  be  instan6ated  
•  They  can  contain  methods  declared  without  
any  implementa6on  

However,  abstract  classes  have  


addi4onal  capabili4es  
Comparing Abstract Classes & Interfaces  
Abstract  classes   Interfaces  
•  can  define  fields  that  are     •  all  fields  must  be  public,  
not  sta6c  nor  final   sta6c  &  final  
•  can  also  define  public,   •  all  methods  must  be  public  &  
protected,  &  private   not  implemented  
concrete  methods  
Comparing Abstract Classes & Interfaces  
•  A  class  can  extend  only  one  class  
public class AtomicGhoul extends Ghoul{...

•  A  class  can  implement  several  interfaces  


public class HazMatBox extends Box
implements ShippingContainer,
implements Cube{...
Overview of Java Nested Classes
A  nested  class  is  defined  within  an  enclosing  class  
Overview of Java Nested Classes
package java.util;
Nested  classes  provide  several    
benefits   public class Vector<E> ... {
...
•  Grouping  together  classes  that     public Spliterator<E> 

are  only  used  in  one  place   spliterator() {
return new 

•  Increasing  encapsula6on   VectorSpliterator<>(...);
}
•  Enhancing  maintainability  
static final class 

VectorSpliterator<E> 

implements Spliterator<E> 

{ ... }

...
There are two types of nested classes
Overview of Java Nested Classes
package java.util;
public class Vector<E> ... {
...
int mCount;
...

private class Itr 

implements Iterator<E>{
int cursor; 

...
public boolean hasNext() {
return cursor != mCount;
}

An inner nested class can reference


non-static instance methods & fields
Overview of Java Nested Classes
package java.util;

public class Vector<E> ... {


...


static final class VectorSpliterator<E> implementsSpliterator<E> { 



... 

private Object[] array;
private int index; 

... 

}

...
A static nested class can’t reference
non-static instance methods & fields

You might also like