Inheritance is the process where one class acquires the properties and methods of another class.
The class that gives properties β Parent / Superclass.
The class that receives properties β Child / Subclass.
Syntax:
Child class extends Parent class
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
public class Test {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
public class Test {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
Method overloading means multiple methods with the same name but different parameters in the same class.
class MathUtil {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
class MathUtil {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
Encapsulation means wrapping data (variables) and code (methods) together into a single unit, and restricting direct access to the data.
In Java, this is usually done by:
Making variables private
Accessing them using public getter and setter methods.
Class:
A Class is a blueprint or template used to create objects.
It defines properties (variables) and behaviors (methods/functions) that the objects created from it will have.
Object:
An Object is an instance of a class.
It represents a real-world entity and can use the variables and methods defined in the class.
class Student {
String name;
int age;
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.name = "Rahul";
s.age = 20;
System.out.println(s.name);
}
}
class Student {
String name;
int age;
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.name = "Rahul";
s.age = 20;
System.out.println(s.name);
}
}
OOP is a programming paradigm based on objects and classes.
Main principles:
1.Encapsulation
2.Inheritance
3.Polymorphism
4.Abstraction
class Car {
String brand;
void drive() {
System.out.println("Car is driving");
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.brand = "Toyota";
c.drive();
}
}
class Car {
String brand;
void drive() {
System.out.println("Car is driving");
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.brand = "Toyota";
c.drive();
}
}
JVM (Java Virtual Machine) is a virtual machine that runs Java bytecode.
Process:
Java Code β Compiler β Bytecode β JVM β Machine Code
Responsibilities of JVM:
Executes bytecode
Memory management
Garbage collection
Platform independence
Compile and run:
javac HelloWorld.java
java HelloWorld
Java is a high-level, object-oriented, platform-independent programming language developed by James Gosling at Sun Microsystems in 1995.
Java programs run on the Java Virtual Machine (JVM) which allows the code to run on any operating system.
Key features:
Platform Independent (Write Once Run Anywhere)
Object-Oriented
Secure
Robust
Multithreaded
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}