{"id":3446,"date":"2019-06-20T11:43:07","date_gmt":"2019-06-20T11:43:07","guid":{"rendered":"http:\/\/softlect.in\/?p=3446"},"modified":"2019-06-20T12:01:20","modified_gmt":"2019-06-20T12:01:20","slug":"java-inheritance","status":"publish","type":"post","link":"http:\/\/softlect.com\/index.php\/java-inheritance\/","title":{"rendered":"Java Inheritance"},"content":{"rendered":"<h1><a name=\"_Toc179309346\"><\/a><strong>Inheritance<\/strong><\/h1>\n<p>&nbsp;<\/p>\n<p>Inheritance is one of the foundations of object-oriented programming because it allows the creation of hierarchical classifications. Using inheritance, you can create a general class that defines behavior common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In the terminology of Java, a class that is inherited is called a <em>superclass. <\/em>The class that does the inheriting is called a <em>subclass. <\/em><\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309347\"><\/a><strong>Inheritance Basics<\/strong><\/h2>\n<p>To inherit a class, you simply incorporate the definition of one class into another by using the <strong>extends <\/strong>keyword. To see how, let\u2019s begin with a short example. The following program creates a superclass called <strong>A <\/strong>and a subclass called <strong>B<\/strong>. Notice how the keyword <strong>extends <\/strong>is used to create a subclass of <strong>A<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ A simple example of inheritance.<\/p>\n<p>\/\/ Create a superclass.<\/p>\n<p>class A {<\/p>\n<p>int i, j;<\/p>\n<p>void showij() {<\/p>\n<p>System.out.println(&#8220;i and j: &#8221; + i + &#8221; &#8221; + j);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ Create a subclass by extending class A.<\/p>\n<p>class B extends A {<\/p>\n<p>int k;<\/p>\n<p>void showk() {<\/p>\n<p>System.out.println(&#8220;k: &#8221; + k);<\/p>\n<p>}<\/p>\n<p>void sum() {<\/p>\n<p>System.out.println(&#8220;i+j+k: &#8221; + (i+j+k));<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class SimpleInheritance {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>A superOb = new A();<\/p>\n<p>B subOb = new B();<\/p>\n<p>\/\/ The superclass may be used by itself.<\/p>\n<p>superOb.i = 10;<\/p>\n<p>superOb.j = 20;<\/p>\n<p>System.out.println(&#8220;Contents of superOb: &#8220;);<\/p>\n<p>superOb.showij();<\/p>\n<p>System.out.println();<\/p>\n<p>\/* The subclass has access to all public members of<\/p>\n<p>its superclass. *\/<\/p>\n<p>subOb.i = 7;<\/p>\n<p>subOb.j = 8;<\/p>\n<p>subOb.k = 9;<\/p>\n<p>System.out.println(&#8220;Contents of subOb: &#8220;);<\/p>\n<p>subOb.showij();<\/p>\n<p>subOb.showk();<\/p>\n<p>System.out.println();<\/p>\n<p>System.out.println(&#8220;Sum of i, j and k in subOb:&#8221;);<\/p>\n<p>subOb.sum();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The output from this program is shown here:<\/p>\n<p>Contents of superOb:<\/p>\n<p>i and j: 10 20<\/p>\n<p>Contents of subOb:<\/p>\n<p>i and j: 7 8<\/p>\n<p>k: 9<\/p>\n<p>Sum of i, j and k in subOb:<\/p>\n<p>i+j+k: 24<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, the subclass <strong>B <\/strong>includes all of the members of its superclass, <strong>A<\/strong>. This is why <strong>subOb <\/strong>can access <strong>i <\/strong>and <strong>j <\/strong>and call <strong>showij( )<\/strong>. Also, inside <strong>sum( )<\/strong>, <strong>i <\/strong>and <strong>j <\/strong>can be referred to directly, as if they were part of <strong>B<\/strong>. Even though <strong>A <\/strong>is a superclass for <strong>B<\/strong>, it is also a completely independent, stand-alone class. Being a superclass for a subclass does not mean that the superclass cannot be used by itself. Further, a subclass can be a superclass for another subclass. The general form of a <strong>class <\/strong>declaration that inherits a superclass is shown here:<\/p>\n<p>&nbsp;<\/p>\n<p>class <em>subclass-name <\/em>extends <em>superclass-name <\/em>{<\/p>\n<p>\/\/ body of class<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>You can only specify one superclass for any subclass that you create. Java does not support the inheritance of multiple superclasses into a single subclass. You can, as stated, create a hierarchy of inheritance in which a subclass becomes a superclass of another subclass. However, no class can be a superclass of itself.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309348\"><\/a><strong>Member Access and Inheritance<\/strong><\/h2>\n<p>Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as <strong>private<\/strong>. For example, consider the following simple class hierarchy:<\/p>\n<p>&nbsp;<\/p>\n<p>\/* In a class hierarchy, private members remain<\/p>\n<p>private to their class.<\/p>\n<p>This program contains an error and will not<\/p>\n<p>compile.<\/p>\n<p>*\/<\/p>\n<p>\/\/ Create a superclass.<\/p>\n<p>class A {<\/p>\n<p>int i; \/\/ public by default<\/p>\n<p>private int j; \/\/ private to A<\/p>\n<p>void setij(int x, int y) {<\/p>\n<p>i = x;<\/p>\n<p>j = y;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ A&#8217;s j is not accessible here.<\/p>\n<p>class B extends A {<\/p>\n<p>int total;<\/p>\n<p>void sum() {<\/p>\n<p>total = i + j; \/\/ ERROR, j is not accessible here<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class Access {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>B subOb = new B();<\/p>\n<p>subOb.setij(10, 12);<\/p>\n<p>subOb.sum();<\/p>\n<p>System.out.println(&#8220;Total is &#8221; + subOb.total);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>This program will not compile because the reference to <strong>j <\/strong>inside the <strong>sum( ) <\/strong>method of <strong>B <\/strong>causes an access violation. Since <strong>j <\/strong>is declared as <strong>private<\/strong>, it is only accessible by other members of its own class. Subclasses have no access to it. Here, the final version of the <strong>Box <\/strong>class developed in the preceding chapter will be extended to include a fourth component called <strong>weight<\/strong>. Thus, the new class will contain a box\u2019s width, height, depth, and weight.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ This program uses inheritance to extend Box.<\/p>\n<p>class Box {<\/p>\n<p>double width;<\/p>\n<p>double height;<\/p>\n<p>double depth;<\/p>\n<p>\/\/ construct clone of an object<\/p>\n<p>Box(Box ob) { \/\/ pass object to constructor<\/p>\n<p>width = ob.width;<\/p>\n<p>height = ob.height;<\/p>\n<p>depth = ob.depth;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when all dimensions specified<\/p>\n<p>Box(double w, double h, double d) {<\/p>\n<p>width = w;<\/p>\n<p>height = h;<\/p>\n<p>depth = d;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when no dimensions specified<\/p>\n<p>Box() {<\/p>\n<p>width = -1; \/\/ use -1 to indicate<\/p>\n<p>height = -1; \/\/ an uninitialized<\/p>\n<p>depth = -1; \/\/ box<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when cube is created<\/p>\n<p>Box(double len) {<\/p>\n<p>width = height = depth = len;<\/p>\n<p>}<\/p>\n<p>\/\/ compute and return volume<\/p>\n<p>double volume() {<\/p>\n<p>return width * height * depth;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ Here, Box is extended to include weight.<\/p>\n<p>class BoxWeight extends Box {<\/p>\n<p>double weight; \/\/ weight of box<\/p>\n<p>\/\/ constructor for BoxWeight<\/p>\n<p>BoxWeight(double w, double h, double d, double m) {<\/p>\n<p>width = w;<\/p>\n<p>height = h;<\/p>\n<p>depth = d;<\/p>\n<p>weight = m;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class DemoBoxWeight {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);<\/p>\n<p>BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);<\/p>\n<p>double vol;<\/p>\n<p>vol = mybox1.volume();<\/p>\n<p>System.out.println(&#8220;Volume of mybox1 is &#8221; + vol);<\/p>\n<p>System.out.println(&#8220;Weight of mybox1 is &#8221; + mybox1.weight);<\/p>\n<p>System.out.println();<\/p>\n<p>vol = mybox2.volume();<\/p>\n<p>System.out.println(&#8220;Volume of mybox2 is &#8221; + vol);<\/p>\n<p>System.out.println(&#8220;Weight of mybox2 is &#8221; + mybox2.weight);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>The output from this program is shown here:<\/p>\n<p>Volume of mybox1 is 3000.0<\/p>\n<p>Weight of mybox1 is 34.3<\/p>\n<p>Volume of mybox2 is 24.0<\/p>\n<p>Weight of mybox2 is 0.076<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>BoxWeight <\/strong>inherits all of the characteristics of <strong>Box <\/strong>and adds to them the <strong>weight <\/strong>component. It is not necessary for <strong>BoxWeight <\/strong>to re-create all of the features found in <strong>Box<\/strong>. It can simply extend <strong>Box <\/strong>to meet its own purposes. A major advantage of inheritance is that once you have created a superclass that defines the attributes common to a set of objects, it can be used to create any number of more specific subclasses. For example, the following class inherits <strong>Box <\/strong>and adds a color attribute:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Here, Box is extended to include color.<\/p>\n<p>class ColorBox extends Box {<\/p>\n<p>int color; \/\/ color of box<\/p>\n<p>ColorBox(double w, double h, double d, int c) {<\/p>\n<p>width = w;<\/p>\n<p>height = h;<\/p>\n<p>depth = d;<\/p>\n<p>color = c;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Remember, once you have created a superclass that defines the general aspects of an object, that superclass can be inherited to form specialized classes. Each subclass simply adds its own, unique attributes.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309349\"><\/a><strong>A Superclass Variable Can Reference a Subclass Object<\/strong><\/h2>\n<p>A reference variable of a superclass can be assigned a reference to any subclass derived from that superclass. For example, consider the following:<\/p>\n<p>&nbsp;<\/p>\n<p>class RefDemo {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);<\/p>\n<p>Box plainbox = new Box();<\/p>\n<p>double vol;<\/p>\n<p>vol = weightbox.volume();<\/p>\n<p>System.out.println(&#8220;Volume of weightbox is &#8221; + vol);<\/p>\n<p>System.out.println(&#8220;Weight of weightbox is &#8221; +<\/p>\n<p>weightbox.weight);<\/p>\n<p>System.out.println();<\/p>\n<p>\/\/ assign BoxWeight reference to Box reference<\/p>\n<p>plainbox = weightbox;<\/p>\n<p>vol = plainbox.volume(); \/\/ OK, volume() defined in Box<\/p>\n<p>System.out.println(&#8220;Volume of plainbox is &#8221; + vol);<\/p>\n<p>\/* The following statement is invalid because plainbox<\/p>\n<p>does not define a weight member. *\/<\/p>\n<p>\/\/ System.out.println(&#8220;Weight of plainbox is &#8221; + plainbox.weight);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Here, <strong>weightbox <\/strong>is a reference to <strong>BoxWeight <\/strong>objects, and <strong>plainbox <\/strong>is a reference to <strong>Box <\/strong>objects. Since <strong>BoxWeight <\/strong>is a subclass of <strong>Box<\/strong>, it is permissible to assign <strong>plainbox <\/strong>a reference to the <strong>weightbox <\/strong>object.<\/p>\n<p>It is important to understand that it is the type of the reference variable not the type of the object that it refers to that determines what members can be accessed. That is, when a reference to a subclass object is assigned to a superclass reference variable, you will have access only to those parts of the object defined by the superclass. This is why <strong>plainbox <\/strong>can\u2019t access <strong>weight <\/strong>even when it refers to a <strong>BoxWeight <\/strong>object. This is why the last line of code in the preceding fragment is commented out. It is not possible for a <strong>Box <\/strong>reference to access the <strong>weight <\/strong>field, because it does not define one.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309350\"><\/a><strong>Using super<\/strong><\/h2>\n<p>For example, the constructor for <strong>BoxWeight <\/strong>explicitly initializes the <strong>width<\/strong>, <strong>height<\/strong>, and <strong>depth <\/strong>fields of <strong>Box( )<\/strong>. Not only does this duplicate code found in its superclass, which is inefficient, but it implies that a subclass must be granted access to these members. However, there will be times when you will want to create a superclass that keeps the details of its implementation to itself that is, that keeps its data members private. In this case, there would be no way for a subclass to directly access or initialize these variables on its own. Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword <strong>super<\/strong>. <strong>super <\/strong>has two general forms. The first calls the superclass\u2019 constructor. The second is used to access a member of the superclass that has been hidden by a member of a subclass. Each use is examined here.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309351\"><\/a><strong>Using super to Call Superclass Constructors<\/strong><\/h2>\n<p>A subclass can call a constructor method defined by its superclass by use of the following form of <strong>super<\/strong>:<\/p>\n<p>super(<em>parameter<\/em>&#8211;<em>list<\/em>);<\/p>\n<p>&nbsp;<\/p>\n<p>Here, <em>parameter<\/em>&#8211;<em>list <\/em>specifies any parameters needed by the constructor in the superclass. <strong>super( ) <\/strong>must always be the first statement executed inside a subclass\u2019 constructor. To see how <strong>super( ) <\/strong>is used, consider this improved version of the <strong>BoxWeight( ) <\/strong>class:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ BoxWeight now uses super to initialize its Box attributes.<\/p>\n<p>class BoxWeight extends Box {<\/p>\n<p>double weight; \/\/ weight of box<\/p>\n<p>\/\/ initialize width, height, and depth using super()<\/p>\n<p>BoxWeight(double w, double h, double d, double m) {<\/p>\n<p>super(w, h, d); \/\/ call superclass constructor<\/p>\n<p>weight = m;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Here, <strong>BoxWeight( ) <\/strong>calls <strong>super( ) <\/strong>with the parameters <strong>w<\/strong>, <strong>h<\/strong>, and <strong>d<\/strong>. This causes the <strong>Box( ) <\/strong>constructor to be called, which initializes <strong>width<\/strong>, <strong>height<\/strong>, and <strong>depth <\/strong>using these\u00a0 values. <strong>BoxWeight <\/strong>no longer initializes these values itself. It only needs to initialize the value unique to it: <strong>weight<\/strong>. This leaves <strong>Box <\/strong>free to make these values <strong>private <\/strong>if desired. In the preceding example, <strong>super( ) <\/strong>was called with three arguments. Since constructors can be overloaded, <strong>super( ) <\/strong>can be called using any form defined by the superclass. The constructor executed will be the one that matches the arguments. For example, here is a complete implementation of <strong>BoxWeight <\/strong>that provides constructors for the various ways that a box can be constructed. In each case, <strong>super( ) <\/strong>is called using the appropriate arguments. Notice that <strong>width<\/strong>, <strong>height<\/strong>, and <strong>depth <\/strong>have been made private within <strong>Box<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ A complete implementation of BoxWeight.<\/p>\n<p>class Box {<\/p>\n<p>private double width;<\/p>\n<p>private double height;<\/p>\n<p>private double depth;<\/p>\n<p>\/\/ construct clone of an object<\/p>\n<p>Box(Box ob) { \/\/ pass object to constructor<\/p>\n<p>width = ob.width;<\/p>\n<p>height = ob.height;<\/p>\n<p>depth = ob.depth;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when all dimensions specified<\/p>\n<p>Box(double w, double h, double d) {<\/p>\n<p>width = w;<\/p>\n<p>height = h;<\/p>\n<p>depth = d;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when no dimensions specified<\/p>\n<p>Box() {<\/p>\n<p>width = -1; \/\/ use -1 to indicate<\/p>\n<p>height = -1; \/\/ an uninitialized<\/p>\n<p>depth = -1; \/\/ box<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when cube is created<\/p>\n<p>Box(double len) {<\/p>\n<p>width = height = depth = len;<\/p>\n<p>}<\/p>\n<p>\/\/ compute and return volume<\/p>\n<p>double volume() {<\/p>\n<p>return width * height * depth;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ BoxWeight now fully implements all constructors.<\/p>\n<p>class BoxWeight extends Box {<\/p>\n<p>double weight; \/\/ weight of box<\/p>\n<p>\/\/ construct clone of an object<\/p>\n<p>BoxWeight(BoxWeight ob) { \/\/ pass object to constructor<\/p>\n<p>super(ob);<\/p>\n<p>weight = ob.weight;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor when all parameters are specified<\/p>\n<p>BoxWeight(double w, double h, double d, double m) {<\/p>\n<p>super(w, h, d); \/\/ call superclass constructor<\/p>\n<p>weight = m;<\/p>\n<p>}<\/p>\n<p>\/\/ default constructor<\/p>\n<p>BoxWeight() {<\/p>\n<p>super();<\/p>\n<p>weight = -1;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when cube is created<\/p>\n<p>BoxWeight(double len, double m) {<\/p>\n<p>super(len);<\/p>\n<p>weight = m;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class DemoSuper {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);<\/p>\n<p>BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);<\/p>\n<p>BoxWeight mybox3 = new BoxWeight(); \/\/ default<\/p>\n<p>BoxWeight mycube = new BoxWeight(3, 2);<\/p>\n<p>BoxWeight myclone = new BoxWeight(mybox1);<\/p>\n<p>double vol;<\/p>\n<p>vol = mybox1.volume();<\/p>\n<p>System.out.println(&#8220;Volume of mybox1 is &#8221; + vol);<\/p>\n<p>System.out.println(&#8220;Weight of mybox1 is &#8221; + mybox1.weight);<\/p>\n<p>System.out.println();<\/p>\n<p>vol = mybox2.volume();<\/p>\n<p>System.out.println(&#8220;Volume of mybox2 is &#8221; + vol);<\/p>\n<p>System.out.println(&#8220;Weight of mybox2 is &#8221; + mybox2.weight);<\/p>\n<p>System.out.println();<\/p>\n<p>vol = mybox3.volume();<\/p>\n<p>System.out.println(&#8220;Volume of mybox3 is &#8221; + vol);<\/p>\n<p>System.out.println(&#8220;Weight of mybox3 is &#8221; + mybox3.weight);<\/p>\n<p>System.out.println();<\/p>\n<p>vol = myclone.volume();<\/p>\n<p>System.out.println(&#8220;Volume of myclone is &#8221; + vol);<\/p>\n<p>System.out.println(&#8220;Weight of myclone is &#8221; + myclone.weight);<\/p>\n<p>System.out.println();<\/p>\n<p>vol = mycube.volume();<\/p>\n<p>System.out.println(&#8220;Volume of mycube is &#8221; + vol);<\/p>\n<p>System.out.println(&#8220;Weight of mycube is &#8221; + mycube.weight);<\/p>\n<p>System.out.println();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>This program generates the following output:<\/p>\n<p>Volume of mybox1 is 3000.0<\/p>\n<p>Weight of mybox1 is 34.3<\/p>\n<p>Volume of mybox2 is 24.0<\/p>\n<p>Weight of mybox2 is 0.076<\/p>\n<p>Volume of mybox3 is -1.0<\/p>\n<p>Weight of mybox3 is -1.0<\/p>\n<p>Volume of myclone is 3000.0<\/p>\n<p>Weight of myclone is 34.3<\/p>\n<p>Volume of mycube is 27.0<\/p>\n<p>Weight of mycube is 2.0<\/p>\n<p>Pay special attention to this constructor in <strong>BoxWeight( )<\/strong>:<\/p>\n<p>\/\/ construct clone of an object<\/p>\n<p>BoxWeight(BoxWeight ob) { \/\/ pass object to constructor<\/p>\n<p>super(ob);<\/p>\n<p>weight = ob.weight;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Notice that <strong>super( ) <\/strong>is called with an object of type <strong>BoxWeight <\/strong>not of type <strong>Box<\/strong>. This still invokes the constructor <strong>Box(Box ob)<\/strong>. As mentioned earlier, a superclass variable can be used to reference any object derived from that class. Thus, we are able to pass a <strong>BoxWeight <\/strong>object to the <strong>Box <\/strong>constructor. Of course, <strong>Box <\/strong>only has knowledge of its own members. Let\u2019s review the key concepts behind <strong>super( )<\/strong>. When a subclass calls <strong>super( )<\/strong>, it is calling the constructor of its immediate superclass. Thus, <strong>super( ) <\/strong>always refers to the superclass immediately above the calling class. This is true even in a multileveled hierarchy. Also, <strong>super( ) <\/strong>must always be the first statement executed inside a subclass constructor.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309352\"><\/a><strong>A Second Use for super<\/strong><\/h2>\n<p>The second form of <strong>super <\/strong>acts somewhat like <strong>this<\/strong>, except that it always refers to the superclass of the subclass in which it is used. This usage has the following general form:<\/p>\n<p>&nbsp;<\/p>\n<p>super.<em>member<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Here, <em>member <\/em>can be either a method or an instance variable. This second form of <strong>super <\/strong>is most applicable to situations in which member names of a subclass hide members by the same name in the superclass. Consider this simple class hierarchy:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Using super to overcome name hiding.<\/p>\n<p>class A {<\/p>\n<p>int i;<\/p>\n<p>}<\/p>\n<p>\/\/ Create a subclass by extending class A.<\/p>\n<p>class B extends A {<\/p>\n<p>int i; \/\/ this i hides the i in A<\/p>\n<p>B(int a, int b) {<\/p>\n<p>super.i = a; \/\/ i in A<\/p>\n<p>i = b; \/\/ i in B<\/p>\n<p>}<\/p>\n<p>void show() {<\/p>\n<p>System.out.println(&#8220;i in superclass: &#8221; + super.i);<\/p>\n<p>System.out.println(&#8220;i in subclass: &#8221; + i);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class UseSuper {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>B subOb = new B(1, 2);<\/p>\n<p>subOb.show();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>This program displays the following:<\/p>\n<p>i in superclass: 1<\/p>\n<p>i in subclass: 2<\/p>\n<p>&nbsp;<\/p>\n<p>Although the instance variable <strong>i <\/strong>in <strong>B <\/strong>hides the <strong>i <\/strong>in <strong>A<\/strong>, <strong>super <\/strong>allows access to the <strong>I <\/strong>defined in the superclass. As you will see, <strong>super <\/strong>can also be used to call methods that are hidden by a subclass.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309353\"><\/a><strong>Creating a Multilevel Hierarchy<\/strong><\/h2>\n<p>Up to this point, we have been using simple class hierarchies that consist of only a superclass and a subclass. However, you can build hierarchies that contain as many layers of inheritance as you like. As mentioned, it is perfectly acceptable to use a subclass as a superclass of another. For example, given three classes called <strong>A<\/strong>, <strong>B<\/strong>, and <strong>C<\/strong>, <strong>C <\/strong>can be a subclass of <strong>B<\/strong>, which is a subclass of <strong>A<\/strong>. When this type of situation occurs, each subclass inherits all of the traits found in all of its superclasses. In this case, <strong>C <\/strong>inherits all aspects of <strong>B <\/strong>and <strong>A<\/strong>. To see how a multilevel hierarchy can be useful, consider the following program. In it, the subclass <strong>BoxWeight <\/strong>is used as a superclass to create the subclass called <strong>Shipment<\/strong>. <strong>Shipment <\/strong>inherits all of the traits of <strong>BoxWeight <\/strong>and <strong>Box<\/strong>, and adds a field called <strong>cost<\/strong>, which holds the cost of shipping such a parcel.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Extend BoxWeight to include shipping costs.<\/p>\n<p>\/\/ Start with Box.<\/p>\n<p>class Box {<\/p>\n<p>private double width;<\/p>\n<p>private double height;<\/p>\n<p>private double depth;<\/p>\n<p>\/\/ construct clone of an object<\/p>\n<p>Box(Box ob) { \/\/ pass object to constructor<\/p>\n<p>width = ob.width;<\/p>\n<p>height = ob.height;<\/p>\n<p>depth = ob.depth;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when all dimensions specified<\/p>\n<p>Box(double w, double h, double d) {<\/p>\n<p>width = w;<\/p>\n<p>height = h;<\/p>\n<p>depth = d;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when no dimensions specified<\/p>\n<p>Box() {<\/p>\n<p>width = -1; \/\/ use -1 to indicate<\/p>\n<p>height = -1; \/\/ an uninitialized<\/p>\n<p>depth = -1; \/\/ box<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when cube is created<\/p>\n<p>Box(double len) {<\/p>\n<p>width = height = depth = len;<\/p>\n<p>}<\/p>\n<p>\/\/ compute and return volume<\/p>\n<p>double volume() {<\/p>\n<p>return width * height * depth;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ Add weight.<\/p>\n<p>class BoxWeight extends Box {<\/p>\n<p>double weight; \/\/ weight of box<\/p>\n<p>\/\/ construct clone of an object<\/p>\n<p>BoxWeight(BoxWeight ob) { \/\/ pass object to constructor<\/p>\n<p>super(ob);<\/p>\n<p>weight = ob.weight;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor when all parameters are specified<\/p>\n<p>BoxWeight(double w, double h, double d, double m) {<\/p>\n<p>super(w, h, d); \/\/ call superclass constructor<\/p>\n<p>weight = m;<\/p>\n<p>}<\/p>\n<p>\/\/ default constructor<\/p>\n<p>BoxWeight() {<\/p>\n<p>super();<\/p>\n<p>weight = -1;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when cube is created<\/p>\n<p>BoxWeight(double len, double m) {<\/p>\n<p>super(len);<\/p>\n<p>weight = m;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ Add shipping costs<\/p>\n<p>class Shipment extends BoxWeight {<\/p>\n<p>double cost;<\/p>\n<p>\/\/ construct clone of an object<\/p>\n<p>Shipment(Shipment ob) { \/\/ pass object to constructor<\/p>\n<p>super(ob);<\/p>\n<p>cost = ob.cost;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor when all parameters are specified<\/p>\n<p>Shipment(double w, double h, double d,<\/p>\n<p>double m, double c) {<\/p>\n<p>super(w, h, d, m); \/\/ call superclass constructor<\/p>\n<p>cost = c;<\/p>\n<p>}<\/p>\n<p>\/\/ default constructor<\/p>\n<p>Shipment() {<\/p>\n<p>super();<\/p>\n<p>cost = -1;<\/p>\n<p>}<\/p>\n<p>\/\/ constructor used when cube is created<\/p>\n<p>Shipment(double len, double m, double c) {<\/p>\n<p>super(len, m);<\/p>\n<p>cost = c;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class DemoShipment {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>Shipment shipment1 =<\/p>\n<p>new Shipment(10, 20, 15, 10, 3.41);<\/p>\n<p>Shipment shipment2 =<\/p>\n<p>new Shipment(2, 3, 4, 0.76, 1.28);<\/p>\n<p>double vol;<\/p>\n<p>vol = shipment1.volume();<\/p>\n<p>System.out.println(&#8220;Volume of shipment1 is &#8221; + vol);<\/p>\n<p>System.out.println(&#8220;Weight of shipment1 is &#8221;<\/p>\n<p>+ shipment1.weight);<\/p>\n<p>System.out.println(&#8220;Shipping cost: $&#8221; + shipment1.cost);<\/p>\n<p>System.out.println();<\/p>\n<p>vol = shipment2.volume();<\/p>\n<p>System.out.println(&#8220;Volume of shipment2 is &#8221; + vol);<\/p>\n<p>System.out.println(&#8220;Weight of shipment2 is &#8221;<\/p>\n<p>+ shipment2.weight);<\/p>\n<p>System.out.println(&#8220;Shipping cost: $&#8221; + shipment2.cost);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>The output of this program is shown here:<\/p>\n<p>Volume of shipment1 is 3000.0<\/p>\n<p>Weight of shipment1 is 10.0<\/p>\n<p>Shipping cost: $3.41<\/p>\n<p>Volume of shipment2 is 24.0<\/p>\n<p>Weight of shipment2 is 0.76<\/p>\n<p>Shipping cost: $1.28<\/p>\n<p>&nbsp;<\/p>\n<p>Because of inheritance, <strong>Shipment <\/strong>can make use of the previously defined classes of <strong>Box <\/strong>and <strong>BoxWeight<\/strong>, adding only the extra information it needs for its own, specific application. This is part of the value of inheritance; it allows the reuse of code. This example illustrates one other important point: <strong>super( ) <\/strong>always refers to the constructor in the closest superclass. The <strong>super( ) <\/strong>in <strong>Shipment <\/strong>calls the constructor in <strong>BoxWeight<\/strong>. The <strong>super( ) <\/strong>in <strong>BoxWeight <\/strong>calls the constructor in <strong>Box<\/strong>. In a class hierarchy, if a superclass constructor requires parameters, then all subclasses must pass those parameters \u201cup the line.\u201d This is true whether or not a subclass needs parameters of its own.<\/p>\n<p><em>\u00a0<\/em><\/p>\n<h2><a name=\"_Toc179309354\"><\/a><strong>When Constructors Are Called<\/strong><\/h2>\n<p>When a class hierarchy is created, in what order are the constructors for the classes that make up the hierarchy called? The answer is that in a class hierarchy, constructors are called in order of derivation, from superclass to subclass. Further, since <strong>super( ) <\/strong>must be the first statement executed in a subclass\u2019 constructor, this order is the same whether or not <strong>super( ) <\/strong>is used. If <strong>super( ) <\/strong>is not used, then the default or parameterless constructor of each superclass will be executed. The following program illustrates when constructors are executed:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate when constructors are called.<\/p>\n<p>\/\/ Create a super class.<\/p>\n<p>class A {<\/p>\n<p>A() {<\/p>\n<p>System.out.println(&#8220;Inside A&#8217;s constructor.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ Create a subclass by extending class A.<\/p>\n<p>class B extends A {<\/p>\n<p>B() {<\/p>\n<p>System.out.println(&#8220;Inside B&#8217;s constructor.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ Create another subclass by extending B.<\/p>\n<p>class C extends B {<\/p>\n<p>C() {<\/p>\n<p>System.out.println(&#8220;Inside C&#8217;s constructor.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class CallingCons {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>C c = new C();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The output from this program is shown here:<\/p>\n<p>Inside A\u2019s constructor<\/p>\n<p>Inside B\u2019s constructor<\/p>\n<p>Inside C\u2019s constructor<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, the constructors are called in order of derivation.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309355\"><\/a><strong>Method Overriding<\/strong><\/h2>\n<p>In a class hierarchy, when a method in a subclass has the same name and type signature or prototype as a method in its superclass, then the method in the subclass is said to <em>override <\/em>the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden. Consider the following:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Method overriding.<\/p>\n<p>class A {<\/p>\n<p>int i, j;<\/p>\n<p>A(int a, int b) {<\/p>\n<p>i = a;<\/p>\n<p>j = b;<\/p>\n<p>}<\/p>\n<p>\/\/ display i and j<\/p>\n<p>void show() {<\/p>\n<p>System.out.println(&#8220;i and j: &#8221; + i + &#8221; &#8221; + j);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class B extends A {<\/p>\n<p>int k;<\/p>\n<p>B(int a, int b, int c) {<\/p>\n<p>super(a, b);<\/p>\n<p>k = c;<\/p>\n<p>}<\/p>\n<p>\/\/ display k \u2013 this overrides show() in A<\/p>\n<p>void show() {<\/p>\n<p>System.out.println(&#8220;k: &#8221; + k);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class Override {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>B subOb = new B(1, 2, 3);<\/p>\n<p>subOb.show(); \/\/ this calls show() in B<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>The output produced by this program is shown here:<\/p>\n<p>k: 3<\/p>\n<p>&nbsp;<\/p>\n<p>When <strong>show( ) <\/strong>is invoked on an object of type <strong>B<\/strong>, the version of <strong>show( ) <\/strong>defined\u00a0 within <strong>B <\/strong>is used. That is, the version of <strong>show( ) <\/strong>inside <strong>B <\/strong>overrides the version declared in <strong>A<\/strong>. If you wish to access the superclass version of an overridden function, you can do so by using <strong>super<\/strong>. For example, in this version of <strong>B<\/strong>, the superclass version of <strong>show( ) <\/strong>is invoked within the subclass\u2019 version. This allows all instance variables to be displayed.<\/p>\n<p>&nbsp;<\/p>\n<p>class B extends A {<\/p>\n<p>int k;<\/p>\n<p>B(int a, int b, int c) {<\/p>\n<p>super(a, b);<\/p>\n<p>k = c;<\/p>\n<p>}<\/p>\n<p>void show() {<\/p>\n<p>super.show(); \/\/ this calls A&#8217;s show()<\/p>\n<p>System.out.println(&#8220;k: &#8221; + k);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>If you substitute this version of <strong>A <\/strong>into the previous program, you will see the following output:<\/p>\n<p>i and j: 1 2<\/p>\n<p>k: 3<\/p>\n<p>&nbsp;<\/p>\n<p>Here, <strong>super.show( ) <\/strong>calls the superclass version of <strong>show( )<\/strong>. Method overriding occurs <em>only <\/em>when the names and the type signatures of the two methods are identical. For example, consider this modified version of the preceding example:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Methods with differing type signatures are overloaded \u2013 not<\/p>\n<p>\/\/ overridden.<\/p>\n<p>class A {<\/p>\n<p>int i, j;<\/p>\n<p>A(int a, int b) {<\/p>\n<p>i = a;<\/p>\n<p>j = b;<\/p>\n<p>}<\/p>\n<p>\/\/ display i and j<\/p>\n<p>void show() {<\/p>\n<p>System.out.println(&#8220;i and j: &#8221; + i + &#8221; &#8221; + j);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ Create a subclass by extending class A.<\/p>\n<p>class B extends A {<\/p>\n<p>int k;<\/p>\n<p>B(int a, int b, int c) {<\/p>\n<p>super(a, b);<\/p>\n<p>k = c;<\/p>\n<p>}<\/p>\n<p>\/\/ overload show()<\/p>\n<p>void show(String msg) {<\/p>\n<p>System.out.println(msg + k);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class Override {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>B subOb = new B(1, 2, 3);<\/p>\n<p>subOb.show(&#8220;This is k: &#8220;); \/\/ this calls show() in B<\/p>\n<p>subOb.show(); \/\/ this calls show() in A<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>The output produced by this program is shown here:<\/p>\n<p>This is k: 3<\/p>\n<p>i and j: 1 2<\/p>\n<p>&nbsp;<\/p>\n<p>The version of <strong>show( ) <\/strong>in <strong>B <\/strong>takes a string parameter. This makes its type signature different from the one in <strong>A<\/strong>, which takes no parameters. Therefore, no overriding (or name hiding) takes place.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309356\"><\/a><strong>Dynamic Method Dispatch<\/strong><\/h2>\n<p>Method overriding forms the basis for one of Java\u2019s most powerful concepts: <em>dynamic method dispatch. <\/em>Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism. a superclass reference variable can refer to a subclass object. Java uses this fact to resolve calls to overridden methods at run time. Here is how. When an overridden method is called through a superclass reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs. Thus, this determination is made at run time. When different types of objects are referred to, different versions of an overridden method will be called. if a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable, different versions of the method are executed. Here is an example that illustrates dynamic method dispatch:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Dynamic Method Dispatch<\/p>\n<p>class A {<\/p>\n<p>void callme() {<\/p>\n<p>System.out.println(&#8220;Inside A&#8217;s callme method&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class B extends A {<\/p>\n<p>\/\/ override callme()<\/p>\n<p>void callme() {<\/p>\n<p>System.out.println(&#8220;Inside B&#8217;s callme method&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class C extends A {<\/p>\n<p>\/\/ override callme()<\/p>\n<p>void callme() {<\/p>\n<p>System.out.println(&#8220;Inside C&#8217;s callme method&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class Dispatch {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>A a = new A(); \/\/ object of type A<\/p>\n<p>B b = new B(); \/\/ object of type B<\/p>\n<p>C c = new C(); \/\/ object of type C<\/p>\n<p>A r; \/\/ obtain a reference of type A<\/p>\n<p>r = a; \/\/ r refers to an A object<\/p>\n<p>r.callme(); \/\/ calls A&#8217;s version of callme<\/p>\n<p>r = b; \/\/ r refers to a B object<\/p>\n<p>r.callme(); \/\/ calls B&#8217;s version of callme<\/p>\n<p>THE JAVA LANGUAGE<\/p>\n<p>r = c; \/\/ r refers to a C object<\/p>\n<p>r.callme(); \/\/ calls C&#8217;s version of callme<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>The output from the program is shown here:<\/p>\n<p>Inside A\u2019s callme method<\/p>\n<p>Inside B\u2019s callme method<\/p>\n<p>Inside C\u2019s callme method<\/p>\n<p>&nbsp;<\/p>\n<p>This program creates one superclass called <strong>A <\/strong>and two subclasses of it, called <strong>B <\/strong>and <strong>C<\/strong>. Subclasses <strong>B <\/strong>and <strong>C <\/strong>override <strong>callme( ) <\/strong>declared in <strong>A<\/strong>. Inside the <strong>main( ) <\/strong>method, objects of type <strong>A<\/strong>, <strong>B<\/strong>, and <strong>C <\/strong>are declared. Also, a reference of type <strong>A<\/strong>, called <strong>r<\/strong>, is declared. The program then assigns a reference to each type of object to <strong>r <\/strong>and uses that reference to invoke <strong>callme( )<\/strong>. As the output shows, the version of <strong>callme( ) <\/strong>executed is determined by the type of object being referred to at the time of the call. Had it been determined by the type of the reference variable, <strong>r<\/strong>, you would see three calls to <strong>A<\/strong>\u2019s <strong>callme( ) <\/strong>method.<\/p>\n<p>&nbsp;<\/p>\n<p>Let\u2019s look at a more practical example that uses method overriding. The following program creates a superclass called <strong>Figure <\/strong>that stores the dimensions of various two-dimensional objects. It also defines a method called <strong>area( ) <\/strong>that computes the area of an object. The program derives two subclasses from <strong>Figure<\/strong>. The first is <strong>Rectangle <\/strong>and the second is <strong>Triangle<\/strong>. Each of these subclasses overrides <strong>area( ) <\/strong>so that it returns the area of a rectangle and a triangle, respectively.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Using run-time polymorphism.<\/p>\n<p>class Figure {<\/p>\n<p>double dim1;<\/p>\n<p>double dim2;<\/p>\n<p>Figure(double a, double b) {<\/p>\n<p>dim1 = a;<\/p>\n<p>dim2 = b;<\/p>\n<p>}<\/p>\n<p>double area() {<\/p>\n<p>System.out.println(&#8220;Area for Figure is undefined.&#8221;);<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class Rectangle extends Figure {<\/p>\n<p>Rectangle(double a, double b) {<\/p>\n<p>super(a, b);<\/p>\n<p>}<\/p>\n<p>\/\/ override area for rectangle<\/p>\n<p>double area() {<\/p>\n<p>System.out.println(&#8220;Inside Area for Rectangle.&#8221;);<\/p>\n<p>return dim1 * dim2;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class Triangle extends Figure {<\/p>\n<p>Triangle(double a, double b) {<\/p>\n<p>super(a, b);<\/p>\n<p>}<\/p>\n<p>\/\/ override area for right triangle<\/p>\n<p>double area() {<\/p>\n<p>System.out.println(&#8220;Inside Area for Triangle.&#8221;);<\/p>\n<p>return dim1 * dim2 \/ 2;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class FindAreas {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>Figure f = new Figure(10, 10);<\/p>\n<p>Rectangle r = new Rectangle(9, 5);<\/p>\n<p>Triangle t = new Triangle(10, 8);<\/p>\n<p>Figure figref;<\/p>\n<p>figref = r;<\/p>\n<p>System.out.println(&#8220;Area is &#8221; + figref.area());<\/p>\n<p>figref = t;<\/p>\n<p>System.out.println(&#8220;Area is &#8221; + figref.area());<\/p>\n<p>figref = f;<\/p>\n<p>System.out.println(&#8220;Area is &#8221; + figref.area());<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>The output from the program is shown here:<\/p>\n<p>Inside Area for Rectangle.<\/p>\n<p>Area is 45<\/p>\n<p>Inside Area for Triangle.<\/p>\n<p>Area is 40<\/p>\n<p>Area for Figure is undefined.<\/p>\n<p>Area is 0<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309357\"><\/a><strong>Using Abstract Classes<\/strong><\/h2>\n<p>There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. That is, sometimes you will want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details. Such a class determines the nature of the methods that the subclasses must implement. One way this situation can occur is when a superclass is unable to create a meaningful implementation for a method. This is the case with the class <strong>Figure <\/strong>used in the preceding example. The definition of <strong>area( ) <\/strong>is simply a placeholder. It will not compute and display the area of any type of object. You may have methods which must be overridden by the subclass in order for the subclass to have any meaning. Consider the class <strong>Triangle<\/strong>. It has no meaning if <strong>area( ) <\/strong>is not defined. In this case, you want some way to ensure that a subclass does, indeed, override all necessary methods. Java\u2019s solution to this problem is the <em>abstract method. <\/em>You can require that certain methods be overridden by subclasses by specifying the <strong>abstract <\/strong>type modifier. Thus, a subclass must override them it cannot simply use the version defined in the superclass. To declare an abstract method, use this general form:<\/p>\n<p>&nbsp;<\/p>\n<p>abstract <em>type name(parameter-list)<\/em>;<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, no method body is present. Any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the <strong>abstract <\/strong>keyword in front of the <strong>class <\/strong>keyword at the beginning of the class declaration. There can be no objects of an abstract class. That is, an abstract class cannot be directly instantiated with the <strong>new operator<\/strong>. Such objects would be useless, because an abstract class is not fully defined. Also, you cannot declare abstract constructors, or abstract static methods. Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared <strong>abstract<\/strong>. Here is a simple example of a class with an abstract method, followed by a class which implements that method:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ A Simple demonstration of abstract.<\/p>\n<p>abstract class A {<\/p>\n<p>abstract void callme();<\/p>\n<p>\/\/ concrete methods are still allowed in abstract classes<\/p>\n<p>void callmetoo() {<\/p>\n<p>System.out.println(&#8220;This is a concrete method.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class B extends A {<\/p>\n<p>void callme() {<\/p>\n<p>System.out.println(&#8220;B&#8217;s implementation of callme.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class AbstractDemo {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>B b = new B();<\/p>\n<p>b.callme();<\/p>\n<p>b.callmetoo();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Notice that no objects of class <strong>A <\/strong>are declared in the program. As mentioned, it is not possible to instantiate an abstract class. One other point: class <strong>A <\/strong>implements a concrete method called <strong>callmetoo( )<\/strong>. This is perfectly acceptable. Abstract classes can include as much implementation as they see fit. Although abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java\u2019s approach to run-time polymorphism is implemented through the use of superclass references. Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object. For example:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Using abstract methods and classes.<\/p>\n<p>abstract class Figure {<\/p>\n<p>double dim1;<\/p>\n<p>double dim2;<\/p>\n<p>Figure(double a, double b) {<\/p>\n<p>dim1 = a;<\/p>\n<p>dim2 = b;<\/p>\n<p>}<\/p>\n<p>\/\/ area is now an abstract method<\/p>\n<p>abstract double area();<\/p>\n<p>}<\/p>\n<p>class Rectangle extends Figure {<\/p>\n<p>Rectangle(double a, double b) {<\/p>\n<p>super(a, b);<\/p>\n<p>}<\/p>\n<p>\/\/ override area for rectangle<\/p>\n<p>double area() {<\/p>\n<p>System.out.println(&#8220;Inside Area for Rectangle.&#8221;);<\/p>\n<p>return dim1 * dim2;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class Triangle extends Figure {<\/p>\n<p>Triangle(double a, double b) {<\/p>\n<p>super(a, b);<\/p>\n<p>}<\/p>\n<p>\/\/ override area for right triangle<\/p>\n<p>double area() {<\/p>\n<p>System.out.println(&#8220;Inside Area for Triangle.&#8221;);<\/p>\n<p>return dim1 * dim2 \/ 2;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class AbstractAreas {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>\/\/ Figure f = new Figure(10, 10); \/\/ illegal now<\/p>\n<p>Rectangle r = new Rectangle(9, 5);<\/p>\n<p>Triangle t = new Triangle(10, 8);<\/p>\n<p>THE JAVA LANGUAGE<\/p>\n<p>Figure figref; \/\/ this is OK, no object is created<\/p>\n<p>figref = r;<\/p>\n<p>System.out.println(&#8220;Area is &#8221; + figref.area());<\/p>\n<p>figref = t;<\/p>\n<p>System.out.println(&#8220;Area is &#8221; + figref.area());<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>As the comment inside <strong>main( ) <\/strong>indicates, it is no longer possible to declare objects of type <strong>Figure<\/strong>, since it is now abstract. And, all subclasses of <strong>Figure <\/strong>must override <strong>area( )<\/strong>. Although it is not possible to create an object of type <strong>Figure<\/strong>, you can create a reference variable of type <strong>Figure<\/strong>. The variable <strong>figref <\/strong>is declared as a reference to <strong>Figure<\/strong>, which means that it can be used to refer to an object of any class derived from <strong>Figure<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309358\"><\/a><strong>Using final to Prevent Overriding<\/strong><\/h2>\n<p>While method overriding is one of Java\u2019s most powerful features, there will be times when you will want to prevent it from occurring. To disallow a method from being overridden, specify <strong>final <\/strong>as a modifier at the start of its declaration. Methods declared as <strong>final <\/strong>cannot be overridden. The following fragment illustrates <strong>final<\/strong>:<\/p>\n<p>&nbsp;<\/p>\n<p>class A {<\/p>\n<p>final void meth() {<\/p>\n<p>System.out.println(&#8220;This is a final method.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class B extends A {<\/p>\n<p>void meth() { \/\/ ERROR! Can&#8217;t override.<\/p>\n<p>System.out.println(&#8220;Illegal!&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Because <strong>meth( ) <\/strong>is declared as <strong>final<\/strong>, it cannot be overridden in <strong>B<\/strong>. If you attempt to do so, a compile-time error will result. Normally, Java resolves calls to methods dynamically, at run time. This is called <em>late binding. <\/em>However, since <strong>final <\/strong>methods cannot be overridden, a call to one can be resolved at compile time. This is called <em>early binding.<\/em><\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309359\"><\/a><strong>Using final to Prevent Inheritance<\/strong><\/h2>\n<p>Sometimes you will want to prevent a class from being inherited. To do this, precede the class declaration with <strong>final<\/strong>. Declaring a class as <strong>final <\/strong>implicitly declares all of its methods as <strong>final<\/strong>. As you might expect, it is illegal to declare a class as both <strong>abstract <\/strong>and <strong>final <\/strong>since an abstract class is incomplete by itself and relies upon its subclasses to provide complete implementations. Here is an example of a <strong>final <\/strong>class:<\/p>\n<p>&nbsp;<\/p>\n<p>final class A {<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>}<\/p>\n<p>\/\/ The following class is illegal.<\/p>\n<p>class B extends A { \/\/ ERROR! Can&#8217;t subclass A<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>As the comments imply, it is illegal for <strong>B <\/strong>to inherit <strong>A <\/strong>since <strong>A <\/strong>is declared as <strong>final<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance &nbsp; Inheritance is one of the foundations of object-oriented programming because it allows the creation of hierarchical classifications. Using inheritance, you can create a general class that defines behavior&hellip; <\/p>\n","protected":false},"author":1,"featured_media":3468,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[83],"tags":[],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3446"}],"collection":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/comments?post=3446"}],"version-history":[{"count":2,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3446\/revisions"}],"predecessor-version":[{"id":3485,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3446\/revisions\/3485"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media\/3468"}],"wp:attachment":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media?parent=3446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/categories?post=3446"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/tags?post=3446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}