I like the example so much , many thanks :)

Yes we can see clearly how the car class can be used as many references.

Now i am wondering how these classes with its data represented in the memory
?? i mean parent class and child.

Thanks

Nada






On Wed, Dec 23, 2009 at 12:11 AM, etudient.java <[email protected]>wrote:

> The past two homeworks are examples of Polymorphism.
>
> taking from the JavaPassion page on Polymorphism,
>
> "Polymorphism is the ability to appear in many forms."
>
> "Subclasses of a class can define their own unique behaviors and yet
> share some of the same functionality of the parent class."
>
> A CAR CAN APPEAR IN MANY FORMS, i.e models (Toyota,GMC,FORM,NISSAN etc).
>
> i.e you have a Car class
>
> public class Car{
>
>        int speed;
>        int numberOfDoors;
>        String modelBrand;
>        String countryOfManufacturer;
>
>        public Car(int speed,int numberOfDoors,String modelBrand,String
>                        countryOfManufacturer){
>                this.speed = speed;
>                this.numberOfDoors = numberOfDoors;
>                this.modelBrand = modelBrand;
>                this.countryOfManufacturer = countryOfManufacturer;
>
>                }
>
>        //getter and setter methods below
>
>         public String getModelBrand() {
>                return modelBrand;
>        }
>
>        public void setModelBrand(String modelBrand) {
>                this.modelBrand = modelBrand;
>        }
>
>        public int getNumberOfDoors() {
>                return numberOfDoors;
>        }
>
>        public void setNumberOfDoors(int numberOfDoors) {
>                this.numberOfDoors = numberOfDoors;
>        }
>
>        public int getSpeed() {
>                return speed;
>        }
>
>        public void setSpeed(int speed) {
>                this.speed = speed;
>        }
>
>        //WILL PRINT VARIABLE VALUES OF YOUR OBJECT
>
>        public void print() {
>
>        System.out.println("The name of your class  = " + getClass());
>        System.out.println("countryOfManufacturer  = " +
> getCountryOfManufacturer());
>        System.out.println("modelBrand  = " + getModelBrand());
>        System.out.println("numberOfDoors  = " + getNumberOfDoors());
>        System.out.println("speed  = " + getSpeed());
>
>
>    }
>
>
> }
>
> You can extend the Car class in classes Toyota,GMC,FORM,NISSAN etc.
>
> i.e, you have a Toyota class
>
> public class Toyota extends Car{
>
>
>
>        public Toyota(int speed,int numberOfDoors,String modelBrand,String
> countryOfManufacturer){
> super(speed,numberOfDoors,"Camry","Japan");//For all Toyota objects the
> modelBrand is "Camry", countryOfManufaturer is "Japan"
>
>                }
>
>
>
> }
>
>
>
> Lets now look at the Main class that will instantiate a Toyota object.
>
> public class Main{
>
>        public static void main(String[] args){
>
>                 Car toyota1 = new Toyota(200,4,"Yaris","China"); //"Yaris"
> and "China"
> will not be assigned to their respective variables
>                toyota1.print(); //will print values of toyota1 variables
>
>
>                }
> }
>
> Output;
>
> The name of your class  = class javaexample.Toyota
> countryOfManufacturer  = Japan
> modelBrand  = Camry
> numberOfDoors  = 4
> speed  = 200
>
>
>
> Why is this important;
>
> This is also good for encapsulating your code.
>
> See attachment for Files
>
>
>
> --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]<javaprogrammingwithpassion%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to