In every book and website I look at it says for guitar that sharps and flats are on the same fret for example C sharp and D flat are both on the forth fret of the a string and I've seen that it is similar for piano. However I saw on a violin web page that it can play a flat and G sharp as separate notes and the a flat is on the G string so why are violins able to separate the notes but not guitars and pianos? that's assuming the information is OK. Also why are the sharps and flats sometimes identified even though they are using the same fret or piano key? for example on sheet music and chord Books.
I suggest reading this answer first (where I derive the stuff I talk about here with some extra graphics).
Western music is derived from diatonic scales, like the one created by all the white keys on piano. Not sharps and flats at all! Such diatonic scales are based on just intonation, i.e. intervals that sound good because of objective physical reasons, namely a low integer frequency ratio, which causes some overtones to match up. To be precise: Western diatonic scales are originally Pythagorean, but by the time of Renaissance the thirds were made mostly Ptolemaic. This is still the scale that's naturally used by singers and string instruments when playing in harmony (especially at long major chords, where there's time to really eliminate all impurities (beat) from the sound).
The ratios of a Ptolemaic scale in C are as follows (frequency in Hertz and ratio to tonic):
264 297 330 352 396 440 495 528 594 660 704 792 1/1 9/8 5/4 4/3 3/2 5/3 15/8 2/1 9/4 5/2 8/3 3/1 C D E F G A B C D E F G
Again note there are no real semitones (as in, halves of whole tones) here – most of the intervals are different from each other, for instance the interval from D to E is a bit smaller than the one from C to D!
Nevertheless, there is some symmetry: if we start another diatonic scale from the F, we notice that most of the notes reoccur:
264 297 330 352 396 440 495 528 594 660 704 792 C D E F G A B C D E F G 352 396 440 469⅓ 528 586⅔ 660 704 792 1/1 9/8 5/4 4/3 3/2 5/3 15/8 2/1 9/4 F G A B♭ C D E F G
There are only two discrepancies:
- The fourth note with 469⅓ Hz is very notably lower than the one that would come there in the C-based scale. These really can't be replaced for one another, hence this note is always given a different name: B♭ instead of B.
- The sixth note is also a bit lower than the ninth in the C scale. Is this important? Well, our ears can't measure pitch with infinite precision. Players and singers always stray somewhat away from the “ideal” pitch anyway. To most listeners, it would not be quite obvious what's wrong if you replace 594 Hz with 587 Hz, though in some chords it will definitely cause a bit of an impure sound.
This kind of difference between two notes in different just-intonation scales is essentially also what's different between e.g. C♯ and D♭, just there it's even stronger yet harder to notice because you need to modulate across a whole bunch of different keys in the circle of fifths.
The idea behind tempered tunings, in particular the 12-edo scale that you find on modern pianos, is to simply disregard these small differences and use a compromise somewhere in between, which will work for all scales equally well (though not quite perfectly for any of them). For comparison, the two Ptolemaic scales and the equally-spaced 12-edo scale (rounded to integer Hertz frequency) between them:
264 297 330 352 396 440 495 528 594 660 704 792 263 278 295 313 331 351 372 394 417 442 468 496 526 557 590 625 662 702 743 788 352 396 440 469⅓ 528 586⅔ 660 704 792 C D E F G A B C D E F G
As you see, this matches the just-intonation scales pretty well on most tones, only on the thirds of each scale (E and A) are a bit higher. Indeed that's the biggest problem with the 12-edo scale: major thirds are sharp, sounding a bit restless. This is especially notable when playing chords on distorted guitar, which in turn is a reason why rock guitar tends to prefer playing only powerchords (pure fifths): fifths don't have that deficiency in 12-edo!
So, to answer the question: why are violins able to separate the notes but not guitars and pianos – well pianos would ideally be able to separate them too, but for practical reasons they are designed so these enharmonically equivalent notes are actually played on the same key.
Violins are fretless instruments, whereas guitars have frets. This means that the placement of the guitar fret determines the placement of this A♭/G♯. A violinist, however, can move their finger ever so slightly in either direction to adjust the tuning of the A♭/G♯ as they see fit in a given musical environment.
Although A♭/G♯ are the same in an abstract sense (and from a standpoint of what we call twelve-tone equal temperament), the pitches can be different based on what's happening in the music. The most obvious example is from a standpoint of just intonation, where a G♯ in an E major chord will need to be lowered significantly, whereas an A♭ in an A♭ major chord will (can) actually be tuned higher than the G♯!
Another factor that applies to the violin is the "standard" way that the fingers of the left hand are used to stop the strings. Because of the (relatively small) size of the instrument, the most natural way is to use successive fingers for successive notes of a major or minor scale, without needing to move your hand position.
So if you are playing in A flat major, the notes on the D string would be fingered
1 - E flat 2 - F natural 3 - G natural 4 - A flat
(1 is your index finger, 4 your pinky). But in A major, you would use
1 - E natural 2 - F sharp 3 - G sharp 4 - A natural (if you don't want to use the open A string)
So in that sense G sharp and A flat are "different notes" for a violinist because they are played with different fingers, even if they are at exactly the same pitch.
The strings on a guitar are nearly twice as long as the violin, so keeping your hand in a fixed position on the neck of a guitar while playing different notes is less practical. So guitarists tend to think of a which fret corresponds to each note, and the left hand finger they use for that fret depends on the notes that precede or follow it.
As Richard says, because the violin is fretless, you can slightly alter the pitch of every note to create the temperament that you want. I might have two songs containing 'B', and in one case I might pitch the 'B' a bit higher- even when the notes are named exactly the same, the same chromatic note might have different pitches.
So when your source says...
I saw on a violin web page that it can play A flat and G sharp as separate notes
...I think that's potentially misleading, because the fact that the notes are named differently is not itself a definitive reason to play the same chromatic note at different pitches.
It would imply that you were playing in a different key though, so if you were not trying to play in equal temperament, a difference in pitch would be likely. So it's not an incorrect statement, but it is conflating two different concepts.
All disciplines have their own Vocabulary, 'Music Theory' also has words to explain their idiosyncrasies.
The 'Concept you describe' in 'basic music theory' is called: En-harmonic! Two names for one musical tone.
The C Major Chromatic Scale is, C,C#,D,D#,E,F,F#,G,G#,A,A#,B... * ( 12 semi tones )*. Sharps are added in Ascending Order.
Flats are used in descending order, C,B,Bb,A,Ab,G,Gb,F,E,Eb,D,Db. No ( flats or #'s ) between B&C / E&F ). ( C# is en-harmonic to Db. G# is en-harmonic to Ab,... same tone, different name ).