Sunday, October 19, 2008

Section 1.2: Concrete and Abstract Class, and Interfaces

Requirement: Describe, compare, and contrast concrete classes, abstract classes, and interfaces, and how inheritance applies to them.


Classes:

(1.2.1) A class is a template or blueprint from which objects are made. (1.2.2) An object constructed from a particular class is said to be an instance of that class. (1.2.3)* In a single source file you can have multiple classes but exactly one class must be declared as public (see the 1st and 2nd comments for corrections, thanks to Tchulda). (1.2.4) The name of the source file must match the name of the public class declared in that file.


Abstract Classes:

(1.2.5) A class with one or more abstract methods must itself be declared abstract. (1.2.6) An abstract method is a method declared with the abstract keyword and has no implementation (ex: public abstract String getDescription();). (1.2.7) Abstract classes can have fields and concrete methods. (1.2.8) When you extend an abstract class, if the subclass leaves some or all of the abstract methods undefined then the subclass must be declared abstract as well. (1.2.9) When you extend an abstract class, if the subclass implements all of the abstract methods then the subclass is no longer abstract unless you declare some abstract methods in the subclass. (1.2.10) Abstract classes cannot be instantiated. (1.2.11) You can create object variables from an abstract class, but such variable must refer to an object of a non-abstract class which is a subclass of the abstract class. (1.2.12) In this case, when you call a method that is abstract in the abstract class what will be executed actually is the implementation of that method in the non-abstract class.


Interfaces:

(1.2.13) In Java, an interface is not a class but a set of requirements for classes that want to conform to that interface. (1.2.14) All methods of an interface are automatically public so there is no need to use the public keyword with each method in the interface. (1.2.15) When you implement an interface in a particular class you must declare the implemented interface methods as public or the compiler will consider them to have the package visibility (i.e. they are not considered to be public despite they considered public by default in the interface). (1.2.16) Interfaces can define constants (ex: public interface Interface1 { int SOME_CONST = 12; //This is considered public static final int SOME_CONST = 12}) so fields are always considered to be public static final even if you don't specify that in your code. (1.2.17) Interfaces never have instance fields and methods are never implemented in interfaces. (1.2.18) You cannot instantiate interface objects with the new keyword (since interface are not classes) but you can declare interface variables which refer to an object of class that implements the interface. (1.2.19) You can use instaceof keyword to test whether an object implements an interface (ex: if(anObj instanceof Interface1) { ... }). (1.2.20) Classes can implement multiple interfaces.


Inheritance:

(1.2.21) The idea behind inheritance is that you can create new classes that are built on existing classes. (1.2.22) The new class reuses the old class's methods and fields and adds new methods and fields to add new functionality. (1.2.23) The new class is called subclass, derived class, or child class while the old class is called superclass, base class, or parent class. (1.2.24) When there is a method that is implemented in both the superclass and subclass but you want to use the superclass version in the subclass, you have to use the super keyword (ex: super.someMethod();). (1.2.25) Inheritance hierarchy is the set of classes extending from a common superclass. (1.2.26) Inheritance chain is the path from a particular class to its ancestors in the inheritance hierarchy.

(Approximately all of the material written here is taken from the references mentioned in the first post. I don't claim to have authored any part of this material . I just compile my own notes from one or many of these references).

2 comments:

Tchulda said...

In a single source file you can have multiple classes but exactly one class must be declared as public.
That's not completely true. You may have a source file without any public class.

Kareem Ergawy said...

After some experiments, I ended up with the following 6 possibilities:

1 - The source file is empty: in this case the code compiles without any problems.

2 - The source file contains only 1 class which is NOT declared as public: in this case the code compiles without and problems AND the source file can be named with any name. The name of the source file can be the same as the name of the class or any other name.

3 - The source file contains only 1 class which declared as public: in this case the code compiles ONLY IF the name of the source file is the same as the name of the declared public class.

4 - The source file contains many class where NONE of them is declared as public: in this case the code compiles without any problems. It is NOT a must that name of the source file matches any of the declared classes.

5 - The source file contains many classes where ONLY 1 class is declarec as public: in this case the code compiles without any problems BUT the name of the source file MUST match the name of the declared public class. If this is not the case the code will not compile correctly.

6 - The source file contians many classes where MANY classes are declared as public: in this case the code will NOT compile.