Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Java Interitance Example

This is the first example of Java Inheritance shown on our recent Java course - the second example is here

Anitest.java


public class Anitest {

public static void main(String [] args) {

Farm ermintrude = new Farm("Cow",100,20);
Farm cocky = new Farm("Cockerel",4,2,2);
Pet belle = new Pet("Cat",5,300);

int ew = ermintrude.getweight();
int ev = ermintrude.getvalue();
String et = ermintrude.getbreed();
int el = ermintrude.getlegs();
System.out.println(et + " which weighs " + ew +
" worth " + ev + " legs: "+el);

ew = cocky.getweight();
ev = cocky.getvalue();
et = cocky.getbreed();
el = cocky.getlegs();
System.out.println(et + " which weighs " + ew +
" worth " + ev + " legs: "+el);

ew = belle.getweight();
ev = belle.getvalue();
et = belle.getbreed();
el = belle.getlegs();
System.out.println(et + " which weighs " + ew +
" worth " + ev + " legs: "+el);
}}

Farm.java


public class Farm extends Animal {
int vpk;

public Farm (String breed, int weight, int vpk) {
super(breed, weight);
this.vpk = vpk;
}

public Farm (String breed, int weight, int vpk, int legs) {
super(breed, weight, legs);
this.vpk = vpk;
}

public int getvalue() {
return vpk * weight;
}
}

Pet.java


public class Pet extends Animal {
int v;

public Pet (String breed, int weight, int v) {
super(breed, weight);
this.v = v;
}

public int getvalue() {
return v;
}
}

Animal.java


public class Animal {
String breed;
int weight;
int legs;

public Animal(String breed, int weight) {
this.breed = breed;
this.weight = weight;
this.legs = 4;
}

public Animal(String breed, int weight, int legs) {
this.breed = breed;
this.weight = weight;
this.legs = legs;
}

public String getbreed() {
return breed;
}

public int getweight() {
return weight;
}

public int getlegs() {
return legs;
}
}



OPTIONS AVAILABLE TO YOU
You may follow links in the text to explore other topics.
Extra link - Return to the HomePage of this share system

STATUS AND CONTROL
You are a guest
Enter name and password to


This is a page from Well House Consultant's shared information directory. Learn about our shared information system.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD. 2010 : Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 344596 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho