0% found this document useful (0 votes)
44 views

Lab Task # 4: Sir Faheem Ahmed Khan

AttributeError: 'Student' object has no attribute 'schoolName' So private constructors and attributes/methods can only be accessed within the class itself and not in any subclasses.

Uploaded by

muhammad arquam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Lab Task # 4: Sir Faheem Ahmed Khan

AttributeError: 'Student' object has no attribute 'schoolName' So private constructors and attributes/methods can only be accessed within the class itself and not in any subclasses.

Uploaded by

muhammad arquam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Name: Muhammad Arquam CMS ID:1345-2020 BSCS

Teacher: Sir Faheem Ahmed


Khan

Lab Task # 4
4.1 Examine the following code. What is true about the variables and methods within the class NY
Customer?

Ans:

They are private. They can be accessed by their classes. They can be accessed only in subclasses. They are
protected

4.2 What is the difference between access and non-access modifiers?

Ans:

Access Modifiers non-Access Modifiers

Access modifiers are the keywords which are used Non-access modifiers are those keywords that do not
with classes, variables, methods, and constructors to have anything related to the level of access but they
control their access level. provide a special functionality when specified.

There are three access modifiers in Python.

Public Final

The members of a class that are declared public are Final keyword can be used with variable, method or
easily accessible from any part of the program. All class. It prevents from its content from being
data members and member functions of a class are modified. When declared with class, it prevents the
public by default. class from being extended.

Private Static

The members of a class that are declared private are The static modifier is used with class variables and
accessible within the class only, private access methods which can be accessed without an instance
modifier is the most secure access modifier. Data of a class. Static variables have only single storage.
members of a class are declared private by adding a All objects share the single storage of static variable.
double underscore ‘__’ symbol before the data
member of that
class.

Protected Abstract

The members of a class that are declared Abstract can be used with class and methods. An
protected are only accessible to a class derived from abstract class can never be instantiated and its purpose
it. Data members of a class are declared protected by is only to be extended. Abstract methods are declared
adding a single underscore ‘_’ symbol before the data without body.
member of that class.

4.3 Can you create a sub class to the following class?

classA
{
privateA()
{
//First Constructor
}

privateA(inti)
{
//Second Constructor
}
}

Explain the answer in with reason and justify through python code.

Ans:
No, you can’t create sub classes to that class which has only private constructors. We can’t

access private modifiers.

Example:

Python code:

class Student:
schoolName = 'XYZ School' # private class attribute

def init (self, name, age):


self. name=name # private instance attribute self.
salary=age # private instance attribute
def display(self): # private method
print('This is private method.')

std = Student("Bill", 25)


std. schoolName

Output:

You might also like