Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

java - What is subclassing?

I am new to java and I am trying to create an XML document and clone a specific node (minus the textnode) of this document over and over again. Someone answered me and said that I should subclass the node and override the cloning. So my question is what is sub-classing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Subclassing means to define a new class that has the properties of an old class (the "superclass") with some changes.

In this case, your original responder is saying something like this:

Say you have a base class Base which has a method getTwo like so:

class Base {
   public int getTwo(){ return 2;}
}

You decide you want a new class that still have a method getTwo but that returns the string "two" instead of the number 2. You could define it as

class Subclass extends Base {
   public String getTwo() { return "two"; }
}

We say Subclass is a subclass of -- or more commonly, "is a kind of" -- Base.

Beyond that, you'd be best off to read a book on object-oriented programming with Java. I'm fond of Thinking in Java, which has the added advantage that it's available freely on line.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...