Classes hence become blueprints for these objects, and with them, structure and some sets of behaviors are inherited by every individual object created. An instance is an object created from a class; the state of the object is held by instance variables, which are usually declared in a class but outside any method and maintain consistency throughout the class. The `null` keyword is employed to denote that the variables of the program refer to no object. Instance methods describe an object's behaviour; getters and setters have to do with accessing private instance variables, guarding an object from direct access to its internal state—an equally important principle called encapsulation. This is where constructors play their role in initializing new instances of a class. Called at the events of object creation, they can optionally set initial values for a number of attributes an object would have.
Well, at first, I was really confused with regards to this garbage collection thing until I hit upon the solution for Problem 3 of Assignment 2. Java automatically runs a process called garbage collection, which reclaims the memory occupied by an object that is no longer accessible. In so doing, it avoids problems like dangling pointers—your program referring to deleted objects—and memory leaks where an unused object is not deleted and therefore leads to memory overload. Garbage collection can be forced to happen with the `finalize()` method.
Polymorphism and inheritance are two concepts that have rendered coding a lot easier. A superclass in Java is a base class from which other subclasses inherit, thus forming an extension of the superclass. Inheritance is organized in a tree form, showing the relationships between classes. Polymorphism enables a method to change its behavior depending on the object it is called on. An abstract class is a template for subclasses and cannot be instantiated. A concrete class can be instantiated to create objects.
The chapter covers nested classes, such as classes defined within another class. Inner classes are a type of non-static nested class that cannot refer to non-static members of the outer class. Local classes are defined inside methods, constructors, or initialization blocks, and anonymous classes are unnamed for simple, immediate implementations, often used to implement interfaces.
This chapter has greatly improved my understanding of all concepts related to Java programming and their practical application.
The Landing is a social site for Athabasca University staff, students and invited guests. It is a space where they can share, communicate and connect with anyone or everyone.
Unless you are logged in, you will only be able to see the fraction of posts on the site that have been made public. Right now you are not logged in.
If you have an Athabasca University login ID, use your standard username and password to access this site.
We welcome comments on public posts from members of the public. Please note, however, that all comments made on public posts must be moderated by their owners before they become visible on the site. The owner of the post (and no one else) has to do that.
If you want the full range of features and you have a login ID, log in using the links at the top of the page or at https://landing.athabascau.ca/login (logins are secure and encrypted)
Posts made here are the responsibility of their owners and may not reflect the views of Athabasca University.