Basic concepts of Java

java dev.to

Java Oop's

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects that contain data (fields) and behavior (methods). It focuses on designing software that closely represents real-world entities. It is used to:

  • Improves code reusability
  • Enhances maintainability and scalability
  • Makes programs easier to understand and manage
  • Closely models real-world entities

Class

A Class in Java is a blueprint or template used to create objects. It defines the properties (data) and behaviors (methods) that objects of that class will have.

  • A class is a user-defined data type.
  • It contains variables (fields) and methods.
  • Multiple objects can be created from a single class.

Object

An Object in Java is an instance of a class that represents a real-world entity. It is used to access the variables and methods defined inside a class.

  • State: Represents the current data or attributes of an object.
  • Behavior: Represents the actions that an object can perform.
  • Identity:Every object has a unique identity in memory that distinguishes it from other objects, even if they contain the same data.

Abstraction

Abstraction in Java is the process of hiding implementation details and showing only the essential features of an object. It helps users focus on what an object does rather than how it does it.

  • Hides complexity: Internal implementation details are hidden from the user.
  • Improves maintainability: Changes in implementation do not affect the user code.
  • Enhances flexibility: Supports loose coupling through abstract classes and interfaces.

Encapsulation

Encapsulation is the process of wrapping data and methods into a single unit, usually a class, and restricting direct access to the data. It acts as a protective shield that prevents data from being accessed directly from outside the class.

  • Data members are hidden using the private access modifier.
  • Access to data is provided through public getter and setter methods.
  • It improves data security, maintainability, and controlled access.

Inheritance

Inheritance is a core OOP concept in Java that allows one class to acquire the fields and methods of another class using the extends keyword. It represents an “is-a” relationship between classes.

  • The class being inherited is called the superclass, and the inheriting class is the subclass.
  • A subclass can use existing features of the superclass and also add its own.
  • Inheritance promotes code reusability and reduces redundancy.

Polymorphism

Polymorphism means “many forms”, where a single entity can behave differently in different situations. In Java, it allows the same method or object to show different behavior based on context.

  • Same method, different behavior depending on the object
  • Achieved through method overloading and method overriding

Java Data types:

Java data types define the type of data a variable can store in a program. They help the compiler allocate memory efficiently and ensure type safety. Java provides two main categories: primitive and non-primitive data types.

  • Memory allocation determines how much memory is required for each variable
  • Operations support defines what operations can be performed on data
  • Each data type has a default value when not initialized
  1. Primitive Data Types: Store simple values directly in memory.
  2. Non-Primitive (Reference) Data Types: Store memory references to objects.

References:

https://www.geeksforgeeks.org/java/object-oriented-programming-oops-concept-in-java/
https://www.geeksforgeeks.org/java/java-data-types/

Source: dev.to

arrow_back Back to Tutorials