Support Forums

Full Version: [Tutorial] Java Inheritance
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi! I'm here again to explain you more about java and it's programmation, in this part of the tutorial I'm gonna explain some about inheritance and how it works. This part it's easy when you know what are the objects and classes, I explained it in my second tutorial, Java 2 (Classes, objects and methods) .

The inheritance it's a good part for not to do COPY & PASTE of a bunch of code, so this will help you not to write a lot and do the program easy to understand. This is one of the most important parts and it's the base of the objects programmation, so it's useful because of the inheritance and polymorphism that I'm going to explain in the next tutorial, that maybe I'm going to do it today, so you will be able to learn a lot today!

LET'S START!!!!!!!!!!!!!

Inheritance is useful when you have to make a program about something that have some kind of that something this is hard but it's easy to understand when you learn an example, here is:

A program like a shop car, there is a Class named Car, (Yes!! Like I wrote in the second tutorial Big Grin) and a kind of car are the Ferrari, that are more powerful than Honda for example, so, there is a FATHER class and two or more CHILD classes Big Grin see:


Code:
-------------------------
|          CAR          |
-------------------------
         /                  \
      /                        \
------------------       -------------------
|    Ferrari      |     |       Honda      |
-----------------        -------------------

(So It's like a family, yes Big Grin)

Now, the methods that are written in the class CAR, the child classes are going to know it's methods so it's not needed to write it AGAIN!!! (wooooooow it's for lazy people!! Yeah, for me Big Grin) then, MAKE ME AN EXAMPLE! yes! I was going to do it now!

Code:
class Car {
    protected int tyres, doors, seats; //initializing variables to set a value later. and protected for not to be changed out this method
    Car (int tyres, int doors, int seats) {
        this.tyres = tyres;
        this.doors = doors;
        this.seats = seats;
    }
    public int getTyres() { return tyres; }  //get the number of tyres, doors or seats
    public int getDoors() { return doors; }
    public int getSeats() { return seats; }
    public int setTyres(int tyres) { this.tyres = tyres; } //change the number of tyres
}


class Ferrari extends Car {
    int power;
    Ferrari(int power) {
        this.power = power;
    }
    public getPower() { return power; }
}
class Honda extends Car {
    int space;
    Honda (int space) {
        this.space = space;
    }
    public int getSpace() { return space; }
}

Easy, no?


BUT HOW CAN I USE IT?!?!?!? Shhh, calm, I'm going to do a main class to show people how to use.

Code:
Code:
class Car {
    protected int tyres, doors, seats; //initializing variables to set a value later. and protected for not to be changed out this method
    Car (int tyres, int doors, int seats) {
        this.tyres = tyres;
        this.doors = doors;
        this.seats = seats;
    }
    public int getTyres() { return tyres; }  //get the number of tyres, doors or seats
    public int getDoors() { return doors; }
    public int getSeats() { return seats; }
    public int setTyres(int tyres) { this.tyres = tyres; } //change the number of tyres
}


class Ferrari extends Car {
    int power;
    Ferrari(int tyres, int doors, int seats, int power) {
        super(tyres, doors, seats); //This is for calling the builder of the SUPERCLASS (dad class :D)
        this.power = power;
    }
    public getPower() { return power; }
}
class Honda extends Car {
    int space;
    Honda (int tyres, int doors, int seats, int space) {
        super(tyres,doors,seats);
        this.space = space;
    }
    public int getSpace() { return space; }
}
class Main {
    public static void main(String[] args) {
        Ferrari testarrosa = new Ferrari(4,5,2,200);//tyres,doors,seats,power
        Honda honda = new Honda(4,4,5,100); //tyres,doors,seats,space
        System.out.println("Seats: "+testarrosa.getSeats());
        System.out.println("Seats: "+honda.getSeats());
        //And now lets print the other "options"
        System.out.println("Power: "+testarrosa.getPower());
        System.out.println("Space: "+honda.getSpace());
    }
}

This is the end with a full example explaining and now, let's make your own!!

See you in the next tutorial!! about polymorphism!!! Big Grin

+rep is welcome Big Grin
Keep up the tuts. Some nice examples and I hope all the starters can follow you. Big Grin
nice tut.........sure going to help me..........
Thank you, I wrote them for another forum but I have copied them to here, now, I'll write more, but I have no ideas for doing more tutorials for basic java, I'm not any expert ^^