Pages

Saturday, April 13, 2024

Basic terminologies in Java

 

  1. Class: A blueprint for creating objects, defining attributes (fields) and behaviors (methods) that objects of that type will have.

  2. Example :

  3. public class Car { // Class definition String color; int year; // Constructor public Car(String color, int year) { this.color = color; this.year = year; } // Method public void drive() { System.out.println("The car is driving."); } }


  4. Object: An instance of a class. It's a concrete realization of the class blueprint, with its own set of attributes and behaviors.

  5. Example :

  6. Car myCar = new Car("Red", 2022); // Creating an object of the Car class


  7. Method: A block of code that performs a specific task. Methods are defined within a class and can be called to execute their functionality.

  8. Example :

  9. public void drive() { System.out.println("The car is driving."); }


  10. Constructor: A special type of method that is called when an object is instantiated. It is used to initialize the object's state.

  11. Example :

  12. public Car(String color, int year) { this.color = color; this.year = year; }


  13. Instance Variables (Fields): Variables declared within a class but outside of any method. They represent the state of an object.

  14. Example :

  15. String color; int year;


  16. Instance Method: A method that operates on an instance of a class. It can access and modify the instance variables of the class.

  17. Example :

  18. public void drive() { System.out.println("The car is driving."); }


  19. Inheritance: A mechanism in which a new class inherits properties and behaviors from an existing class (superclass). It promotes code reuse and allows for the creation of hierarchical relationships between classes.

  20. Example :

  21. public class SportsCar extends Car { // Additional fields and methods specific to SportsCar }


  22. Polymorphism: The ability of objects of different classes to be treated as objects of a common superclass. It allows methods to be called on objects without knowing their specific type at compile time.

  23. Example :

  24. Car myCar = new SportsCar(); // Polymorphism


  25. Encapsulation: The bundling of data (instance variables) and methods that operate on the data into a single unit (class). It hides the internal state of an object and only exposes the necessary functionalities.

  26. Example :

  27. private String model; // Encapsulated field public void setModel(String model) { // Encapsulated method this.model = model; }


  28. Abstraction: The process of hiding the implementation details of a class and only showing its essential features. It allows for the creation of user-defined data types with specific behaviors.

  29. Example :

  30. public abstract class Vehicle { public abstract void drive(); // Abstract method }


  31. Interface: A reference type in Java that defines a set of abstract methods. Classes can implement interfaces, thereby agreeing to provide concrete implementations for those methods.

  32. Example :

  33. public interface Drivable { void drive(); }


  34. Package: A namespace that organizes a set of related classes and interfaces. It helps in avoiding naming conflicts and provides better modularization of code.

  35. Example :

  36. package com.example.myproject;


  37. Access Modifiers: Keywords such as public, private, protected, and default that control the accessibility of classes, variables, and methods in Java.

  38. Example :

  39. public class Car { // Public access modifier private String color; // Private access modifier protected int year; // Protected access modifier int price; // Default (package-private) access modifier }


  40. Static Keyword: A keyword used to declare members (variables and methods) that belong to the class rather than to any specific instance. Static members can be accessed using the class name itself.

  41. Example :

  42. public static double PI = 3.14; // Static variable public static void printMessage() { // Static method System.out.println("Hello!"); }


  43. Exception Handling: The process of handling runtime errors or exceptional conditions in a program. Java provides keywords such as try, catch, finally, and throw for exception handling.

  44. Example :

  45. try { // Code that may throw an exception } catch (Exception e) { // Handling the exception } finally { // Code that runs regardless of whether an exception occurred or not }

No comments:

Post a Comment