What Is OOPS?: 1) Class
What Is OOPS?: 1) Class
1) Class
The class is one of the Basic concepts of OOPs which is a group of similar
entities. It is only a logical component and not the physical entity. Lets
understand this one of the OOPs Concepts with example, if you had a class
called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota,
etc. Its properties(data) can be price or speed of these cars. While the
methods may be performed with these cars are driving, reverse, braking etc.
2) Object
An object can be defined as an instance of a class, and there can be multiple
instances of a class in a program. An Object is one of the Java OOPs
concepts which contains both the data and the function, which operates on
the data. For example – chair, bike, marker, pen, table, car, etc.
3) Inheritance
Inheritance is one of the Basic Concepts of OOPs in which one object
acquires the properties and behaviors of the parent object. It’s creating a
parent-child relationship between two classes. It offers robust and natural
mechanism for organizing and structure of any software.
4) 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. 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.
5) Abstraction
Abstraction is one of the OOP Concepts in Java which 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.
Lets understand this one of the OOPs Concepts with example, while driving a
car, you do not have to be concerned with its internal working. Here you just
need to concern about parts like steering wheel, Gears, accelerator, etc.
6) Encapsulation
Encapsulation is one of the best Java OOPs concepts of wrapping the data
and code. In this OOPs concept, the variables of a class are always hidden
from other classes. It can only be accessed using the methods of their current
class. For example – in school, a student cannot exist without a class.
Abstraction Encapsulation
Abstraction in Object Oriented
Encapsulation solves it implementation
Programming solves the issues at the
level.
design level.
An abstract class can have both abstract and The interface can have only
non-abstract methods. abstract methods.
It does not support multiple inheritances. It supports multiple inheritances.
It can not provide the
It can provide the implementation of the interface. implementation of the abstract
class.
An abstract class can have protected and An interface can have only have
abstract public methods. public abstract methods.
An abstract class can have final, static, or static The interface can only have a
final variable with any access specifier. public static final variable.
Advantages of Abstraction
The main benefit of using an Abstraction in Programming is that it
allows you to group several related classes as siblings.
Abstraction in Object Oriented Programming helps to reduce the
complexity of the design and implementation process of software.
When to use Abstract Methods & Abstract Class?
Abstract methods are mostly declared where two or more subclasses are also
doing the same thing in different ways through different implementations. It
also extends the same Abstract class and offers different implementations of
the abstract methods.
Java Syntax
System.out.println("Hello World");
A class should always start with an uppercase first letter. Any code inside
the main() method will be executed. every Java program has a class name
which must match the filename, and that every program must contain
the main() method.
System.out.println()
Inside the main() method, we can use the println() method to print a line of text
to the screen:
System is a built-in Java class that contains useful members, such as out,
which is short for "output". The println() method, short for
"print line", is used to print a value to the screen (or a file)
You should also note that each code statement must end with a semicolon ( ;).
If you don't want others (or yourself) to overwrite existing values, use
the final keyword (this will declare the variable as "final" or "constant", which
means unchangeable and read-only): e.g final int myNum = 15;
Casting. Is when you assign the value of a primitive datatype to another datatype
public class Main {
int myInt = 9;
System.out.println(myInt); // Outputs 9
Code Result
\n New Line
\r Carriage Return
\t Tab
\b Backspace
\f Form Feed
Java Short Hand If...Else
(Ternary Operator)
int time = 20;
System.out.println(result);