Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

14. OOPs Concepts

 

OOP's Concepts

  • Object means a real-world entity such as a pen, chair, table, laptop, etc.
  • Object-Oriented Programming is a methodology (a system of methods) to design a program using classes and objects.
  • The main aim of OOP's is to bind together the data and the functions that operate on them so that no other part of the code can access this data expect that function.

It simplifies software development and maintenance by providing some following concepts:

  1. Object
  2. Class
  3. Method
  4. Encapsulation
  5. Abstraction
  6. Inheritance
  7. Polymorphism
    • Compile-time polymorphism
    • Run-time polymorphism

Class

  • A class is a user-defined blueprint or prototype from which objects are created.
  • It represents the set of properties or methods that are common to all objects of one type.

Object

  • An object is a basic unit of Object-Oriented Programming and represents the real life entities.
  • It is an instance of a class that contains properties of all the variables and methods in that class.

Methods

  • A method is a collection of statements that perform some specific task and return to the caller.
  • A method can perform some specific task without returning anything.
  • A method is just like a function which is declared inside a class.

Pillars of OOP's

Abstraction

  • Data Abstraction is the property by virtue of which only the essential details are displayed to the user.
  • In Java, abstraction is achieved by interfaces and abstract classes.
  • Using interfaces, we can achieve 100% abstraction while using abstract classes, we can achieve 0-100% abstraction.

Encapsulation

  • It is defined as the wrapping up of data under a single unit.
  • It is the mechanism that binds together code and the data it manipulates.
  • It is a protective shield that prevents the data from being accessed by the code outside this shield.

Inheritance

  • It is the mechanism in Java by which one class is allow to inherit the features (variables and methods) of another class.
  • Super Class: The class whose features are inherited is known as super class.
  • Sub Class: The class that inherits the other class is known as sub class.
  • The sub class can add its own variables and methods in addition to the super class variables and methods.

Polymorphism

  • It refers to the ability of OOPs to differentiate between entities with the same name efficiently.
  • It is a concept by which we can perform a single action in different ways.
  • Polymorphism in Java are achieved by mainly 2 types:
    1. Overloading: Compile-time polymorphism
    2. Overriding: Run-time polymorphism

Custom Class in Java

We can create any number of custom classes in our program. A Java class contains mainly variables and methods.

import java.util.Scanner;

class Student { // Custom Class
    String name;
    int rollNo;
    int marksobt;

    void details() {
        System.out.println("Student name = " + name);
        System.out.println("Student roll no = " + rollNo);
    }

    int marks() {
        return marksobt;
    }
}

class CustomClass {
    public static void main(String[] arr) {
        Student stu1 = new Student(); // Creating an object of custom class
        stu1.name = "Aviskar Chaudhary";
        stu1.rollNo = 1;
        stu1.marksobt = 95;

        Scanner input = new Scanner(System.in);
        Student stu2 = new Student(); // Creating another object of custom class
        System.out.print("Enter your name: ");
        stu2.name = input.nextLine();
        System.out.print("Enter your roll no: ");
        stu2.rollNo = input.nextInt();
        System.out.print("Enter your marks: ");
        stu2.marksobt = input.nextInt();

        System.out.println("\nPrinting details of student 1...");
        stu1.details();
        System.out.println("Student marks: " + stu1.marks());

        System.out.println("\nPrinting details of student 2...");
        stu2.details();
        System.out.println("Student marks: " + stu2.marks());
        input.close();
    }
}

Output :

Enter your name: Deepak Chandra Sharma
Enter your roll no: 2
Enter your marks: 77

Printing details of student 1...
Student name = Aviskar Chaudhary
Student roll no = 1
Student marks: 95

Printing details of student 2...
Student name = Deepak Chandra Sharma
Student roll no = 2
Student marks: 77


Previous

Next

Comments