WEEK 7 PART 1 Flashcards
What is OOP?
OOP is a design concept. It stands for Object Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old programming languages (C, Pascal, etc.). Everything in OOP is grouped as self- sustainable “objects”. In OOP we use different programming languages like - Ruby, Python, Java, C++ etc. Simula was the first OOP language.
What is a Class?
A class is simply a representation of a type of object. It is the blueprint, or plan, or template, that describes the details of an object. A class is the blueprint from which the individual object is created. Class is composed of three things: a name, attributes, and operations.
What is an Object?
Object is an instance of a class. An object in OOPS is nothing but a self-contained component that consists of methods and properties to make a particular type of data useful.
Objects are derived from the Class by the blueprint or design of a class. For example: if we have a Student class, we can derive objects from it as follows -
Student objStudent = new Student;
What are the 4 principles of OOP?
Object-oriented programming has four basic concepts:
- *1.Encapsulation**
- *2.Abstraction**
- *3.Inheritance**
- *4.Polymorphism**
What Is Encapsulation?
Encapsulation is one of the best Java OOPs concepts of wrapping the data and code. In this OOP concept, the variables of a class are always hidden from other classes. It means data hiding. This will wrap the code and data into a single unit and be better for ‘unit testing’. It can only be accessed using the methods of their current class. For example – in school, a student cannot exist without a class.
What Is Abstraction?
Abstraction is an act of representing essential features without including background details. It is a technique of creating a new data type that is suited for a specific application. For example, while driving a car, you do not have to be concerned with its internal working. Here you just need to be concerned about parts like the steering wheel, Gear, accelerator, etc.
What is Inheritance?
Inheritance is one of the Basic Concepts of OOPs in which one object acquires the properties and behaviors of the parent object. It is basically creating a parent-child relationship between two classes. Child class can obtain all the features from a parent class and also allows us to reuse the methods of that class.
What Is Polymorphism?
Polymorphism refers to one of the OOPs concepts in Java which is the ability of a variable, object, or function to take on multiple forms. Polymorphism happens when multiple classes are related to each other due to inheritance.
For example, in English, the verb run has a different meaning if you use it with a laptop, a foot race, and business. Here, we understand the meaning of run based on the other words used along with it. The same also applied to Polymorphism.
It’s used in Method Overloading and Method Overriding.
Data types in Java
Java has two categories of data:
- *1. Primitive Data Type:** These data types are pre-defined and can’t be assigned a null value as these have their own value. For example - boolean, char, int, short, byte, long, float, and double
- *2. Non-Primitive Data Type or Object Data type:** These data types are created by the programmers, are not pre-defined and can be assigned with null values. For example - String, Array, etc.
Primitive Datatypes
A primitive data type is pre-defined by the programming language. The size and type of variable values are specified, and it has no additional methods.
1. boolean: A boolean data type comprises of a bit of information and can store only true or false values. This data type is used to track true/false conditions.
2. byte: This is an example of a primitive data type. It is an 8-bit signed two’s complement integer. It stores whole numbers that lie between -128 to 127. A byte data type is helpful for saving memory in large amounts.
3. short: A short data type is greater than byte in terms of size and less than a integer. It stores the value that ranges from -32,768 to 32767. The default size of this data type: 2 bytes.
4. int: This data type can store whole numbers from -2147483648 to 2147483647. Generally, int is the preferred data type when you create variables with a numeric value.
5. long: This data type is a 64-bit two’s complement integer. By default, the size of a long data type is 64 bit and its value ranges from -263 to 263-1.
6. float: You should use a floating point type whenever you need a number with a decimal, such as 8.88 or 3.14515.
7. double: The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a “d”:
8. char: This data type is used to store a single character. The character must be enclosed within single quotes, like ‘E’ or ‘e’. Alternatively, you can also use ASCII values to display certain characters.
Non-Primitive Datatypes
Non-Primitive data types refer to objects and hence they are called reference types. Examples of non-primitive types include Strings, Arrays, Classes, Interfaces, etc.
1. Strings: String is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object.
2. Arrays: Arrays in Java are homogeneous data structures implemented in Java as objects. Arrays store one or more values of a specific data type and provide indexed access to store the same. A specific element in an array is accessed by its index.
3. Classes: A class in Java is a blueprint that includes all your data. A class contains fields(variables) and methods to describe the behavior of an object.
4. Interface: Like a class, an interface can have methods and variables, but the methods declared in the interface are by default abstract (only method signature, nobody).
Difference between primitive and non-primitive data types
Primitive types are predefined in Java. Non-primitive types are created by the programmer and are not defined by Java.
Non Primitive types can be used to call methods to perform certain operations, while primitive types cannot.
A primitive type always has a value, whereas non-primitive types can be null.
A primitive type starts with a lowercase letter, while non-primitive types start with an uppercase letter.
The size of a primitive type depends on the data type, while non-primitive types have all the same size.
What is a method/function?
When a few lines of code are grouped together is called a method. The benefits of creating method are to create
cleaner code, increase reusability, and easy maintenance.
Type of method
there are two types of method
1. void - this type of method does not return any value
2. Non-void - this type of method does always return value based on the method’s data type
Can we declare the main method of our class as private?
In java, the main method must be public static in order to run any application correctly. If the main method is declared as private, the developer won’t get any compilation error however, it will not get executed and will give a runtime error.