OOP with java

 Java

Introduction to java 


Java is a general purpose  object oriented programming language developed by Sun Micro system of USA in 1991 by the team lead by James Gosling.

The main purpose of to create java is for platform independent. It is object oriented means that everything in java is associated with class and object among with attributes (data)  and method (function).


Here is simple java program

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello world ");
    }
   
}


Bytecode:

                Byte code is a lower-level representation of a program that is usually produced as the output of a compiler. It is a series of instructions that can be interpreted by a virtual machine or runtime environment, rather than by the processor of a computer. This makes it easier to run the same program on different types of systems, since the byte code can be interpreted by a virtual machine that is implemented for each platform. Byte code is often used in the context of programming languages such as Java and Python, which use a byte code interpreter to execute programs.

Java Virtual Machine (JVM):

                                                    It is a engine that provides runtime  environment to execute byte code and convert byte code into machine language. JVM  is different for different system , hence it is platform dependent but all the JVM can interpret byte code. 

Compiling and executing the java program 

public class Example1{
    public static void main(String[] args) {
        System.out.println("Hello everyone :");
        int x= 5;
        int y= 6;
        int sum=x+y;
        System.out.println("Sum is :"+sum);
    }
   
}

For above java program Example2
Compiling :-
                    javac Example1.java 
                    after bytecode is created .It is in the form of class extension i.e. Example2.class
Executing :-
                    java Example2 

Principle or Characteristic of OOP:

1. Encapsulation: 

Encapsulation is a programming concept that refers to the bundling of data with the methods that operate on that data. In object-oriented programming languages, encapsulation is used to refer to the bundling of data and the methods that operate on that data within a single unit, or object. In Java, encapsulation is implemented using the concept of a class, which defines the data and methods that belong to an object. By encapsulating data and methods within a class, developers can protect the data from unauthorized access and modification, and can more easily reuse the code in other programs.


2. Inheritance:

Inheritance is a programming concept that refers to the ability of a class to inherit characteristics and behavior from another class. In object-oriented programming languages, inheritance is used to define a relationship between two classes, where one class (the subclass) is derived from another class (the superclass). The subclass inherits characteristics and behavior from the superclass, and can also have additional characteristics and behavior of its own.


In Java, inheritance is implemented using the extends keyword, which is used to define a subclass that is derived from a superclass. For example:


public class Animal { // characteristics and behavior of animals 
}
public class Dog extends Animal {
// characteristics and behavior of dogs, which are a type of animal 
}

In this example, the Dog class is a subclass of the Animal class, and it inherits all of the characteristics and behavior of animals. The Dog class can also have additional characteristics and behavior of its own, which are specific to dogs.


3.Polymorphism :

Polymorphism is a programming concept that refers to the ability of a class to take on multiple forms. In object-oriented programming languages, polymorphism is used to allow a single interface to be used to refer to multiple different implementations of that interface.


There are two main types of polymorphism in Java:


Method overloading: This is the ability of a class to have multiple methods with the same name, but with different parameters. When a method is called, the Java runtime determines which version of the method to execute based on the number and type of the parameters passed to the method.


Method overriding: This is the ability of a subclass to provide a different implementation of a method that is inherited from a superclass. When a method is called on an object, the Java runtime determines which version of the method to execute based on the type of the object.


Polymorphism allows developers to write code that is more flexible and reusable, as the same interface can be used to refer to different implementations of that interface depending on the context.


4. Abstraction: 

Abstraction is a programming concept that refers to the ability to focus on the essential characteristics of an object, while ignoring unimportant details. In object-oriented programming languages, abstraction is used to define the interface of an object, which specifies the methods that can be called on the object and the parameters that those methods accept, without revealing the implementation details of the object.


In Java, abstraction is implemented using abstract classes and interfaces. An abstract class is a class that cannot be instantiated, but can be subclassed. An abstract class can contain both abstract methods (which have no implementation) and concrete methods (which have an implementation). An interface is a collection of abstract methods that must be implemented by any class that implements the interface.


Abstraction allows developers to create reusable code that can be customized for specific use cases, without needing to know the implementation details of the code. This makes it easier to develop complex programs, as developers can focus on the high-level functionality of the code rather than getting bogged down in the details.




Comments