Question

Python question about creating classes, methods and objects. Class instance variables can be referenced using two...

Python question about creating classes, methods and objects.

  1. Class instance variables can be referenced using two different ways: either using the accessor methods or direct referencing by using the instance variable reference (for example to get the day of the date you can use self.d or self.getDay()). Which one abides more towards the principle of information hiding?
0 0
Add a comment Improve this question Transcribed image text
Answer #0

self.d abides more towards information hiding.

Consider the following example code:

class Test:

def __init__(self, a, b):

self.__a = a
self.b = b
  

def getaval(self):

return self.__a
  

def getbval(self):

return self.b

t = Test(1234, 5678)
print(t.getbval())
print(t.getaval())

print(t.b)
print(t.a)

Output

5678                                                               

1234                                                               

5678                                                               

Traceback (most recent call last):                                 

  File "main.py", line 17, in <module>                             

    print(t.a)                                                     

AttributeError: 'Test' object has no attribute 'a'

The class instances can be accessed using accessor methods as well as instance variable reference as by default they are public.

But when hidden value a is used using double underscores as prefix, it can be accessed using method but not direct reference.

Hence, infomation hiding is more when using direct references.

Add a comment
Know the answer?
Add Answer to:
Python question about creating classes, methods and objects. Class instance variables can be referenced using two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Looking for some quick and useful notes on this: • Using Classes and Objects o Creating...

    Looking for some quick and useful notes on this: • Using Classes and Objects o Creating Objects: understand how to create objects, assign them, invoke methods on them and re-assign references. o The String Class: understanding and using strings and String methods o Packages: use import statement when needed o Wrapper Classes: use Wrapper classes to parse strings. • Writing classes o Anatomy of a Class: understand the concept of a class, define its instance variables, constructor and methods. o...

  • A(n) _______ can typically modify the values of instance variables. a. mutator method b. accessor method...

    A(n) _______ can typically modify the values of instance variables. a. mutator method b. accessor method c. get method d. set method Choose the correct responses.  static methods can be _______. a. called only from other static methods b. called only when an object has been instantiated c. accessed through a reference to any object of the class if they are public d. called when no objects of the class have been instantiated.

  • Start a new project in NetBeans that defines a class Person with the following: Instance variables...

    Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...

  • Need help creating a Java program with mandatory requirements! VERY IMPORTANT: The George account class instance...

    Need help creating a Java program with mandatory requirements! VERY IMPORTANT: The George account class instance must be in the main class, and the requirement of printing a list of transactions for only the ids used when the program runs(Detailed in the Additional simulation requirements) is extremely important! Thank you so very much! Instructions: This homework models after a banking situation with ATM machine. You are required to create three classes, Account, Transaction, and the main class (with the main...

  • Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java,...

    Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java, which will contain code that utilizes your Date.java class. You will submit both files (each with appropriate commenting). A very similar project is described in the Programming Projects section at the end of chapter 8, which you may find helpful as reference. Use (your own) Java class file Date.java, and a companion DateClient.java file to perform the following:  Ask user to enter Today’s...

  • Answer the following: 1.        Create a Class Car with the following instance variables: model (String), Brand...

    Answer the following: 1.        Create a Class Car with the following instance variables: model (String), Brand (String), maxspeed (double) Note: use encapsulation (all variables are private). (3 Marks) 2.        Create a non-default constructor for Class Car which takes 3 parameters. (2 Marks) 3.        Create a get and set methods for instance variable model variable only. (2 Marks) 4.        Create PrintInfo() method which prints all three instance variables. (2 Marks) 5.        Create a subclass GasCar with instance variable TankSize (double).. (2...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • Java Homework Question: Explain how class (static) variables and methods differ from their instance counterparts. Give...

    Java Homework Question: Explain how class (static) variables and methods differ from their instance counterparts. Give an example of a class that contains at least one class variable and at least one class method. Don't forget to provide the code. Also, explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT