isam alie

Greenhorn
+ Follow
since Jun 11, 2021
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
6
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by isam alie

"Class abstraction separates class implementation from class use, by encapsulating the implementation details of the class,
and providing an interface to interact with/use the encapsulated class."  
2 years ago

Junilu Lacar wrote:Yes, you got it. Don't overthink it. They are both related but they're different perspectives.

Think of it like this: If you are a cashier at a store, abstraction is like you thinking about the customer paying for a purchase. That's the main behavior you care about. Implementation is whether the payment is done with cash, card, or check.

On the other hand, encapsulation is about the Customer knowing how much money they have in their bank account, how much cash they have in their wallet, or how much more they can charge before they reach their credit limit. To you, the cashier, that is information you are not privvy to,  i.e. the information is hidden from you. Any attempt by you to obtain that information is inappropriate, i.e. you would be violating your customer's privacy or in ther words, you're breaking their encapsulation.

Does that make sense?



Thank you for the example.

Here is an example I came up with after doing a little more search.


Say you have a RAM class and a HDD class, the RAM uses the HDD to read and write files. If the HDD class was "well encapsulated" (the implementation details of how the HDD stores files and searches for them are hidden) then the RAM just use two methods, one to write to the HDD (say hdd1.writeFile(File f)) and another to read from (say hdd1.readFile(File f)) and that achieves the concept of abstraction, because the RAM class is not concerned with how the files are stored within the HDD, it just reads and writes to it using two methods (through the class interface), so the the implementation of HDD class is separated from the use of it.

However, if the HDD was not "well encapsulated" (its implementation details are visible to the client/user classes), then the RAM class will do something like this to read a file from the HDD :



In the preceding code we see that the implementation details of the HDD class are exposed (for example we know that the HDD class stores the files in an array), thus the concept of encapsulation is not fulfilled.
Also, we see that the use of the HDD class is not separated from the its implementation (when we use it we actually think of how its implemented in order to read a file), thus the concept of abstraction is not fulfilled, as well.

So, what we can conclude is that encapsulation is a requirement for abstraction. If there were no encapsulation (the encapsulation is violated), there would be no abstraction.

I hope you validate me on my understanding.  

Thank you once again.


2 years ago
from*

And there is information hiding (It's claimed to be the same as encapsulation)
2 years ago
As far as I understand, class abstraction is the separation of class implementation for class use.
and class encapsulation is the hiding of the details of the class implementation.

For example, say we have a Car class, the abstraction part will be that I do not think about how the Car class is implemented (its data fields and methods) when I use it. While
the encapsulation part would be that placing everything related to a car in the Car class, and then providing a class interface for the user to use the class.

Is my understanding the class abstraction and class encapsulation correct ? I feel they are kinda of the same. Also I've read that abstraction is dependent on encapsulation (the abstraction is achieved through encapsulation)

I dunno this seems to be a bit complicated.
2 years ago

Junilu Lacar wrote:Please quote your sources so we know where you're getting your information.




From Introduction to Java Programming and Data Structures, Comprehensive Version by Y. Daniel Liang :

Java provides a system-independent encapsulation of date and time in the java.util.Date class
2 years ago
How came the Date class is considered system-independent and it extracts the time for the operating system clock ?

Shouldn't be system dependent ?

I know it's deprecated and should not be used. But that is the question anyway.
2 years ago

Jesse Silverman wrote:

isam alie wrote:

Junilu Lacar wrote:

EDIT -- to be more explicit -- so, sure, it will still be anonymous, but that does NOT mean it is garbage collected after, as the method you passed your "one off" anonymous instance to can pretty easily squirrel it away somewhere for later enjoyment.





Why would it be anonymous if the method stored the object reference say in an ArrayList ? then the passed Circle Object will be accessible via an ArrayList indexed variable.  

2 years ago

Junilu Lacar wrote:
however, once you dig down into the Circle.equals() method and follow the path the reference takes, it will no longer be anonymous because the object reference will be assigned to a parameter which of course has a name.




But it will be nameless (anonymous) once again once the equals method is finished (as it will be popped from the call stack) and returns control back the the main method. Won't it ?
2 years ago

Jesse Silverman wrote:

I suspect this is a case of a multi-lingual author borrowing a phrase commonly used in one or more other languages and applying it to Java.



You are right. Laing has a C++ book. He does mention anonymous objects in it as follows :

Usually you create a named object and later access its members through its name. Occasionally you may create an object and use it only once. In this case, you don’t have to name it.
Such objects are called anonymous objects.
2 years ago

Junilu Lacar wrote:As far as I can tell, the Java Tutorials and the Java Language Specification only reference "anonymous classes"—with a quick Google search, I don't see any links to trusted Java reference materials that refer to "anonymous object."

@OP: please cite your sources where you found the term "anonymous object."



From: Introduction to Java Programming and Data Structures, Comprehensive Version by Y. Daniel Liang

Usually you create an object and assign it to a variable, then later you can use the variable
to reference the object. Occasionally, an object does not need to be referenced later. In this
case, you can create an object without explicitly assigning it to a variable using the syntax:
new Circle();
or
System.out.println("Area is " + new Circle(5).getArea());
The former statement creates a Circle object. The latter creates a Circle object and
invokes its getArea method to return its area. An object created in this way is known
as an anonymous object.

Also : printArray(new int[]{3, 1, 2, 6, 4, 2});
The preceding statement creates an array using the following syntax:
new elementType[]{value0, value1, ..., valuek};
There is no explicit reference variable for the array. Such array is called an anonymous
array.

Also mentioned in the book that an object that is not referenced by an reference variable is known as "garbage". So is the term "garbage" would be better to use instead of anonymous object to describe an object that is not referenced by any reference variable to not confuse anonymous objects with anonymous classes ?
2 years ago

Campbell Ritchie wrote:Are we confusing anonyous classes and anonymous objects, the latter being a poor term which should never have been coined?



I am referring to anonymous objects here.

To my understanding: an anonymous is an object that its reference has not been assigned explicitly ( using an direct assignment statement) to a reference variable. But its reference can be assigned implicitly (by passing it to a method parameter).
Or for an object to be anonymous it has be nameless (no reference variable refers to it)

which one is correct ?

And am I understanding right ? or Am I missing something ?  
2 years ago
My question is : Is the object created in line 13 an anonymous object ? if so why ?
2 years ago
I have a question concerning anonymous object in Java.

Is an anonymous object an object that has no explicit nor implicit one. Or is it an object that has no explicit reference but it can have an implicit one (e.g a method paramter)

The following example to illustrates what I mean ::

2 years ago

Campbell Ritchie wrote:Please explain what the algorithm is. You have so‑called arrow head code in lines 28‑49. That sort of code would need an explanation about how it works.



I did. Thank you for the suggestion.
2 years ago
Hi there!
I have solved this problem on the book Introduction to Java Programming and Data Structures, Comprehensive Version by Y. Daniel Liang, but I'm not sure whether the solution is correct.

Here is the problem :

(Largest block) Given a square matrix with the elements 0 or 1, write a program to
find a maximum square submatrix whose elements are all 1s. Your program should
prompt the user to enter the number of rows in the matrix. The program then displays the location of the first element in the maximum square submatrix and the
number of rows in the submatrix. Here is a sample run:

Enter the number of rows in the matrix: 5
Enter the matrix row by row:
1 0 1 0 1
1 1 1 0 1
1 0 1 1 1
1 0 1 1 1
1 0 1 1 1
The maximum square submatrix is at (2, 2) with size 3

Your program should implement and use the following method to find the maximum square submatrix:
public static int[] findLargestBlock(int[][] m)
The return value is an array that consists of three values. The first two values are
the row and column indices for the first element in the submatrix, and the third
value is the number of the rows in the submatrix. For an animation of this problem,
see https://liveexample.pearsoncmg.com/dsanimation/LargestBlockeBook.html.

Here is the algorithm for approaching the problem :


1. Check all possible square sizes from largest (m.length by m.length) to smallest (1 by 1).

2. Check each element in the matrix, if it were appropriate to go for size by size square block, (That's done by checking if the matrix.length - element's row is greater or equal to size and matrix.length - element's column is greater or equal to size).

3. If that was the case (we were able to go for size by size square block starting from the current element), we go for it.

4. We suppose that all the elements in the square block are all ones.

5. We iterate over the square block starting from the current element.

6. If all the elements in the square block are all ones, we return the row and column of the starting element in the square block, and the size of the square block.

7. If that wasn't the case (at least one element is not one in the square block) we break out and go for the next element in the array and check whether it is appropriate to go starting from it to a size by size square block (we go back to step 2)

8. If there were not any square blocks with all ones (the matrix is all zeroes) we return 0.

Here is the code::



Any suggestions and remarks  would be really appreciated.
2 years ago