Dinkar Joshi

Greenhorn
+ Follow
since Jan 02, 2020
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
10
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Dinkar Joshi

Hello Everyone,
I am in the process of understanding the Visitor design pattern. My understanding is that somewhere this pattern creates a monolithic class with various functions.
If even 1 of the functions changes, then either a new class has to be created (just to change a function and copying the rest of the functions as it is) or one can edit the current function thereby violating open/close principle.

Let me share an example.



below is one of the classes that implement the above interface



Now suppose Necessity's implementation changes, either one has to create another implementation, copying the rest of the code, to just change Necessity or one has to manually change the visit method (used by Necessity), thereby violating open/close principle.

Is this violating the interface segregation principle?  Can we improve or change anything in this? Thanks in advance.

Paul Clapham wrote:Have a look at the Java API for a start. On the page for FilenameFilter (and in fact on every class and interface's page), at the top, there's a "USE" link. That will show you everything in the standard API which uses FilenameFilter. If you really feel keen, you can then look in the source code for the standard API and see how people have used that method.



Thank You for the tip. I shall.

Paul Clapham wrote:

Dinkar Joshi wrote:Since accept is an instance method called (indirectly, through FilenameFilter reference) by a File object, what is the use of File object calling a method and passing itself to a method?



First of all, the code inside the FilenameFilter might well need to know about the directory in question before it can say yes or no. So the most straightforward way to arrange that is to pass the directory as a parameter.

And second, I don't believe you have looked at all other places where a FilenameFilter is used in the standard API.




Thank You. Your reasoning makes sense, I think I need to read and find more examples of accept() usage, hope, I would stumble upon the File object usage in those. Thanks again.
I am learning File Handling in java and came across the File class.

Statement:
The File class has a method called list().
One of the overloaded methods called list takes in FilenameFilter Reference Object.

The interface FilenameFilter has a method called accept, with the signature - >  boolean accept(File dir, String name).

Question:
Since accept is an instance method called (indirectly, through FilenameFilter reference) by a File object, what is the use of File object calling a method and passing itself to a method? I see no obvious use of how this would help to filter files?

example:



The above code makes no use of File object in the accept method, especially when the File object is the same object which called the method in the first place. Can anyone suggest use or if I am missing any deeper concept. As accept method can do just fine without a File object reference.
Thanks in advance.
I doubt, since wikipedia also has the same Subject layout

Paul Clapham wrote:

Junilu Lacar wrote:If you look at the example implementations on the Wikipedia article, you'll see the notify method is private, which it should be.



That's true. But the Java example there doesn't have a "Source" interface to be implemented. If it did, then it couldn't have a private "notify" method.

So yes, that "Source" interface is problematic.



Also the UML in the wiki page UML shows the Subject  to have a public method notifyObservers(). Since it has a + sign to it.
Thank you Paul, just don't understand why would such a famous book, include this example. I have just started , may be further down the book, there is some clarification or a better design pattern.
Thank you, for the formatting and the answer as well. It explains the doubts.
4 years ago
I am trying to understand the design patterns reading HeadFirst Design Pattern. I have a doubt while trying to understand Observer pattern.

The Subject interface:


The Observer interface:



Statement:
Any Observer implementing class would need to be passed the reference of the Subject interface, in order for it to be added/removed as an observer in the list of the Subject, or more importantly to be notified for the update.

Question:
Now since the observer has the reference to the class implementing Subject interface, the observer can wrongfully call the notifyobservers() method and all the observers will be notified wrongly.

Can anyone help if my understanding is correct or wrong? If correct why such an implementation. thanks in advance.
I have 2 questions, but both seem to be related.

1) Why java does not allow static methods to be overridden or hidden by inheriting class's instance method? Below is not allowed why?




2) With whatever the reasoning behind the answer to Q1 is, with the same reason why is this allowed?





Q1, static method is not being overridden not hidden and in Q2 static variable is overridden/hidden.

Thanks in advance.
4 years ago

Carey Brown wrote:



Thank you. It works.
4 years ago

Consider the below code



Problem Statement:

its possible to post increment the class variable x from method m1, however is it possible to pre increment also? if yes, then how?
4 years ago
From java11 files can be compiled and run from the same line. Below is the code and I shall ask the question after the code.

in a file named filename.java


class filename{
public static void main(String[] a){
System.out.println("From filename main method");
}
}

public class ClassName{
public static void main(String[] a){
System.out.println("From First main method");
}
}




Observe below commands:

Command 1:
C:\javaDJ>java filename.java
From filename main method

Command 2:
C:\javaDJ>javac filename.java
filename.java:7: error: class ClassName is public, should be declared in a file named ClassName.java
public class ClassName{
      ^
1 error

Observation:

command 1 compiles (i assume internally ) and executes successfully.
command 2 throws compilation error.

Problem Statement :

How is java cmd able to compile the file called filename.java, when the file(filename.java) contains a public class (ClassName)which is not named 'filename.java'  (the name of the file-name.) ?



4 years ago
Chapter 4: point 4 under for loop.
4. Using Incompatible Data Types in the Initialization Block

Like the third example, this code will not compile, although this time for a different reason. The variables in the initialization block must all be of the same type. In the multiple terms example, y and z were both long, so the code compiled without issue, but in this example they have differing types, so the code will not compile.

The above statement suggests that the below code should also not compile, whereas it does:


It's just my suggestion, as I was confused when I was trying various permutations myself as to understand the for loop better.
4 years ago
I have tried again, please find below:

C:\Users\DJ\OCP>jar -cvf myNewFile.jar C:\Users\DJ\OCP
added manifest
adding: Users/DJ/OCP/(in = 0) (out= 0)(stored 0%)
adding: Users/DJ/OCP/OCP1/(in = 0) (out= 0)(stored 0%)
adding: Users/DJ/OCP/OCP1/pack1/(in = 0) (out= 0)(stored 0%)
adding: Users/DJ/OCP/OCP1/pack1/Classa.class(in = 192) (out= 161)(deflated 16%)
adding: Users/DJ/OCP/pack1/(in = 0) (out= 0)(stored 0%)
adding: Users/DJ/OCP/pack1/Classa.java(in = 46) (out= 44)(deflated 4%)
adding: Users/DJ/OCP/pack2/(in = 0) (out= 0)(stored 0%)
adding: Users/DJ/OCP/pack2/ClassB.java(in = 165) (out= 130)(deflated 21%)
adding: Users/DJ/OCP/thi.jar(in = 140885) (out= 140834)(deflated 0%)
adding: Users/DJ/OCP/Zoo.java(in = 410) (out= 216)(deflated 47%)

C:\Users\DJ\OCP>