Inheritance is an important part of object- oriented programming (OOP). It allows you to extend and enhance another class with out having that classes' source code or even knowing how that class works. This is great because you can create a class to your liking without changing the existing class which won't screw up any other applications that use the existing class.
We will continue on with the code from the previous two lessons. First we need something to inherit from. We will use the original Square class from Introduction to Classes.
the new class we will create will be a Cube. Create a new .java file and call it Cube. This class will inherit from the Square because a cube is basically six squares put together to form a box. Now to inherit another class you use the keyword extends after the class declaration.
public class Cube extends Square
Cube now has every method that Square has and functions just like a Square does. This is because it inherited (just like you inherit traits from your parents) everything from the Square. Also note that it did not inherit the constructors so those will have to be re-declared. It also didn't inherit the variable for the length because it is private but there is a public property we can use to get and set the value.
The constructors for Cube will just call the constructors of Square.
public Cube(double len)
Calling super is used to reference the parent class (Square). Since we are using it in a constructor is must be the first line in the constructor. We also pass any necessary parameters to the parent constructor.
One difference between a square and a cube is the Area. That means we have to change how the area is calculated by Overriding the Area method. The Area of a cube is simply length * length * 6 which is the area of a square times 6. Therefore we can still use the Square to make our calculation easier.
public double area()
return super.area() * 6;
Calling super.area() will call the area method in Square and return Length * Length. Therefore all we have to do is multiply it by 6 (the number of sides a cube has).
Now on to the final part of the Cube class. Cube is a three dimensional shape and what do 3D shapes have? Volume. To calculate the volume of a cube it is simply Length3 or Length * Length * Length.
public double volume()
double len = super.getLength();
return len * len * len;
Since we do not have access to length we have to use getLength of the base class (Square) to get it.
Time to test! Notice that you can declare a Square variable and set it to a new Cube object. When you call the Area function it still calls the Area function of the Cube so you get the correct result. This works just like it did with the IFourSidedShape interface. The variable holds a Cube object but the type is Square so it can access all the properties and methods of the Square class. Again to be able to call the Volume function you have to cast it.
public static void main(String args)
Square r = new Cube(2);
System.out.println("Area of the cube is " + r.area());
Cube c = (Cube)r;
System.out.println("Volume of the cube is " + c.volume());
Keep in mind that Java does not allow multiple inheritance but you can extend a class that extends another class (and so on).
Also note that an Interface can inherit another Interface and can actually inherit many other Interfaces unlike classes. If you implement an Interface that inherits from one or more other Interfaces then you must implement all methods and fields of all the interfaces.
I mentioned final in the Introduction to Classes lesson. Using the keyword final on a method or field means that the method or field cannot be overwritten. Using it on a class means that the class cannot be inherited (extended).
Congratulations you have completed the Introduction to Inheritance tutorial. If wish you can now continue on with the Abstract Class lesson.
Add your comments here.
You can also send feedback to firstname.lastname@example.org
There are currently no comments available.
|» Domain name registration|
|» E commerce solutions|
|» Flash design websites|
|» Flash designing india|
|» Google ranking services|
|» Joomla website developers|
|» Mobile application development|
|» OS commerce website development|
|» php website development|
|» Search engine marketing|
|» Seo services india|
|» Software development india|
|» Web design services|
|» Web development india|
|» Website redesign services|
|» Wordpress website developers|
|» Magento website developers|
|» Search engine services|
|» Search engine optimization|
|» Freelance php developers|
|» Hire mobile developers|
|» Zen cart developers|
|» X cart developers|
|» Open cart developers|
|» Blog developers india|
|» Content management system|
|» Article submission india|
|» CRM development india|