GP OOPS in Java
GP OOPS in Java
Definition
Advantages of OOPs:-
1
Structure of object-oriented programming
The structure of object-oriented programming like java includes the following things:
● Classes are user-defined data types that act as a blueprint for creating individual
objects, methods, and properties.
● Attributes represent the current state of an object. Objects will have data stored
in the attributes field. e.g., color, score, email, etc
● In the context of Java, class is a template used to create objects and define
objects' data types/properties and the methods.
2
● Classes are like categories, and objects are like items within each category.
● All the class objects should have the basic properties of the class.
● Main Core properties include the actual properties/attributes and methods used
by the object.
For example, a specific Dog is an object of the "Dogs" class in this real world. All Dogs in
the world share some characteristics from the same template. Being an animal, they
have a tail and are the faithful of all animals.
In Java, the "Dogs" class is the blueprint from which all individual Dogs can be generated
that includes all Dog's characteristics, such as race, fur color, tail length, eyes shape, etc.
So, for example, you cannot create a car from the cat class because a car must have
specific characteristics such as:- having an engine, windows, and lights — and none of
these objects' properties can be found in the dogs class.
3
● Class keyword followed by the Class name
● superclass with appropriate keyword extends (the name of a class' parent, if
available)
● appropriate Keywords depending on whether the class implements one or more
interfaces or extends from a superclass (if any)
● The class body should be enclosed within curly brackets {}
Syntax:-
● Java object is an instance of a Java class. Each object has a unique identity, a
behavior, and a state of it.
● The new keyword is used to allocate memory at runtime. All objects get memory
in the Heap memory area.
4
Object explanation with real-world entities
Java objects are pretty similar to what we come across in the real world like A Dog, a
lighter, a cat, or vehicles are all objects.
For example, a dog's state includes its color, size, gender, and age, while its behavior is
sleeping, barking, walking around like a security guard at 3 a.m.
Using the new keyword is the best way to create an instance of the class. When we
create an object by using the new keyword, it allocates memory (heap) for the
object and it also returns the reference of that object.
Syntax:-
5
Let's create a program to get familiar with classes and objects
Features of OOPs:-
Four major object-oriented programming features make them different from non-OOP
languages:
● Abstraction is the property by virtue of which only the essential details are
displayed to the user.
● Inheritance allows you to create class hierarchies, where a base class gives its
behavior and attributes to a derived class.
● Polymorphism ensures that it will execute the proper method based on the
calling object’s type.
● Encapsulation allows you to control access to your object’s state while making it
easier to maintain or change your implementation at a later date.