Inheritance Lakshmish Ramaswamy Interface Alternative to multiple inheritance

  • Slides: 12
Download presentation
Inheritance Lakshmish Ramaswamy

Inheritance Lakshmish Ramaswamy

Interface • Alternative to multiple inheritance • The ultimate abstract class • Consists only

Interface • Alternative to multiple inheritance • The ultimate abstract class • Consists only of public abstract methods & public static final fields – No implementation of any method • Class providing definitions of all abstract methods is said to implement the interface • Multiple interfaces do not suffer from same problems as multiple inheritance • Class can implement multiple interfaces but extend from a single class

Specifying Interface • Similar to a class declaration but uses the keyword interface •

Specifying Interface • Similar to a class declaration but uses the keyword interface • Lists methods that need to be implemented • Class implementing an interface should do two things – Declare that it implements the interface using “implements” – Provide implementations of all methods declared in interface • A method implementing an interface may remain abstract

Example of Interface

Example of Interface

Example of Implementing Class

Example of Implementing Class

Multiple Interfaces • Java allows multiple interfaces • List all the interfaces a class

Multiple Interfaces • Java allows multiple interfaces • List all the interfaces a class is implementing • Provide implementations of all methods in all interfaces public class X implements a, b { … }

Interfaces as Abstract Classes • IS-A relationship holds • instanceof operator can be used

Interfaces as Abstract Classes • IS-A relationship holds • instanceof operator can be used to test type compatibility • Implementing class cannot reduce the visibility of methods • Implementing class may not add checked exceptions to throws list – In case of multiple interfaces implementation can throw exception that is listed in all interfaces

Interfaces as Abstract Classes • Class implementing an interface should override with the exact

Interfaces as Abstract Classes • Class implementing an interface should override with the exact signature • Cannot implement two interfaces with same signature but different return types • Class that partially implements an interface should be declared abstract • Interfaces can extend other interfaces

Fundamental Inheritance in Java • If class does not explicitly extend another class, it

Fundamental Inheritance in Java • If class does not explicitly extend another class, it implicitly extends Object class • Object contains several methods • Object is not abstract class – All methods are implemented • to. String, equals and hash. Code are some of the important methods

Exception Hierarchy • Exceptions in Java are organized as a hierarchy • Root is

Exception Hierarchy • Exceptions in Java are organized as a hierarchy • Root is Throwable – Set of Print. Stack. Trace methods, pair of constructor and to. String method • First level children Error and Exception • Any exception that is not Run. Time. Exception is checked exception • Generally, exception extends another exception but provides constructors

Nested Classes • A class declaration within another class declaration (the outer class) –

Nested Classes • A class declaration within another class declaration (the outer class) – The keyword “static” is used before inner classname • The inner class is considered as a member of outer class • Can be public, private, package or protected • Typically private • Inner class methods can access private static members of the outer class and private instance members when given a reference