Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tutorial] Java Inheritance
#1
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
Reply
#2
Keep up the tuts. Some nice examples and I hope all the starters can follow you. Big Grin
Reply
#3
nice tut.........sure going to help me..........
CHINAMAN
Quote:i like to make myself belie
[Image: 2n7ehx4.jpg]
[Image: save_banner.asp?file=HWHERMJMMH.jpg]
Reply
#4
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 ^^
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Java Tutorial United States of America 0 682 01-23-2012, 03:39 PM
Last Post: United States of America
  why java haphazard 8 1,757 12-12-2011, 03:23 AM
Last Post: RDCA
  Need Java help from java expert. Blazin 2 2,093 09-07-2011, 02:43 PM
Last Post: AceInfinity
  Java help php 1 1,033 04-06-2010, 06:41 AM
Last Post: php
  Java Problem for real java programmer Testgamma1 10 5,142 03-14-2010, 09:08 AM
Last Post: uber1337

Forum Jump:


Users browsing this thread: 1 Guest(s)