1.2: Fundamental Object-Oriented Concepts

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

A concrete class is a regular class that can be instantiated.

An abstract class cannot be instantiated and must be extended. It may also contain abstract methods which are not implemented. The abstract keyword is used to indicate that it is an abstract class. They have valid method signature ut must be overriden and implemented in the calss that extends the abstract class. The purpose of an abstract method is to define the required functionality that any subclass must have.

Interfaces are used to define a required set of functionalities from that classes that implement the interface. Unlike inheriting from base classes, a class is free to implement as many interfaces as needed by using the extends keyword, followed by a comma-delimited list of interfaces. A class that implements an interface is required to implement all of the methods defined in the interface. Any unimplemented methods will result in compilation errors. Generally, interfaces are used to create a standard public interface for similar items. The interface keyword is used to specify that it is an interface while the implements keyword is used to implement an interface.

Inheritance allows general classes to be created and then be used as the foundation for multiple unique classes. In order words, unique classes are allowed to inherit the methods and instance variables of more general classes. Inheritance reduces codes redundancy and makes codes more maintainable. Inheritance enables Polymorphism to be used. A class can only extend 1 class where multiple inheritance is impossible. The extends keyword is used to inherits from a superclass to become a subclass. The subclass may then adds new methods and instance variables that are unique to the subclass.

A class that extends another class may override any inherited method by defining another method of the same name with the same arguments as in the superclass. A class may override all, none or only some of the methods it inherits from a super class. A subclass may also execute the superclass overriden method by using the super keyword (opposite of this keyword).

Object is the base class in Java programming language and does not need to be explicitly extended with the extends keyword.

0 comments:

Post a Comment