A strange question...
It's sort of an odd concept really, as you'll often here of gravity being solely related to mass, but there's a few situations out there in the universe that can be quite difficult to understand if you fail to consider the density of the object at question... Or at least I found them confusing!
Don't get me wrong, the force of gravity felt between two objects is and only is dependant on the mass of the two objects and the distance between them, as described by Newton's law of gravitation.
However, since density is a measure of mass per unit volume, and the volume of the two objects essentially effects the distance that the two can be away from each other, density really does matter.
To help understand this, let's consider the following thought experiment, which is a conundrum that I remember finding myself in when I first started learning about astronomy/physics.
"If a black hole forms from the collapse of a massive star, then how come it's gravity is strong enough to trap light, when the gravity of the star that it came from wasn't?!"
In fact, a black hole would have significantly less mass than the star that it came from, as a lot of mass would've inevitably been lost during the supernova/hypernova, as matter is blown away into the cosmos!
How on Earth (or a black hole) could the black hole be trapping light and beholding all sorts of these characterises, such as insane tidal forces and spaghetiffication (what in the....), when its gravitational "strength" should be less than the star that it came from?
As you may have guessed from the title already, it's because of density.
Taking 2 planets with different densities as an example
If you take the Earth and crush it down to the size of a tennis ball, with no loss or gain in mass, then it's true that they both have the same gravitational field strength, or more technically speaking, they both curve space-time to the same degree (if that sounds confusing, then I'd suggest giving my articles on relativity here a read).
But we're not considering something here....
Gravity can be thought of as deriving from an objects centre of mass, as opposed to the entire mass that's distributed over the volume of that object.
Using this, we can see that we would only be able to get so close to the Earth's centre of mass before the surface physically prevents us from getting any closer - there's a whole structure of molten metal and rock that lies between us at the surface, and the core.
Ultimately, there's thousands of kilometres worth of material that's separating us from the centre of mass, and we know that the further away you are from the centre of mass of an object, the weaker the acceleration that you feel due to that object's gravity.
Isaac Newton's law of Gravitation
This is simply thanks to Isaac Newton's law of Gravitation, which states that:
"Every particle attracts every other particle in the universe with a force that is proportional to the product of their masses and inversely proportional to the square of the distance between their centers."
The first line is straightforward to understand - the more massive an object is (an object contains many particles), the larger the force of which it attracts everything around it.
Hence why we orbit around the Sun and not the other way around - the Sun is over 333,000 times as massive as Earth, hence it attracts the Earth a lot more than the Earth attracts it.
It's the second line though that is of paramount importance to us when we're trying to understand how density affects gravity.
The phrase, "inversely proportional to the square of..." simply means that when the distance between the centres of the objects at question is increased, then the "force" of gravity felt by those objects is decreased by a factor of the square of that increase.
For example, if you double the distance, then you don't just halve the "force" of gravity, rather you divide it by 4 (2 ^2).
Notice that it refers to their centres, as we have been talking about thus far - that's an important part to remember.
Now, think back to the example with the 2 planets.
1 is the size of a tennis ball
1 is the same size as the Earth
Both have the exact same mass
You could get literally centimetres away from the centre of mass of the tennis ball, but you could only get 10 thousand or so kilometres away from the centre of mass of the Earth, before some matter is behind you and essentially pulling you back (assuming you could seamlessly move through the Earth)!
Therefore, you would feel a much stronger force of attraction to the tennis ball, by virtue of you being able to get so much closer to it.
This is the exact reason why black holes have these funky effects like trapping light and spaghetiffying objects that come close enough (click here if you have no idea what that is!).
Even though they'll often have a smaller mass than even large stars, they're so incredibly dense that they pack all that mass into a tiny, and I mean tiny space!
Think of the Sun being crushed down to the size of London.
Therefore, the light that's radiating from the surface gets so incredibly close to the black hole's centre of mass that its acceleration due to the gravity of the black hole is insanely high - not even light travelling at 300,000,000 meters per second can escape!
Why black holes are just that - black
And that it is precisely why back holes are black - the light from them can't escape the gravity of the black hole, so they never reach our eyes and we just see pure blackness in the absence of any light.
So hopefully you understand now how, in reality, density should be considered when thinking about the force of gravity felt between two particles.
Even though it's completely accounted for in Newton's law of Gravitation, where it refers to the distance between the objects, it's easy to forget that by reducing the volume of an object and hence increasing its density, you're able to decrease the distance between the 2 objects.
That's exactly how density relates to gravity - density reduces volume, which essentially allows particles to physically get closer to the objects centre of mass.
This concept explains so many processes in the universe, not just with black holes, but so many others, such as how white dwarfs can pull matter from their neighbouring star(s) even though they're smaller in mass.
When a white dwarf in a binary (or more) star system gets close enough to a companion star, the outer layers of that star could potentially be closer to the centre of mass of the white dwarf than to the centre of mass of the star itself, therefore it feels an overwhelming acceleration towards the white dwarf.
Interestingly, when white dwarfs pull enough matter away from companion stars, their own mass can exceed what's known as the Chandrasekhar limit, meaning that it's own gravity overwhelms the degeneracy pressure of electrons that's keeping the white dwarf from collapsing.
When this happens, the electrons neutralise with protons to form a body that's exclusively made of neutrons - otherwise known as a neutron star.
But that's a topic for a different day!
You can check it out here though.
Thanks for reading!