The Student Room Group

Object-Oriented Programming

I've just learnt about 'Private' and 'Public' fields within classes. I understand Private means no classes outside of the derived class can access the data contained within.

Public means classes outside the derived class can access the data contained within, correct?

I thought the whole idea of OOP was that all data and processes required were inside the object. So why the need to have 'public' available to other classes?

Finally, off topic, is OOP a technique of a Structured Programming? I know that imperative is.
Reply 1
What do you mean by derived? Subclass? There's usually a third access modifier to allow access to subclasses.

Public can be accessed from anywhere, by any class. Private means it can only be accessed by the local class.

The whole point of OOP is so objects can interact with other objects. There should be plenty of data flowing between objects and calling methods of other classes. OOP is a totally different programming paradigm, totally different to structured programming. You have to think about things in a totally different way.

For example, you can have a Car class. The class is the blueprint of how to make a car and what attributes it has. You can create a Car object, you can make it accelerate(), changeeGear() etc and it might have variables such as the current speed, what gear it is. You can create as many cars as you like. You can create a Driver who operates the Car. They obviously have to interact with each other.
I see where you're coming from. One of the concepts of OOP is to encapsulate the data and behaviour, as you say. But, you will have a lot of different classes in your program for different things. E.g. a library system could have a book class, a borrower class, a staff member class etc. Each of these classes will need to interact with each other.

For example if you had a book class, you might use something like this:
myBookObject.Loan(myBorrower)
(remembering objects are derived from classes).

The Loan method of the myBook class would need to be public so that other classes can use that method to loan out a book. If it wasn't public, you'd have to do all the work in the myBook class which is going back to an imperative programming approach.

As for variables, what usually happens is you have private "member" variables which the class keeps to itself to do whatever. If other classes need to access that variable, accessor methods are used. It might look something like this:


start myClass

private m_myname as string

public getMyName() {
return m_myname
}

public setMyName(name as string) {
m_myname = name
}

end myClass


The idea is that if another class needed to know the name of a member, for example, it uses the getMyName method. This allows the class to give it the value rather than the other class going in there and taking it. The idea there being that your first class might be in the middle of doing something with that value which could cause errors later on. So you could add an IF statement or whatever to decide whether or not to give out the value.


If you're used to a procedural style of programming, OOP can seem a bit weird at first, but it'll start to make sense. Which programming language are you using? Might be able to get some better examples to explain it more clearly.
Reply 3
theArchitect
I see where you're coming from. One of the concepts of OOP is to encapsulate the data and behaviour, as you say. But, you will have a lot of different classes in your program for different things. E.g. a library system could have a book class, a borrower class, a staff member class etc. Each of these classes will need to interact with each other.

For example if you had a book class, you might use something like this:
myBookObject.Loan(myBorrower)
(remembering objects are derived from classes).

The Loan method of the myBook class would need to be public so that other classes can use that method to loan out a book. If it wasn't public, you'd have to do all the work in the myBook class which is going back to an imperative programming approach.

As for variables, what usually happens is you have private "member" variables which the class keeps to itself to do whatever. If other classes need to access that variable, accessor methods are used. It might look something like this:


start myClass

private m_myname as string

public getMyName() {
return m_myname
}

public setMyName(name as string) {
m_myname = name
}

end myClass


The idea is that if another class needed to know the name of a member, for example, it uses the getMyName method. This allows the class to give it the value rather than the other class going in there and taking it. The idea there being that your first class might be in the middle of doing something with that value which could cause errors later on. So you could add an IF statement or whatever to decide whether or not to give out the value.


If you're used to a procedural style of programming, OOP can seem a bit weird at first, but it'll start to make sense. Which programming language are you using? Might be able to get some better examples to explain it more clearly.


I see what you mean. (All of this is for theory, I'm not actually using the language)

If classes are going to be interacting with one another doesn't this render encapsulation useless since all the data they need won't be inside the object that is being used?

So if there is a 'Public' asking for the name of a borrower of a book in class A, and then class B decides to ask for the name too, will it get the code from class A? (Hope that makes sense)
Sithius
I see what you mean. (All of this is for theory, I'm not actually using the language)

If classes are going to be interacting with one another doesn't this render encapsulation useless since all the data they need won't be inside the object that is being used?

So if there is a 'Public' asking for the name of a borrower of a book in class A, and then class B decides to ask for the name too, will it get the code from class A? (Hope that makes sense)

The data encapsulated in a class is data about the class itself; but some of that data might be needed by other classes.

Most programming languages like Java or Visual Basic have user interface components like textboxes. Thinking of a textbox...

A textbox is a class - it has a graphical element for user input - but it's still a class.

It encapsulates data about itself like:
> text - the text shown in the textbox
> length - how many characters it can hold
> password - whether it is a password field or not

You might have behaviour like:
> giveArray - give an array of characters that make up the text in the textbox
> validateText - see if the text matches a validation rule.

This means the class encapsulates the data to be processed like the text, length, password etc. and the behaviour that uses this data like a procedure to validate the text entered into the textbox.


What I was touching on earlier was accessor methods. Thinking of the text box again...

Text, length and password could be thought of as properties of the textbox. Each one of these things would probably be useful to other classes. If the textbox was on a form collecting data about a user, for example, the form (another class) would probably want to know what text had been typed into the textbox so it could save the data to a database.

You could just have the text property as a public variable. But you usually have it as private so that other classes can't go in and change it. Insead you have accessor methods which control how the data is taken in and given out.

Therefore you'd might have behaviour like:
> getText - get the text shown in the textbox
> setText - set the text shown in the textbox
> getLength - get how many characters it can hold
> setLength - set how many characters it can hold.

therefore the form class might have a method that gets the data from the textbox and saves it. To get the data from the textbox object it would do:
txtUsername.getText
it can now use that data to save it to the database. It wouldn't actually store it as a property of the form class itself - just a private variable.

What is actually happening is in the textbox class, there is a private variable that stores the text. The can only be accessed by that textbox object. The only way other classes can use that data is to ask for it by using these accessor method. The getText accessor method could just say "OK here's the text...". But maybe the textbox is just about to update the text so the accessor method could do something like "am I about to update the textbox?... oh I am... I won't give out the text at the moment I'll give the other object an error".

So in a class you can have loads of private variables which are used when carrying out calculations - other classes don't need to see these. But for those properties that do need to be seen, accessor methods are used.



Going back to your question, if class A was a Borrower and class B was a Loan class. Class A would store all the data about the person: name, address, DOB etc. It might also have behaviour to, for example, give the number of books that person currently has on loan, suspend the member for not returning books etc.

However, class B's job is to Loan out books. It might store data like how many days a book can be loaned out for, today's date so it knows when it loaned. It would also have behaviour to loan out a book.

As part of class B's behaviour to loan out a book, it might need the borrower's name to save to the database. To get that data it would ask class A for the borrower's name. It could then carry on saving the rest of the details to the database. Because class B is a loan class, it doesn't need to store data on the borrower because the borrower's name isn't to do with loaning books. However, it does need the borrower's name as part of the process of doing the loan which is why it asks the other class.


Hope this kinda makes sense, it's a bit of a tricky topic to explain.
Reply 5
Think I've got it, cheers for the help, I've repped you.

It was said earlier OOP isn't a structured programming language. How is there a difference?
Sithius
Think I've got it, cheers for the help, I've repped you.

It was said earlier OOP isn't a structured programming language. How is there a difference?

Thank for the rep :smile: hope it all makes some sense - it's one of those things that's really hard to explain.

Regarding structured programming, what does the exam board define structured programming as?
Reply 7
theArchitect
Thank for the rep :smile: hope it all makes some sense - it's one of those things that's really hard to explain.

Regarding structured programming, what does the exam board define structured programming as?


Not quite sure, the spec doesn't even say. :s-smilie:
Reply 8
Aren't virtually all modern games basically object-oriented programs operating within a governing 'meta-narrative' framework (which, presumably, is 'structured' in nature)?

I ask only out of idle curiosity, mind; my last game was conceived in Year 8, and employed QBasic.
Reply 9
What about some languages that are definitely not structured? Prolog?
Sithius
Not quite sure, the spec doesn't even say. :s-smilie:

I think structured programming, in this context, means breaking down a program into procedures/functions instead of having one massive code file that's executed from the top going down, only changing place using a GOTO <line number> statement.

So it would seem that OOP could be classed as structured. Although OOP is a different programming paradigm to structured. I think it would be best to double check with your teacher as to what's classed as what.
Sithius
What about some languages that are definitely not structured? Prolog?

Yeah Prolog isn't structured. It's a functional/logic language that uses facts and rules to determine an outcome.
Reply 12
theArchitect
Yeah Prolog isn't structured. It's a functional/logic language that uses facts and rules to determine an outcome.


Ok thanks. Now that I've been reading into I'm fairly sure structured means using selections, iterations and sequences, using procedures/functions like you said. That isn't really OOP?

Latest

Trending

Trending