473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Declaring Methods and classes final

momotaro
357 100+
can some one explain to me what are the adventages of such use?
I tried to understand what sun is posting on the tutorial but they don't explain that much

Plz help!
Jun 12 '08 #1
13 1525
JosAH
11,448 Expert 8TB
can some one explain to me what are the adventages of such use?
I tried to understand what sun is posting on the tutorial but they don't explain that much

Plz help!
Suppose I've made a class and I don't want anyone to override a method in an
extending class; I can make that method final. Suppose I don't want anyone to
extend their class from my class; I can make that entire class final.

kind regards,

Jos
Jun 12 '08 #2
momotaro
357 100+
in the java tutorial site they stated:
"Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results."
why methods called from constractors??
Jun 12 '08 #3
BigDaddyLH
1,216 Expert 1GB
why methods called from constractors??
Expand|Select|Wrap|Line Numbers
  1. public class Example {
  2.     private String text;
  3.  
  4.     public final String getText() {
  5.         return text;
  6.     }
  7.  
  8.     public final void setText(String text) {
  9.         this.text = (text == null) ? "" : text.trim();
  10.     }
  11.  
  12.     public Example() {
  13.     }
  14.  
  15.     public Example(String text) {
  16.         setText(text);
  17.     }
  18. }
Jun 12 '08 #4
BigDaddyLH
1,216 Expert 1GB
And here's an example of where logic may go awry when a polymorphic method is called from a constructor.

Expand|Select|Wrap|Line Numbers
  1. class Example {
  2.     protected String text;
  3.  
  4.     public String getText() {
  5.         return text;
  6.     }
  7.  
  8.     public void setText(String text) {
  9.         this.text = (text == null) ? "" : text.trim();
  10.     }
  11.  
  12.     public Example() {
  13.     }
  14.  
  15.     public Example(String text) {
  16.         setText(text);
  17.     }
  18. }
  19.  
  20. public class SubExample extends Example {
  21.     private boolean preserveNull;
  22.  
  23.     public SubExample(String text, boolean preserveNull) {
  24.         super(text);
  25.         this.preserveNull = preserveNull;
  26.     }
  27.  
  28.     @Override()
  29.     public void setText(String text) {
  30.         System.err.println("in setText, preserveNull is " + preserveNull);
  31.         if (preserveNull) {
  32.             this.text = (text == null) ? null : text.trim();
  33.         } else {
  34.             super.setText(text);
  35.         }
  36.     }
  37.  
  38.     public static void main(String[] args) {
  39.         Example app = new SubExample(null, false);
  40.         System.out.format("text=(%s)%n", app.getText()); //blank -- as expected
  41.  
  42.         Example app2 = new SubExample(null, true);
  43.         System.out.format("text=(%s)%n", app2.getText());//blank -- why not null?
  44.     }
  45.  
  46. }
Jun 12 '08 #5
momotaro
357 100+
Expand|Select|Wrap|Line Numbers
  1. public class Example {
  2.     private String text;
  3.  
  4.     public final String getText() {
  5.         return text;
  6.     }
  7.  
  8.     public final void setText(String text) {
  9.         this.text = (text == null) ? "" : text.trim();
  10.     }
  11.  
  12.     public Example() {
  13.     }
  14.  
  15.     public Example(String text) {
  16.         setText(text);
  17.     }
  18. }
Sorry but can you explain me your code?
this is my first week of java.
Jun 12 '08 #6
BigDaddyLH
1,216 Expert 1GB
Sorry but can you explain me your code?
this is my first week of java.
What don't you understand?
Jun 12 '08 #7
momotaro
357 100+
What don't you understand?
the purpose of that code
Jun 12 '08 #8
BigDaddyLH
1,216 Expert 1GB
the purpose of that code
It demonstrates why one might want to call a method from a constructor.
Jun 12 '08 #9
chaarmann
785 Expert 512MB
Please correct me if I am wrong, but this example demonstrates the use of a not-initialized variable instead of an awry logic or the question why a method called from a constructor should be final.
The question is why app2.getText() returns blank instead of null, and I think following happens:

By calling SubExample(null, true), the method-scope variable preserveNull is set to true, but the class variable preserveNull is still "undefined". That means by just declaring it with private boolean preserveNull; its value will be initialized to "false" automatically by java, that means it's the same as writing private boolean preserveNull = false;.
If super(text) is executed next, then setText(text) is executed in Example(String text) which executes the method setText() in Example and not in SubExample. (Correct me if I am wrong here). Then the statement if (preserveNull) is evaluated and here it always jumps into the else-clause because preserveNull is "undefined", that means false. Setting preserveNull afterwards when it returns from the super(text); therefore has no effect.

I have not yet tried to run the code example, but if you would put super(text); before this.preserveNull = preserveNull; everything would run as expected and not awry. Wait, is this allowed? I remember that super() must be the first command executed in a constructor, so exchanging the statements would not work. Hmmm...
Jun 13 '08 #10
JosAH
11,448 Expert 8TB
An object is constructed as an onion layer: first the inner layers must be there,
i.e. first the superclass object must be constructed before the outer layer (the
sub class object) can be set up and initialized. When you jump from an inner
layer to an outer layer during construction time of the inner layer itself you can
be sure that the outer layer hasn't been initialized yet.

Jumping from an inner layer to an outer layer during construction time happens
when the superclass constructor calls a method that is overridden by one in
that (not yet existing) outer layer (i.e. derived class). That's why it is advisable
to make those methods that are called by a constructor as private so that they
can't be overridden and so that you can't 'jump' to an outer layer.

kind regards,

Jos
Jun 13 '08 #11
momotaro
357 100+
josAH !! you rock!!!

this is the explanation that I was waiting for !!!
Jun 13 '08 #12
JosAH
11,448 Expert 8TB
josAH !! you rock!!!

this is the explanation that I was waiting for !!!
You're welcome of course; if you like the union layer analogy keep it in mind
when you design and implement your classes.

kind regards,

Jos
Jun 13 '08 #13
BigDaddyLH
1,216 Expert 1GB
Please correct me if I am wrong, but this example demonstrates the use of a not-initialized variable instead of an awry logic or the question why a method called from a constructor should be final.
The question is why app2.getText() returns blank instead of null, and I think following happens:

By calling SubExample(null, true), the method-scope variable preserveNull is set to true, but the class variable preserveNull is still "undefined". That means by just declaring it with private boolean preserveNull; its value will be initialized to "false" automatically by java, that means it's the same as writing private boolean preserveNull = false;.
If super(text) is executed next, then setText(text) is executed in Example(String text) which executes the method setText() in Example and not in SubExample. (Correct me if I am wrong here). Then the statement if (preserveNull) is evaluated and here it always jumps into the else-clause because preserveNull is "undefined", that means false. Setting preserveNull afterwards when it returns from the super(text); therefore has no effect.

I have not yet tried to run the code example, but if you would put super(text); before this.preserveNull = preserveNull; everything would run as expected and not awry. Wait, is this allowed? I remember that super() must be the first command executed in a constructor, so exchanging the statements would not work. Hmmm...
The OP actually asked for an explanation of the first, simpler, example.

And to "fix" the second example, I would do the following.

Expand|Select|Wrap|Line Numbers
  1. public SubExample(String text, boolean preserveNull) {
  2.     super();
  3.     this.preserveNull = preserveNull;
  4.     setText(text);
  5. }
Jun 13 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
11
by: Dave Rudolf | last post by:
Hi all, Suppose that I have a class (let's call the class A) with a virtual method. I then define a subclass (B), which could potentially be extended by some other subclass (say, C). So, A <- B...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
7
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them...
7
by: Markus Svilans | last post by:
Hi, What is the difference between having a struct with constructors and methods versus a class? In many C++ examples online I have seen code similar to this: struct Animal { Animal()
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
8
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.