Python Tutorial
Learn python Tutorial here!
Object-Oriented Programming is a programming paradigm that uses objects to organize code. An object is an instance of a class, and a class is a blueprint for creating objects.
public class MyClass {
private int myVariable;
public void setMyVariable(int value) {
myVariable = value;
}
public int getMyVariable() {
return myVariable;
}
}
In Java, a class is a blueprint for creating objects, and objects are instances of classes. Classes define the properties and behaviors that objects of the class will have.
public class Car {
private String model;
private int year;
public Car(String model, int year) {
this.model = model;
this.year = year;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
}
Inheritance is a mechanism in Java OOP that allows a class to inherit properties and behaviors from another class. The class that is inherited from is called the superclass, and the class that inherits is called the subclass.
public class Animal {
protected String species;
public Animal(String species) {
this.species = species;
}
public void makeSound() {
System.out.println("Animal sound");
}
}
public class Dog extends Animal {
public Dog() {
super("Dog");
}
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
Polymorphism allows objects to be treated as instances of their parent class rather than their actual class type. In Java, it can be achieved through method overriding and interfaces.
interface Shape {
void draw();
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing Circle");
}
}
class Square implements Shape {
@Override
public void draw() {
System.out.println("Drawing Square");
}
}
Encapsulation is the bundling of data (attributes) and methods that operate on the data into a single unit known as a class. It helps in hiding the internal implementation details of an object.
public class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
public double getBalance() {
return balance;
}
}
An abstract class in Java is a class that cannot be instantiated. It can have abstract methods, which are methods without a body. Abstract classes are meant to be extended by subclasses, and the subclasses must implement the abstract methods.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing Circle");
}
}
class Square extends Shape {
@Override
void draw() {
System.out.println("Drawing Square");
}
}
An interface in Java is a collection of abstract methods. Classes implement interfaces to provide concrete implementations for those methods. Interfaces allow for multiple inheritance in Java.
interface Printable {
void print();
}
class Printer implements Printable {
@Override
public void print() {
System.out.println("Printing...");
}
}
Composition is a design principle in Java OOP where a class contains objects of other classes, helping to create complex structures by combining simpler objects. It promotes code reuse and maintainability.
class Engine {
void start() {
System.out.println("Engine started");
}
}
class Car {
private Engine engine;
Car(Engine engine) {
this.engine = engine;
}
void start() {
engine.start();
System.out.println("Car started");
}
}
Enums in Java are a special data type that represents a group of constants. Enumerations are often used to define a fixed set of values that are easily readable and maintainable in the code.
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
Abstraction is the process of hiding complex implementation details and exposing only the necessary features of an object. In Java, abstraction is achieved through abstract classes and interfaces.
Java Code Example:
// Abstract class example
abstract class Shape {
abstract void draw(); // Abstract method with no implementation
void display() {
System.out.println("This is a shape.");
}
}
// Concrete class implementing the abstract class
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle.");
}
}
In this example, the abstract class "Shape" declares an abstract method "draw()" that must be implemented by its concrete subclasses. The "display()" method provides a default implementation. The "Circle" class extends "Shape" and provides a specific implementation for the "draw()" method.