Inheritance in Java Assignment Help

Assignment Help: >> Object-Oriented Programming Languages - Inheritance in Java

Inheritance Basics

For inherit a class, you simply incorporate the definition of one class into another through using extends keyword. To see how, let's starts along with a short instance. The following program creates a superclass called A and a subclass called B. Note that how the keyword extends is used to create a subclass of A

// A easy instance of inheritance

// Create a superclass class A {

int i, j ;

void showij( ) {

System.out.println("i and j : " + i  + " " + j ) ;

}

}

// create a subclass via extending class A

class B extends A {

int k;

void showk( ) {

System.out.println("i+j+k: " + ( i+j+k));

}

}

class SimpleInheritance {

public static void main(String args[ ] ) {

A superOb = new A( );

B subOb = new B ( );

// The supeclass may be used by itself

superOb.i = 10;

superOb.j = 20;

System.out println("Contents of superOb: ");

superOb.showij( );

System.out.println( );

/* The subclass has access to all public members of its superclass. */

subOb.i = 7;

subOb.j = 8;

subOb.k = 9;

System.out.println("Contents of subOb: ");

subOb.showij( );

subOb.showk( );

System.out.println( );

System.out.println("Sum of i,j and k in subOb:");

SubOb.sum ( );

}

}

The output from this program is show

Contents of superOb:

i and j : 10 20

Contents of subOb;

i and j: 7 8

Sum of I,j and k in subOb;

i+j+k: 24

As you could see, a subclass B includes all of the members of its superclass, A. This is why subOb can access i an j and call showij( ). Also, within sum ( ),i and   j call showij( ). Also, inside sum ( ),i and j can be referred to Straightly, as if they were part of B.

Even by A is a superclass for B that is also a finished independent, stand-alone class. To be a superclass for a subclass does not mean in which the superclass cannot be used through itself. Additionally, subclass can be a superclass for another subclass.

The common form of a class declaration which inherits a superclass is displays here:

class subclass-name extends superclass-name{

//body of class

}

You can only specify one superclass for any subclass which you create. Java does  not  support  the  inheritance  of  multiple  superclasses  into  a  single subclass. (This is different from C++, in that you can inherit multiple base class.). Create  a  hierarchy  of  inheritance  in  that  a  subclass  becomes  a superclass of another subclass. Moreover, no class could be a superclass of itself.

Final Keyword Super Keyword
Use of Super Using Final to Prevent Inheritance
Using Final to Prevent Overriding Using Final with Inheritance
Using Super to Call Superclass Constructors
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd