01. Abstraction and encapsulation

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Advantages of abstraction:

1. Abstraction allows you to use the functionality (behaviour) of the objects from other parts of the code without showing the internal implementation of that functionality (methods). 2. Rights to change the implementation of a class can be given only to a certain set of users. Others cannot go and arbitrarily change anything within the class, apart from using the behaviours defined by that class by creating and using its objects.

How do you create a fully encapsulated class?

1. Make all the instance variables of a class private. 2. Only use getter and setter methods to read or write values of the instance variables.

Advantages of encapsulation:

1. You can make your class read-only or write-only by declaring only getter or setter methods. This prevents other code or malicious code from accessing instance variables in your class that they should not read or modify. 2. You can add variable logic/constraints in the setter methods, so you have full control of the data that can be assigned to the instance variables.

default constructor

A constructor that has no parameter and does nothing apart from creating a new object is known as a default constructor. It is a method that need not be declared if there is no parameterized constructor, or you can declare it and leave it empty in case of a declaration of any other parameterized constructor. Default constructors assign default values to the instance variables of the objects depending on the type, for example, 0, null, etc.

What are OOP principles?

Abstraction Encapsulation Inheritance Polymorphism

What is Abstraction?

Abstraction is a principle based on hiding the details of implementations of classes, and access only certain features/functionalities given to the users/other parts of the program. This is done by writing your program using the framework of classes and objects.

Instances

An object of a class is also referred to as an instance. The word "object" or "instance" can be interchangeably used. For example, s1, s2, s3, etc. are all instances or objects of the Student class.

What is constructor?

Constructor is a special type of method used to initialize an object of a class and define values of the instance variables of the object.

Instance variables

Instance variables are variables from the class, which become the characteristics (or states) of the objects/instances. Every object has its own copies of instance variables. For example, roll number, name, CGPA are all instance variables of the Student class

Static Method

Methods declared as static are known as static methods. A static method belongs to the class, rather than object of a class. It can be called without creating the object of the class, using ClassName.staticMethod( ) notation.

What is procedural programming?

Procedural programming is a method of the programming which has a support of splitting the functionalities into a number of procedures. In procedural programming, a large program is broken down into smaller manageable parts called procedures or functions. Here, priority is given on functions rather than data. In a procedural programming language, a program basically consists of a sequence of instructions each of which tells the computer to do something such as reading inputs from the user, doing the necessary calculation, displaying output.

What does 'this' refer to?

The 'this' keyword is used to refer to the current class instance variable.

What is state and behavior of an object?

The state of an object refers to the specific values of the variables of the class the object belongs to. The behavior of an object refers to the action performed by the object when called by a particular method from the class.

Parameterised constructor

These constructors take in values in the form of input parameters for initializing the instance variables

Static Variable

Variables declared as static are known as static variables. Static variable is used while referring to the common property of all the objects of the class. For example, university name for students in the Student class for an information management system of a university. Static variables are useful if you need to share the same set of information across all objects of a class, such as the University name in our example.

Getter methods are used to

access private variables from outside the class in which they are declared.

setter methods can be called...

after the object is created, for example, when you want to set/modify the value of any instance variable.

Constructors are automatically called...

as soon as the object is being created

Constructors have a return data type and return a value (true or false)

false

Can a static variable or method access non-static variable or method?

no

Can constructors have different name than class?

no

Can one java file have two public classes?

no

How many times does static variable is allocated memory?

once

If any method is declared as final, you cannot

override that particular method

The final keyword is used to

restrict the user

A static member of a class is the property of a class, while a non-static member is the property of an object (True or False)

true

Constructors can be overloaded (true or false)

true

Constructors cannot be extended (true or false)

true

Constructors cannot be made final (true or false)

true

Outer class cannot be private (true or false)

true

They can be called using an object (True or False)

true

static methods are common to all the objects and any particular object can call those methods. (True or False)

true

When you try to call static methods using an object, it will give you a

warning

Can static methods be called without creating the object

yes

If you declare a method of any class as private...

you can call that method within that class only and there is no way to call it from outside that class.

If you make any class constructor private...

you cannot create the instance of that class from outside the class.


संबंधित स्टडी सेट्स

Spanish 1 Midterm Practice Problems

View Set

Microbiology Old exam questions exam 2

View Set

CH7 Transtheoretical Model (TTM) & Stages of change

View Set

Chapter 8 - Eating and sleep - week disorders

View Set

Voting and Public Opinion Exam 1

View Set

Understanding Core Azure Services (30-35%)

View Set

World Geography Chapter 25: India

View Set