Note that there are some explanatory texts on larger screens.

plurals
  1. POJava API Example for abstract class. unable to understand the concept properly so reposted
    primarykey
    data
    text
    <p>I am just trying myself to be comfortable with java api. But I am unable to do it. Not knowing the purpose of when to use abstract class.</p> <p>Consider the java library collections</p> <pre><code> Collections List Set </code></pre> <p>All 3 are interfaces but the major thing , though they have functions which are common that is add , addAll, contains,containsAll etc. List implements in its own and set implements in another way ( telling about duplication ). Set does not allow duplicates list allows duplicates. </p> <p>So what I am trying to say is when ever there is no common features for sub-entity with super-entity we have to go for interface</p> <p>To still more make it clear , if suppose we make Collection as abstract class we need some methods to be properly defined rather than just declared but the functionality of list and set is differnt ( no common definition to bothe) hence they just declared it as interface and made a force rule that both list and set should implement this.</p> <p>But if there is some common functionality which both has then keep it as to be abstract class. </p> <p>So to get an example from Java itself. Lets consider Number class which is made ABSTRACT. Lets see why it could not be done using interface now!!!</p> <pre><code>**public abstract class Number extends Object methods are** </code></pre> <p><strong><code>byte byteValue()</code></strong> Returns the value of the specified number as a byte.</p> <p><strong><code>abstract double doubleValue()</code></strong> Returns the value of the specified number as a double.</p> <p><strong><code>abstract float floatValue()</code></strong> Returns the value of the specified number as a float.</p> <p><strong><code>abstract int intValue()</code></strong> Returns the value of the specified number as an int.</p> <p><strong><code>abstract long longValue()</code></strong> Returns the value of the specified number as a long.</p> <p><strong><code>short shortValue()</code></strong> Returns the value of the specified number as a short.</p> <p>Now here the byteValue is a non-abstract method and other methods XXXValue are abstract. We will see Y???</p> <p>byteValue()</p> <pre><code>// taken from Number class java doc api public byte byteValue() </code></pre> <p>Returns the value of the specified number as a byte. This may involve rounding or truncation.</p> <pre><code>And this byteValue() method is being overriden by all the other classes like Integer,Double,Float etc. </code></pre> <p>Instead they can keep byteValue() also abstract !!! But they dint do it. Wish to know why they have done like this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload