Java Tutorial/Class Definition/Inheritance — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:00, 1 июня 2010
Содержание
- 1 Accessibility
- 2 Cleanup and inheritance
- 3 Combining composition and inheritance
- 4 Creating a Multilevel Hierarchy
- 5 Demonstrate when constructors are called in a Multilevel Hierarchy
- 6 Derived Class Constructors: Calling the Base Class Constructor
- 7 Deriving a Class
- 8 Inheritance
- 9 Inheritance and upcasting.
- 10 Inheritance, constructors and arguments
- 11 Method Overriding
- 12 Overloading a base-class method name in a derived class does not hide the base-class versions.
- 13 Overriding a Base Class Method
- 14 The extends Keyword
- 15 The keyword super represents an instance of the direct superclass of the current object.
- 16 Type Casting
Accessibility
Within a subclass you can access its superclass"s public and protected methods and fields , but not the superclass"s private methods. If the subclass and the superclass are in the same package, you can also access the superclass"s default methods and fields.
public class P {
public void publicMethod() {
}
protected void protectedMethod() {
}
void defaultMethod() {
}
}
class C extends P {
public void testMethod() {
publicMethod();
protectedMethod();
defaultMethod();
}
}
Cleanup and inheritance
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
class Characteristic {
private String s;
Characteristic(String s) {
this.s = s;
System.out.println("Creating Characteristic " + s);
}
protected void dispose() {
System.out.println("finalizing Characteristic " + s);
}
}
class Description {
private String s;
Description(String s) {
this.s = s;
System.out.println("Creating Description " + s);
}
protected void dispose() {
System.out.println("finalizing Description " + s);
}
}
class LivingCreature {
private Characteristic p = new Characteristic("is alive");
private Description t = new Description("Basic Living Creature");
LivingCreature() {
System.out.println("LivingCreature()");
}
protected void dispose() {
System.out.println("LivingCreature dispose");
t.dispose();
p.dispose();
}
}
class Animal extends LivingCreature {
private Characteristic p = new Characteristic("has heart");
private Description t = new Description("Animal not Vegetable");
Animal() {
System.out.println("Animal()");
}
protected void dispose() {
System.out.println("Animal dispose");
t.dispose();
p.dispose();
super.dispose();
}
}
class Amphibian extends Animal {
private Characteristic p = new Characteristic("can live in water");
private Description t = new Description("Both water and land");
Amphibian() {
System.out.println("Amphibian()");
}
protected void dispose() {
System.out.println("Amphibian dispose");
t.dispose();
p.dispose();
super.dispose();
}
}
class Frog extends Amphibian {
private Characteristic p = new Characteristic("Croaks");
private Description t = new Description("Eats Bugs");
public Frog() {
System.out.println("Frog()");
}
protected void dispose() {
System.out.println("Frog dispose");
t.dispose();
p.dispose();
super.dispose();
}
}
public class MainClass {
public static void main(String[] args) {
Frog frog = new Frog();
System.out.println("Bye!");
frog.dispose();
}
}
Creating Characteristic is alive Creating Description Basic Living Creature LivingCreature() Creating Characteristic has heart Creating Description Animal not Vegetable Animal() Creating Characteristic can live in water Creating Description Both water and land Amphibian() Creating Characteristic Croaks Creating Description Eats Bugs Frog() Bye! Frog dispose finalizing Description Eats Bugs finalizing Characteristic Croaks Amphibian dispose finalizing Description Both water and land finalizing Characteristic can live in water Animal dispose finalizing Description Animal not Vegetable finalizing Characteristic has heart LivingCreature dispose finalizing Description Basic Living Creature finalizing Characteristic is alive
Combining composition and inheritance
class A {
A(int i) {
System.out.println("A constructor");
}
}
class SubA extends A {
SubA(int i) {
super(i);
System.out.println("SubA constructor");
}
}
class B {
B(int i) {
System.out.println("B constructor");
}
}
class SubB1 extends B {
SubB1(int i) {
super(i);
System.out.println("SubB1 constructor");
}
}
class SubB2 extends B {
SubB2(int i) {
super(i);
System.out.println("SubB2 constructor");
}
}
class SubB3 extends B {
SubB3(int i) {
super(i);
System.out.println("SubB3 constructor");
}
}
// A cultural way of doing something:
class C {
C(int i) {
System.out.println("C constructor");
}
}
class SubC extends C {
private SubB1 subB1;
private SubB2 subB2;
private SubB3 subB3;
private SubA subA;
public SubC(int i) {
super(i + 1);
subB1 = new SubB1(i + 2);
subB2 = new SubB2(i + 3);
subB3 = new SubB3(i + 4);
subA = new SubA(i + 5);
System.out.println("SubC constructor");
}
}
public class MainClass{
public static void main(String[] args) {
SubC x = new SubC(9);
}
}
C constructor B constructor SubB1 constructor B constructor SubB2 constructor B constructor SubB3 constructor A constructor SubA constructor SubC constructor
Creating a Multilevel Hierarchy
class Box {
private double width;
private double height;
private double depth;
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}
class BoxWeight extends Box {
double weight; // weight of box
BoxWeight(BoxWeight ob) { // pass object to constructor
super(ob);
weight = ob.weight;
}
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
BoxWeight() {
super();
weight = -1;
}
BoxWeight(double len, double m) {
super(len);
weight = m;
}
}
class Shipment extends BoxWeight {
double cost;
Shipment(Shipment ob) { // pass object to constructor
super(ob);
cost = ob.cost;
}
Shipment(double w, double h, double d, double m, double c) {
super(w, h, d, m); // call superclass constructor
cost = c;
}
Shipment() {
super();
cost = -1;
}
Shipment(double len, double m, double c) {
super(len, m);
cost = c;
}
}
class DemoShipment {
public static void main(String args[]) {
Shipment shipment1 = new Shipment(10, 20, 15, 10, 3.41);
Shipment shipment2 = new Shipment(2, 3, 4, 0.76, 1.28);
double vol;
vol = shipment1.volume();
System.out.println("Volume of shipment1 is " + vol);
System.out.println("Weight of shipment1 is " + shipment1.weight);
System.out.println("Shipping cost: $" + shipment1.cost);
System.out.println();
vol = shipment2.volume();
System.out.println("Volume of shipment2 is " + vol);
System.out.println("Weight of shipment2 is " + shipment2.weight);
System.out.println("Shipping cost: $" + shipment2.cost);
}
}
Demonstrate when constructors are called in a Multilevel Hierarchy
class A {
A() {
System.out.println("Inside A"s constructor.");
}
}
class B extends A {
B() {
System.out.println("Inside B"s constructor.");
}
}
class C extends B {
C() {
System.out.println("Inside C"s constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C();
}
}
Derived Class Constructors: Calling the Base Class Constructor
class Animal {
public Animal(String aType) {
type = new String(aType);
}
public String toString() {
return "This is a " + type;
}
private String type;
}
class Dog extends Animal {
public Dog(String aName) {
super("Dog");
name = aName;
breed = "Unknown";
}
public Dog(String aName, String aBreed) {
super("Dog");
name = aName;
breed = aBreed;
}
private String name;
private String breed;
}
Deriving a Class
class Animal {
public Animal(String aType) {
type = aType;
}
public String toString() {
return "This is a " + type;
}
private String type;
}
class Dog extends Animal {
public Dog(String name){
super(name);
}
private String breed;
}
Inheritance
- You extend a class by creating a new class.
- The former and the latter will then have a parent-child relationship.
- The original class is the parent class or the base class or the superclass.
- The new class is called a child class or a subclass or a derived class of the parent.
- The process of extending a class in object-oriented programming is called inheritance.
- In a subclass you can add new methods and new fields as well as override existing methods in the parent class to change their behaviors.
- Inheritance gives you the opportunity to add some functionality that does not exist in the original class.
- Inheritance can also change the behaviors of the existing class to better suit your needs.
- The subclass and the superclass has an "is-a" relationship.
Inheritance and upcasting.
class A {
public void play() {
}
static void tune(A i) {
i.play();
}
}
// Wind objects are instruments
// because they have the same interface:
class B extends A {
}
public class MainClass {
public static void main(String[] args) {
B flute = new B();
A.tune(flute); // Upcasting
}
}
Inheritance, constructors and arguments
class A {
A(int i) {
System.out.println("A constructor");
}
}
class B extends A {
B(int i) {
super(i);
System.out.println("B constructor");
}
}
class C extends B {
C() {
super(11);
System.out.println("C constructor");
}
}
public class MainClass {
public static void main(String[] args) {
C x = new C();
}
}
A constructor B constructor C constructor
Method Overriding
- When you extends a class, you can change the behavior of a method in the parent class.
- This is called method overriding.
- This happens when you write in a subclass a method that has the same signature as a method in the parent class.
- If only the name is the same but the list of arguments is not, then it is method overloading.
Overloading a base-class method name in a derived class does not hide the base-class versions.
class A {
char doh(char c) {
System.out.println("doh(char)");
return "d";
}
float doh(float f) {
System.out.println("doh(float)");
return 1.0f;
}
}
class B {}
class C extends A {
void doh(B m) {
System.out.println("doh(B)");
}
}
public class MainClass {
public static void main(String[] args) {
C b = new C();
b.doh(1);
b.doh("x");
b.doh(1.0f);
b.doh(new B());
}
}
doh(float) doh(char) doh(float) doh(B)
Overriding a Base Class Method
class Animal {
public Animal(String aType) {
type = new String(aType);
}
public String toString() {
return "This is a " + type;
}
private String type;
}
class Dog extends Animal {
public Dog(String aName) {
super("Dog");
name = aName;
breed = "Unknown";
}
public Dog(String aName, String aBreed) {
super("Dog");
name = aName;
breed = aBreed;
}
public String toString() {
return "It"s " + name + " the " + breed;
}
private String name;
private String breed;
}
The extends Keyword
You extend a class by using the extends keyword in a class declaration,
The Parent class
public class Parent {
}
The keyword super represents an instance of the direct superclass of the current object.
- You can explicitly call the parent"s constructor from a subclass"s constructor by using the super keyword.
- "super" must be the first statement in the constructor.
class Parent {
public Parent(){
}
}
public class Child extends Parent {
public Child () {
super();
}
}
Type Casting
- With objects, you can cast an instance of a subclass to its parent class.
- Casting an object to a parent class is called upcasting.
Child child = new Child ();
Parent parent = child;