0% found this document useful (0 votes)
8 views4 pages

Java OOP Concepts and Best Practices

This document provides a cheat sheet on Java Exception Handling and Object-Oriented Programming (OOP) principles, including encapsulation, inheritance, polymorphism, and abstraction. It explains core concepts with examples, such as classes, objects, constructors, and access modifiers. Additionally, it outlines best practices for writing clean and maintainable code.

Uploaded by

fibinaj632
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Java OOP Concepts and Best Practices

This document provides a cheat sheet on Java Exception Handling and Object-Oriented Programming (OOP) principles, including encapsulation, inheritance, polymorphism, and abstraction. It explains core concepts with examples, such as classes, objects, constructors, and access modifiers. Additionally, it outlines best practices for writing clean and maintainable code.

Uploaded by

fibinaj632
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Exception Handling Cheat Sheet

1. What is OOP?

OOP (Object-Oriented Programming) is a programming paradigm based on the concept of objects which contain data

and methods.

Core principles: Encapsulation, Inheritance, Polymorphism, and Abstraction.

2. Encapsulation

- Wrapping data and methods into a single unit (class)

- Keeps data safe from outside interference

Example:

class Student {

private int age;

public void setAge(int a) { age = a; }

public int getAge() { return age; }

3. Inheritance

- Acquiring properties from a parent class

- Promotes code reuse

Example:

class Animal {

void sound() { [Link]("Animal sound"); }

class Dog extends Animal {

void bark() { [Link]("Dog barks"); }

4. Polymorphism
Java Exception Handling Cheat Sheet

- One interface, many implementations

- Two types: Compile-time (method overloading) and Runtime (method overriding)

Example of Overloading:

class Math {

int add(int a, int b) { return a + b; }

double add(double a, double b) { return a + b; }

Example of Overriding:

class Animal {

void sound() { [Link]("Animal sound"); }

class Dog extends Animal {

void sound() { [Link]("Dog barks"); }

5. Abstraction

- Hiding implementation details and showing only functionality

- Achieved using abstract classes or interfaces

Example using abstract class:

abstract class Shape {

abstract void draw();

class Circle extends Shape {

void draw() { [Link]("Drawing circle"); }

6. Class and Object


Java Exception Handling Cheat Sheet

- Class: Blueprint of an object

- Object: Instance of a class

Example:

class Car {

String color;

void drive() { [Link]("Driving..."); }

public class Main {

public static void main(String[] args) {

Car myCar = new Car();

[Link]();

7. Constructors

- Special method to initialize objects

- Same name as class

- No return type

Example:

class Car {

Car() {

[Link]("Car is created");

8. Access Modifiers

- Controls access to classes, methods, and variables


Java Exception Handling Cheat Sheet

Types:

- private: Accessible only within the class

- default: Accessible within the package

- protected: Accessible within package and subclasses

- public: Accessible from everywhere

9. Best Practices

- Use meaningful class and method names

- Keep class responsibility limited (Single Responsibility Principle)

- Use interfaces for flexibility

- Prefer composition over inheritance

- Keep fields private and use getters/setters

You might also like