Skip to main content

PROJECT- OOP

 

1. What is object-oriented programming? Explain four basic principles of              OOP with suitable examples.

ANS: Object-oriented programming combines a group of data attributes with functions or methods into a unit called an "object." Typically, OOP languages are class-based, which means that a class defines the data attributes and functions as a blueprint for creating objects, which are instances of the class. Popular class-based OOP languages include Java, Python, and C++. Multiple independent objects may be instantiated—or represented—from the same class and interact with each other in complex ways.

Object-oriented programming has four basic concepts: encapsulation, abstraction, inheritance, and polymorphism. Even if these concepts seem incredibly complex, understanding the general framework of how they work will help you understand the basics of an OOP computer program. Below, we outline these four basic principles and what they entail:

  1. Encapsulation
  2. Abstraction
  3. Inheritance
  4. Polymorphism

1. Encapsulation

The word, “encapsulate,” means to enclose something. Just like a pill "encapsulates" or contains the medication inside of its coating, the principle of encapsulation works in a similar way in OOP: by forming a protective barrier around the information contained within a class from the rest of the code.

In OOP, we encapsulate by binding the data and functions which operate on that data into a single unit, the class. By doing so, we can hide private details of a class from the outside world and only expose functionality that is important for interfacing with it. When a class does not allow calling code access to its private data directly, we say that it is well encapsulated.

Example: Elaborating on the person class example from earlier, we might have private data in the class, such as "socialSecurityNumber," that should not be exposed to other objects in the program. By encapsulating this data member as a private variable in the class, outside code would not have direct access to it, and it would remain safe within that person’s object.

If a method is written in the person class to perform, say, a bank transaction called "bankTransaction()," that function could then access the "socialSecurityNumber" variable as necessary. The person’s private data would be well encapsulated in such a class.

2. Abstraction

Often, it’s easier to reason and design a program when you can separate the interface of a class from its implementation, and focus on the interface. This is akin to treating a system as a “black box,” where it’s not important to understand the gory inner workings in order to reap the benefits of using it.

This process is called “abstraction” in OOP, because we are abstracting away the gory implementation details of a class and only presenting a clean and easy-to-use interface via the class’ member functions. Carefully used, abstraction helps isolate the impact of changes made to the code, so that if something goes wrong, the change will only affect the implementation details of a class and not the outside code.

Example: Think of a stereo system as an object with a complex logic board on the inside. It has buttons on the outside to allow for interaction with the object. When you press any of the buttons, you're not thinking about what happens on the inside because you can't see it. Even though you can't see the logic board completing these functions as a result of pressing a button, it's still performing them., albeit hidden to you.

This is the concept of abstraction, which is incredibly useful in all areas of engineering and also applied to great effect in object-oriented programming.

Example: In OOP, we might have a class defined to represent the human body. One might define some functions as part of its publicly facing interface such as “walk()” or “eatFood().” Calling code could call these functions and remain completely oblivious to the complex inner workings of the human body and its necessary functions to perform the act of walking or eating. These details are completely hidden in the implementation of the walk() and eatFood() body functions and are, therefore, us abstracted away from the end user. In these cases, it’s not important for calling code to understand how the brain coordinates walking or how the stomach manages digesting the food, but rather simply that a human walked or ate.

3. Inheritance

Object-oriented languages that support classes almost always support the notion of “inheritance.” Classes can be organized into hierarchies, where a class might have one or more parent or child classes. If a class has a parent class, we say it is derived or inherited from the parent class and it represents an “IS-A” type relationship. That is to say, the child class “IS-A” type of the parent class.

Therefore, if a class inherits from another class, it automatically obtains a lot of the same functionality and properties from that class and can be extended to contain separate code and data. A nice feature of inheritance is that it often leads to good code reuse since a parent class’ functions don’t need to be re-defined in any of its child classes.

Consider two classes: one being the superclass—or parent—and the other being the subclass—or child. The child class will inherit the properties of the parent class, possibly modifying or extending its behavior. Programmers applying the technique of inheritance arrange these classes into what is called an “IS-A” type of relationship.

Example: For instance, in the animal world, an insect could be represented by an Insect superclass. All insects share similar properties, such as having six legs and an exoskeleton. Subclasses might be defined for grasshoppers and ants. Because they inherit or are derived from the Insect class, they automatically share all insect properties.

4. Polymorphism

In OOP, polymorphism allows for the uniform treatment of classes in a hierarchy. Therefore, calling code only needs to be written to handle objects from the root of the hierarchy, and any object instantiated by any child class in the hierarchy will be handled in the same way.

Because derived objects share the same interface as their parents, the calling code can call any function in that class’ interface. At run-time, the appropriate function will be called depending on the type of object passed leading to possibly different behaviors.

Example: Suppose we have a class called, “Animal” and two child classes, “Cat,” and “Dog.” If the Animal class has a method to make a noise, called, “makeNoise,” then, we can override the "makeNoise" function that is inherited by the sub-classes, "Cat" and "Dog," to be “meow” and “bark,” respectively. Another function can, then, be written that accepts any Animal object as a parameter and invokes its "makeNoise" member function. The noise will be different: either a “meow” or a “bark” depending on the type of animal object that was actually passed to the function.

2. What is token? Explain the types of token we use in java programming.

ANS: The Java compiler breaks the line of code into text (words) is called Java tokens. These are the smallest element of the Java program. The Java compiler identified these words as tokens. These tokens are separated by the delimiters. It is useful for compilers to detect errors. Remember that the delimiters are not part of the Java tokens.

For example, consider the following code.

 public class Demo  

{  

public static void main(String args[])  

{  

System.out.println("javatpoint");  

}  

}  

In the above code snippet, public, class, Demo, {, static, void, main, (, String, args, [, ], ), System, ., out, println, javatpoint, etc. are the Java tokens.

The Java compiler translates these tokens into Java bytecode. Further, these bytecodes are executed inside the interpreted Java environment.

Types of Tokens

Java token includes the following Keywords

Identifiers

Literals

Operators

Separators

Comments

Keywords: These are the pre-defined reserved words of any programming language. Each keyword has a special meaning. It is always written in lower case. Java provides the following keywords:

example: 01.abstract 02. boolean 03. byte 04. break 05. class

Identifier: Identifiers are used to name a variable, constant, function, class, and array. It is usually defined by the user. It uses letters, underscores, or a dollar sign as the first character. The label is also known as a special kind of identifier that is used in the goto statement. Remember that the identifier name must be different from the reserved keywords. There are some rules to declare identifiers are:

The first letter of an identifier must be a letter, underscore or a dollar sign. It cannot start with digits but may contain digits.

The whitespace cannot be included in the identifier.

Identifiers are case-sensitive.

Some valid identifiers are:

 PhoneNumber  

PRICE  

radius  

a  

a1  

_phonenumber  

$circumference  

jagged_array  

12radius   //invalid 

Literals: In programming literal is a notation that represents a fixed value (constant) in the source code. It can be categorized as an integer literal, string literal, Boolean literal, etc. It is defined by the programmer. Once it has been defined cannot be changed. Java provides five types of literals are as follows:

Integer, Floating Point, Character, String, Boolean

LiteralType
23int
9.86double
false, trueboolean
'K', '7', '-'char
"javatpoint"String
nullany reference type


Operators: In programming, operators are the special symbol that tells the compiler to perform a special operation. Java provides different types of operators that can be classified according to the functionality they provide. There are eight types of operators in Java, are as follows:

  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Unary Operators
  • Logical Operators
  • Ternary Operators
  • Bitwise Operators
  • Shift Operators
OperatorSymbols
Arithmetic+ , - , / , * , %
Unary++ , - - , !
Assignment= , += , -= , *= , /= , %= , ^=
Relational==, != , < , >, <= , >=
Logical&& , ||
Ternary(Condition) ? (Statement1) : (Statement2);
Bitwise& , | , ^ , ~
Shift<< , >> , >>>



Separators: The separators in Java is also known as punctuators. There are nine separators in Java, are as follows:

  1. separator <= ; | , | . | ( | ) | { | } | [ | ]  

Note that the first three separators (; , and .) are tokens that separate other tokens, and the last six (3 pairs of braces) separators are also known as delimiters. For example, Math. pow(9, 3); contains nine tokens.

  • Square Brackets []: It is used to define array elements. A pair of square brackets represents the single-dimensional array, two pairs of square brackets represent the two-dimensional array.
  • Parentheses (): It is used to call the functions and parsing the parameters.
  • Curly Braces {}: The curly braces denote the starting and ending of a code block.
  • Comma (,): It is used to separate two values, statements, and parameters.
  • Assignment Operator (=): It is used to assign a variable and constant.
  • Semicolon (;): It is the symbol that can be found at end of the statements. It separates the two statements.
  • Period (.): It separates the package name form the sub-packages and class. It also separates a variable or method from a reference variable.

Comments

Popular posts from this blog

English: ch- VALUE by kendel hippolyte

  B 1. Which two phrases in the poem tell us that the poet is recalling childhood memories? Answer: The 1st and the last phrases recall childhood memories. 2. How do we know that the customers at Ma Branch's shop were mostly poor? Answer: Most of the customers get their household chores and pay the money later to the shopkeepers. They are maintaining diaries. On that basis, we can say that most of the customers are poor. 3. Where did Ma Branch position herself? Answer: We consider that Ma Branch was a good shopkeeping store. The behavior of the seller was quite comfortable with the customer. Therefore during later years Ma Branch has improved and consequently shifted to Super Market. 4. Why do you think the poet said no one ever took her for granted? Answer: Both the lady sellers were genuine and workaholics. They used to sell their shops product with the accurate amount and weight without spending more time. They got this habit by day-to-day practicing. That's why customers li...

CHEMISTRY - ATOMIC STRUCTURE

Q 7 d) What is Thomson's plum pudding model? Answer:  Thomson knew that atoms had an overall neutral charge. Therefore, he reasoned that there must be a source of positive charge within the atom to counterbalance the negative charge on the electrons. This led Thomson to propose that atoms could be described as negative particles floating within a soup of diffuse positive charge. This model is often called the plum pudding model of the atom, due to the fact that its description is very similar to plum pudding, a popular English dessert (see image below). Q- 8 a) Explain the discovery of electrons. Ans:  What are Electrons? Electrons are subatomic particles that hold an elementary charge of magnitude -1. The charge of an electron is equal in magnitude to the charge held by a proton (but has an opposite sign). Therefore, electrically neutral atoms/molecules must have an equal number of electrons and protons. Although the magnitude of the charges held by protons and electrons are ...