2023-12-0314:05:33
An Introduction to Object Oriented Programming Language.
Programming in the Large II: Objects and Classes.
The overview outlined below covers the following topics.
Objects, Instance Methods, and Instance Variables.
Lesson Objectives:
Understand the fundamentals of objects
Understand the concept of objects, classes and instances
Understand the essential elements of the class: get and set methods
Understand arrays of objects
Constructors and Object Initialization.
Lesson Objectives:
Understand about some essential features of classes, including initialization of instance variables and constructors.
Understand how memory is managed through “garbage collection”.
Some key points learnt so far:
In the example below,
Student std = new Student("John Smith");Student = objectstd = variable
std = new Student();
In this case, "the variable - std refers to or points to the object, - Student,".
So, suppose that the variable std refers to an object that is an instance of class Student. That object contains instance variables name, test1, test2, and test3. These instance variables can be referred to as std.name, std.test1, std.test2, and std.test3. This follows the usual naming convention that when B is part of A, then the full name of B is A.B. For example, a program might include the lines
Getters and Setters
When writing new classes, it's a good idea to pay attention to the issue of access control. Making a member of a class public makes it accessible from anywhere, including from other classes. On the other hand, a private member can only be used in the class where it is defined.
Worth noting that accessor methods are more often referred to as getter methods. A getter method provides "read access" to a variable. (Sometimes for Boolean variables, "is" is used in place of "get". For example, a getter for a Boolean member variable named done might be called isDone().)
A getter and/or setter method defines a property of the class, that might or might not correspond to a variable. For example, if a class includes a public void instance method with signature setValue(double), then the class has a "property" named value of type double, and it has this property whether or not the class has a member variable named value.
Arrays and Objects
Arrays are objects and array variables hold pointers to arrays.
Constructors and Object Initialization.
Initializing Instance Variables
Constructors.
In Java, there is a special type of subroutine name a constructor.
They are not instance methods since they do not belong to object.
Some characteristics of a constructor are as outlined below:
The definition of a constructor looks much like the definition of any other subroutine, with three exceptions.
A constructor does not have any return type (not even void).
The name of the constructor must be the same as the name of the class in which it is defined.
And the only modifiers that can be used on a constructor definition are the access modifiers public, private, and protected. (In particular, a constructor can't be declared static.)
However, a constructor does have a subroutine body of the usual form, a block of statements.
A constructor call is more complicated than an ordinary subroutine or function call. It is helpful to understand the exact steps that the computer goes through to execute a constructor call:
The end result of this is that you have a reference to a newly constructed object.
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.