Hi guys, In this article, I will explain the Introduction to Java and Oops. let’s get started.
Introduction of Java:-
Java is a popular programming language, created in 1995. It is owned by Oracle, and more than 3 billion devices run Java. James Gosling is the father of Java. Before Oracle, owned by sun microsystems.
It is used for:
- Mobile applications (especially Android apps)
- Desktop applications
- Web applications
- Web servers and application servers
- Games
- Database connection
- And much, much more!
Difference between JDK, JRE, and JVM:-
Java Development Kit:-
JDK stands(or abbreviation, and acronym) for Java Development Kit. JDK is an abstract machine. It is called a virtual machine because it doesn’t physically exist. it is mainly used in runtime environments in which Java bytecode can be executed. JDK is mainly for purposes of loading code, Verifying code, executing code, and providing a runtime environment.
Java Runtime Environment:-
JRE stands for Java Runtime Environment. It is also written as Java RTE. JRE is a set of software tools that are used for developing Java applications. It is used to provide the runtime environment. it is the implementation of JVM. JRE physically exists. It contains a set of libraries + other files that JVM uses at runtime.
Java Development Kit:-
JDK abbreviated Java Development Kit. JDK is a software development environment that is used to develop Java applications and applets. The JDK contains a private JVM and a few other resources such as an interpreter/loader(java), compiler(javac), an archiver(jar), and a documentation generator(Javadoc), etc to complete the development of Java applications. It physically exists. It contains JRE+ development tools. JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation: 1. Standard Edition Java platform, 2. Enterprise Edition Java platform, and 3. Micro Edition Java platform.
Object-Oriented Programming Introduction:-
Object-Oriented Programming:- is a methodology to design a program using classes and objects. It is helpful for software development and maintenance by providing some concepts: 1. Class, 2. Object, 3. Encapsulation, 4. Abstraction, 5. Inheritance, 6. Polymorphism.
The 4 Object-Oriented Principles:- 1. Encapsulation, 2. Abstraction, 3. Inheritance, 4. Polymorphism.
- Encapsulation is known as encapsulation:- binding(or wrapping) data and methods together into a single unit. For example, Capsule is wrapped with different medicines.
Real-time example:- Bag, Bag contains different stuff like pen, pencil, notebook, bottle, etc.
Advantages of Encapsulation:-
- Read-only or write-only (setter or getter).
- Control over the date.
- Data hiding.
- Easy to test.
- Easy and fast to create an Encapsulation class.
Example Program:-
//A Java class which is a fully encapsulated class.
//It has a private data member and getter and setter methods.
public class Student {
// private data
private int id;
private String name;
private long rank;
private String address;
//setter and getter methods
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getRank() {
return rank;
}
public void setRank(long rank) {
this.rank = rank;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class TestStudent {
public static void main(String[] args) {
// creating instance of the encapsulated class
Student s = new Student();
// Setting the values
s.setId(1);
s.setName("John");
s.setRank(11504);
s.setAddress("USA");
// Getting the values
System.out.println("Id: "+s.getId());
System.out.println("Name: "+s.getName());
System.out.println("Rank: "+s.getRank());
System.out.println("Address: "+s.getAddress());
}
}
output:
Id: 1
Name: John
Rank: 11504
Address: USA
2. Abstraction:- is the process of hiding the implementation details of a class and only exposing the necessary information to the user. This allows the user to interact with the object without knowing how it works internally.
Real-time example:- Car, Mobile, Computer, etc.(we don’t know the internal processing).
- A class that is declared with the abstract keyword is known as the Abstract class in Java.
- It can allow abstract and non-abstract methods.
Two Ways to Achieve Abstraction:-
- Abstract class (0% to 100% implementation).
- InterFace(100% Implementation).
Points to remember:-
- An abstract class must be declared with an abstract keyword.
- It can have abstract and non-abstract methods.
- It cannot be instantiated.
- It can have a constructor and static also.
- It can be the final method which will force the subclass not to change the body of the method.
//here, abstrack is a keyword
public abstract class BMW {
public void commanfunction() {
System.out.println("Inside commonfuction method");
}
// we don't(or do) to create body of the method
public abstract void accelerate(); // this is called creation of abstract class
public static void main(String[] args) {
System.out.println("inside the main method");
}
}
public class ThreeSeries extends BMW {
//subclass is override the super class methods.
@Override
public void accelerate() {
System.out.println("Inside threeseries.accelerate");
}
}
public class FiveSeries extends BMW {
//subclass is override the super class methods.
@Override
public void accelerate() {
System.out.println("Inside fiveseries accelerate method");
}
}
public class TestBMW {
// Main method
public static void main(String[] args) {
//Creating a object of ThreeSeries class.
ThreeSeries threeSeries=new ThreeSeries();
//Getting the information
threeSeries.accelerate();
threeSeries.commanfunction();
//Creating a object of ThreeSeries class.
FiveSeries fiveSeries=new FiveSeries();
//Getting the information
fiveSeries.accelerate();
fiveSeries.commanfunction();
}
}
output:
Inside threeseries.accelerate
Inside commonfuction method
Inside fiveseries accelerate method
Inside commonfuction method
Interface:- In Java, Is a blueprint of the class. It has constants and abstract methods. This means that the interface does not specify any code to implement these methods and data fields contain only constants.
Points to remember:-
- An interface can have only abstract methods. Since Java 8, It can have default and static methods.
- It supports multiple Inheritance.
- The interface has only static and final variables.
- The interface can extend to another interface only.
- Members of a Java Interface are public by default.
- Java Interface also represents the IS-A relationship.
// This is interface
public interface TouchScreenLaptop {
//Abstract methods.
public abstract void Scroll();
public abstract void Click();
}
// In this class using abstract keyword because we can implement only one method from the super class
//other wise implements all methods from the super class.
public abstract class Dell implements TouchScreenLaptop {
//we are implementing the only one class that why using abstract keyword.
public void Scroll() {
System.out.println("Dell Scroll");
}
}
public abstract class HP implements TouchScreenLaptop {
//we are implementing the only one class that why using abstract keyword.
public void Scroll() {
System.out.println("HP Scroll");
}
}
// this is not abstract class because we are implementing the 100%(or all methods)
public class HPNoteBook extends HP {
@Override
public void Click() {
System.out.println("HPnotebook click");
}
}
public class Test {
public static void main(String[] args) {
TouchScreenLaptop hp = new HPNoteBook();
hp.Click();
hp.Scroll();
}
}
output:
HPnotebook click
HP Scroll
Final in Interface:-
- If you declare a class final can not extend it.
- If you make a method final you cannot override it and if you make a variable final you cannot modify it. Use final with Java entities you cannot change them further.
- If you make an interface final, you cannot implement its methods which defies the very purpose of the interface.
3. Inheritance: This is the process of creating a new class from an existing class. The new class is called the subclass. inherits the properties and behaviour of the existing class, called the superclass. This allows the Subclass to reuse the code of the superclass and its functionality. Acquiring the properties of one class to another class is called Inheritance. It represents the IS-A relationship which is also known as the parent-child relationship.
In real-time example:- In the real world, a child inherits the features of its parents such as the beauty of the mother and the intelligence of the father.
Inheritance is classified into 4 types: they are 1. Single, 2. Multiple, 3. Multilevel & 4. Hybrid.
- Single Inheritance:- When a class inherits another class. It is known as a Single Inheritance.
public Class A {
public void methodA() {
System.out.println("Base class method");
}
}
Class B extends A {
public void methodB() {
System.out.println("Child class method");
}
public static void main(String args[]) {
B obj = new B();
obj.methodA(); //calling super class method obj.methodB(); //calling local method
}
}
2. Multiple Inheritance:- The capability of creating a single class with multiple superclasses. It is known as Multiple Inheritance. Multiple Inheritance should not be supported because it is confused which method is implemented in the subclass.
public class A {
public void method() {
System.out.println("Inside Method A");
}
}
public class B {
public void method() {
System.out.println("Inside Method B");
}
}
//A,B is get a error like Syntax error on tokens, AnnotationName expected instead
public class Test extends A,B {
public static void main(String[] args) {
Test test= new Test();
Trying to call above functions of class where
// Trying to call above functions of class where
//Error is thrown as this class is inheriting
//multiple classes
test.method();
}
}
3. Multilevel Inheritance:- When there is a chain of inheritance. It is known as Multilevel Inheritance.
class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Maruti extends Car{
public Maruti()
{
System.out.println("Class Maruti");
}
public void brand()
{
System.out.println("Brand: Maruti");
}
public void speed()
{
System.out.println("Max: 90Kmph");
}
}
public class Maruti800 extends Maruti{
public Maruti800()
{
System.out.println("Maruti Model: 800");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
output:
Class Car
Class Maruti
Maruti Model: 800
Vehicle Type: Car
Brand: Maruti
Max: 80Kmph
4. Hierarchical Inheritance:- When two or more classes inherit a single class. It is known as Hierarchical Inheritance.
public class Vehicle {
String fuel() {
return "petrol";
}
}
public class Bike extends Vehicle {
String fuel() {
return "petrol";
}
}
public class Bus extends Vehicle {
String fuel() {
return "CNG";
}
}
public class Car extends Vehicle {
@Override //this lines are overriding line
String fuel() {
return "Diesel";
}
}
public class OverridingTest {
public static void main(String[] args) {
Car car = new Car();
System.out.println("Car: "+car.fuel());
Bike bike = new Bike();
System.out.println("Bike: "+bike.fuel());
Bus bus = new Bus();
System.out.println("Bus: "+bus.fuel());
}
}
output:
Car: Diesel
Bike: petrol
Bus: CNG
4. Polymorphism:- Poly means “Many” Morphic or Morphism means “forms”. Polymorphism is “many forms”.
Real-time example:- A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, and an employee. So the same person possesses different behaviors in different situations.
Points to remember:-
- Compile time polymorphism This type of polymorphism in Java is also called static or non-static polymorphism.
- Method overloading is considered a class where multiple methods have the same name.
- Runtime polymorphism.
- Method overriding.
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}
output:
The animal makes a sound
The pig says: wee wee
The dog says: bow wow
Conclusion:-
Thanks for reading Please Follow Ram.