Java Methods Object-oriented Programming And Data Structures Pdf

0 views
Skip to first unread message

Leontina Heidgerken

unread,
Aug 4, 2024, 9:21:24 PM8/4/24
to flaksencoworl
Aqueue can also be implemented in a procedural language using a struct and a set of functions that operate on the struct. However, in a procedural language you can't make the members of a struct private. Were the members of a data structure implemented in a procedural language left public, or was there some trick to make them private?

OOP did not invent encapsulation and is not synonymous with encapsulation. Many OOP languages do not have C++/Java style access modifiers. Many non-OOP languages have various techniques available to offer encapsulation.


One classic approach for encapsulation is closures, as used in functional programming. This is significantly older than OOP but is in a way equivalent. E.g. in JavaScript we might create an object like this:


The C language supports some kinds of encapsulation through its header file mechanism, particularly the opaque pointer technique. In C, it is possible to declare a struct name without defining its members. At that point no variable of the type of that struct can be used, but we can use pointers to that struct freely (because the size of a struct pointer is known at compile time). For example, consider this header file:


There is also the class of modular programming languages, which focuses on module-level interfaces. The ML language family incl. OCaml includes an interesting approach to modules called functors. OOP overshadowed and largely subsumed modular programming, yet many purported advantages of OOP are more about modularity than object orientation.


There's also the observation that classes in OOP languages like C++ or Java are often not used for objects (in the sense of entities that resolve operations through late binding/dynamic dispatch) but merely for abstract data types (where we define a public interface that hides internal implementation details). The paper On Understanding Data Abstraction, Revisited (Cook, 2009) discusses this difference in more detail.


But yes, many languages have no encapsulation mechanism whatsoever. In these languages, structure members are left public. At most, there would be a naming convention discouraging use. E.g. I think Pascal had no useful encapsulation mechanism.


Secondly, in "C" - which most people would call procedural, and not object oriented, there are lots of tricks you can use to effectively make things private. A very common one is to use opaque (e.g. void*) pointers. Or - you can forward declare an object, and just not define it in a header file.


"Opaque data types" was a well-known concept when I did my computer science degree 30 years ago. We did not cover OOP as it was not in common usage at the time and "functional programming" was considered to be more correct.


It has already been explained by Lewis Pringle how forward declaring a struct can be used in C. Unlike Module-2, a factory function had to be provided to create the object. (Virtual methods were also easy to implement in C by having the first member of a struct being a pointer to another struct that contained function pointers to the methods.)


Every large scale project I have worked on, (before I moved onto C++ then C#) had a system in place to prevent "private" data being accessed by the wrong code. It was just a little less standardized than it is now.


Note there are many OO languages without a built-in ability to mark members private. This can be done by convention, without a need for the compiler to enforce privacy. For example, people will often prefix private variables with an underscore.


There are techniques to make it harder to access "private" variables, the most common being the PIMPL idiom. This puts your private variables in a separate struct, with just a pointer allocated in your public header files. This means an extra dereference and a cast to get any private variables, something like ((private_impl)(obj->private))->actual_value, which gets annoying, so in practice is rarely used.


Data structures didn't have "members", only data fields (assuming it was a record type). Visibility was typically set for the entire type. However, that may not be as limiting as you think, because the functions weren't part of the record.


The dominant programming paradigm before OOP was called structured programming. The initial main goal of this was to avoid use of unstructured jump statements ("goto"s). This is a control flow oriented paradigm (while OOP is more data-oriented), but it was still a natural extension of it to attempt to keep data logically structured just like the code.


Another outshoot of structured programming was information hiding, the idea that the implementations of the code's structure (which is likely to change fairly often) should be kept separate from the interface (which ideally won't change nearly as much). It's dogma now, but in the olden days, many people actually considered it better for every developer to get to know the details of the entire system, so this was at one time actually a controversial idea. The original edition of Brook's The Mythical Man Month actually argued against information hiding.


Later programming languages designed explicitly to be good Structured Programming languages (for example, Modula-2 and Ada) generally included information hiding as a fundamental concept, built around some kind of concept of a cohesive facility of functions (and any types, constants, and objects they might required). In Modula-2 these were called "Modules", in Ada "Packages". A lot of modern OOP languages call the same concept "namespaces". These namespaces were the organizational foundation of development in these languages, and for most purposes could be used similarly to OOP classes (with no real support for inheritance, of course).


So in Modula-2 and Ada (83) you could declare any routine, type, constant, or object in a namespace private or public, but if you had a record type, there was no (easy) way to declare some record fields public and others private. Either your whole record is public, or it's not.


You can also have private and public functions on a module-to-module basis. Functions declared static in the source file are not visible to the exterior, even if you attempt to guess their name. Similarly, you can have static file-level global variables, which is generally bad practice but allows isolation on a module basis.


It's probably important to stress that access restriction as a well standardized convention rather than a language-enforced construct works just fine (see Python). On top of that, restricting access to object fields is only ever going to protect the programmer when there is a need to change the value of the data inside an object after creation. Which is already a code smell. Arguably, C's and in particular C++'s const keyword for methods and function arguments is a far greater help to the programmer than Java's rather poor final.


If your definition of Public is the ability to access implementation and data/properties via your own code at any point, the answer is simply: Yes. However, it was abstracted by diverse means - depending on the language.


Test Package, Part No. 978-0-9824775-J-T.





Brief Contents Contents Preface How to Use This Book Chapter 1. Hardware, Software, and the Internet Chapter 2. An Introduction to Software Engineering Chapter 3. Java Syntax and Style Chapter 4. Objects and Classes Chapter 5. Data Types, Variables, and Arithmetic Chapter 6. Boolean Expressions and if-else Statements Chapter 7. Algorithms and Iterations Chapter 8. Strings Chapter 9. Arrays Chapter 10. Implementing and Using Classes Chapter 11. java.util.ArrayList Chapter 12. Class Hierarchies and Interfaces Chapter 13. Algorithms and Recursion Chapter 14. Searching and Sorting Chapter 15. Streams and Files Chapter 16. Graphics Chapter 17. GUI Components and Events Chapter 18. Mouse, Keyboard, Sounds, and Images Chapter 19. Big-O Analysis of Algorithms Chapter 20. The Java Collections Framework Chapter 21. Lists and Iterators Chapter 22. Stacks and Queues Chapter 23. Recursion Revisited Chapter 24. Binary Trees Chapter 25. Lookup Tables and Hashing Chapter 26. Heaps and Priority Queues Chapter 27. Design Patterns Chapter 28. Computing in Context Appendices and index"Not only have I really enjoyed using this text book,but your customer support is aces! I will recommend this book to anyone who asks!"



"Couldn't have done it without you and your textbook!"



"I get more excited about using your text the more I examine the details. I am always pleasantly surprised by the content you've createdand your ways of explaining it."On the Back CoverOnce again, the Litvins bring you a textbook that expertly covers the subject,is fun to read, and works for students with different learning styles. In one volume, this new edition covers both the introductory Java/OOP materialtested on the AP Computer Science exams and more advanced topics (data structures). The focus is on object-oriented programming and design from the start. What has not changed is the authors' respect for students, clear explanationof concepts, and realistic and fun case studies and labs. By choosing this book, you have joined the many thousands of students who havereceived high grades on AP CS exams and mastered computer science fundamentalsusing the Litvins' C++, Java, and Python books.Skylight Publishing



I'm a tad bit confused whether some classes are objects or data structures. Say for example HashMaps in java.util, are they objects? (because of its methods like put(), get(), we dont know their inner workings) or are they data structures? (I've always thought of it as data structures because its a Map).


The distinction between data structures and classes/objects is a harder to explain in Java than in C++. In C, there are no classes, only data structures, that are nothing more than "containers" of typed and named fields. C++ inherited these "structs", so you can have both "classic" data structures and "real objects".

3a8082e126
Reply all
Reply to author
Forward
0 new messages