0% found this document useful (0 votes)
50 views4 pages

Understanding Classes in Programming

Methods of a class should normally be public so that they can be accessed and used by code outside of the class. Instance variables (attributes) of a class should usually be private to enforce the use of getter and setter methods for accessing and modifying their values. This allows for controlling and restricting access as needed, such as making a field readable but not writeable.

Uploaded by

Iman Javed
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)
50 views4 pages

Understanding Classes in Programming

Methods of a class should normally be public so that they can be accessed and used by code outside of the class. Instance variables (attributes) of a class should usually be private to enforce the use of getter and setter methods for accessing and modifying their values. This allows for controlling and restricting access as needed, such as making a field readable but not writeable.

Uploaded by

Iman Javed
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/ 4

Iman Javed-201400167

Task #3

i. What do you mean by “Class” in programming?

Class is basically a template usually used for modelling real world scenarios. It is a template
which can represent values and behaviour as a unit, a unit which doesn’t exist until someone
creates an instance which is known as an Object.

Now Classes can sometimes be of the following kinds depending on the language of your
preference-

1. Abstract- they cannot be instantiated. Usually extended by other classes which


inherit common behaviour or values.
2. Utility- they usually contain a bunch of utility functions which are context free
and can be used without instantiating an object. Usually these functions are static
in nature.
3. Value - they are usually used for holding values, they have pretty straight
forward functions to get and set values.

ii. What do you mean by "Object" in class?

An object is a component (or instance) of a class; objects have the behaviours of their
class. The thing is that the actual component of programs, while the category specifies
how instances are created and the way they behave. Method a way is an action which an
object is in a position to perform.

iii. How is an object created in JAVA? Write its syntax with


help of example.

In java, objects are an instance of their respective class and a class is a blueprint of an
object. Now, to create an object of a class, keep in mind that the object of an class can
only be create inside a main() function whether that function is in that class itself or it is
inside another class which is located within the same package.

So, to create an object of a class, write the statement following the given syntax inside a
valid main () statement as

class_name obj_name =new class_name (parameter1, parameter2,…)


iv. Why do we build class and objects in programming?

Classes are used to create and manage new objects and support inheritance—a key
ingredient in object-oriented programming and a mechanism of reusing code.

v. What is difference between structured oriented


programming and Object Oriented Programming?

Structured programming mainly focuses on the procedure to be used to handle data


first while Object Oriented Programming (OOP) focuses on data itself.

Structured programming follows top-down approach. Object oriented programming


follows bottom-up approach.

Structured Programming is less secure as there is no way of data hiding. Object Oriented
Programming is more secure as having data hiding feature.

vi. What is Constructor? Why is constructor defined?


A constructor is like an instance method that typically has the identical name because the
class, and may be accustomed set the values of the members of an object, either to default
or to user-defined values. However, although it resembles it, a constructor isn't a correct
method since it doesn't have a return type.

Vii. what is the purpose of keywords: return, private,


public, this?
Public: Java public keyword is an access modifier. It is used to indicate that an
item is accessible anywhere. It has the widest scope among all other modifiers.
Private: Java private keyword is an access modifier. It is used to indicate that a
method or variable may be accessed only in the class in which it is declared.
This: Java this keyword can be used to refer the current object in a method or
constructor.
Return: Java return keyword is used to return from a method when its
execution is complete.
viii. How many ways are used to create an object of the
class in JAVA?

There are five different ways


There are five different ways to create an object in Java: Java new Operator. Java Class.
New Instance() method.

ix. What is the way of accessing the attributes and methods


inside the same class and outside the class?

Accessing the attributes of a class


To check the attributes of a class and also to manipulate those attributes, we
use many python in-built methods as shown below.
• getattr () − A python method used to access the attribute of a class.
• hasattr() − A python method used to verify the presence of an
attribute in a class.
• setattr() − A python method used to set an additional attribute in a
class.
Accessing the method of a class
To access the method of a class, we need to instantiate a class into an object.
Then we can access the method as an instance method of the class as shown in
the program below. Here through the self parameter, instance methods can
access attributes and other methods on the same object.

x. What is difference between primitive and non primitive


data type?
Primitive data type: In simple terms “data type” and “primitive data type” are
simply known and used as interchangeable variables. Primitive data types are
predefined types of data, which are supported by the programming language.
For example, integer, character, and string are all primitive data types.
Non- primitive data type: Non-primitive data types are not defined by the
programming language, but are instead created by the programmer. They are
sometimes called “reference variables,” or “object references,” since they
reference a memory location, which stores the data.
xi. Attributes/ instance variables of class should be
private. Why?

Instance variables are made private to force the users of that class to use
methods to access them. In most cases there are plain getters and setters
but other methods might be used as well. Using methods would allow you,
for instance, to restrict access to read only, i.e. a field might be read but
not written, if there's no setter.

xii. Normally, methods of the class should be public.


Why?
You can certainly override main method and it does not violate any
compiler rules and hence you will not have any compiler errors. You check
that in spite of the fact that you have more than one class a file that is
declared as public is the name of the file you are trying to execute. This is a
convention that the file should be named after the same class which is
public in that code. Hence when you try to execute that class it does not
have a main method from which it starts execution. So if you want to
execute the main method in the non public class the only way to this is call
that main from a main method of the public class.

You might also like