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, g...

18. Abstraction in Java

 

Abstraction in Java

  • Data abstraction is the property by virtue of which only the essential details are displayed to the user.
  • Data abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details.
  • In Java, abstraction is achieved by abstract classes and interfaces.

Abstract classes and abstract methods

  • An abstract class is a class that is declared with an abstract keyword.
  • An abstract method is a method that is declared inside an abstract class with an abstract keyword without implementation.
  • An abstract class may or may not have all abstract methods. Some of them can be concrete methods.
  • A method defined abstract must always be redefined in the subclass, OR either make the subclass itself abstract.
  • There can be no object of an abstract class.
  • We can change the value of a variable declared in an abstract class.
  • An abstract class can have class members like private, protected etc.
abstract class GrandParent { // Abstarct class (partially abstract)
    int time = 10;

    public GrandParent() { // constructor of abstract class
        System.out.println("This is a constructor of GrandParent class.");
    }

    abstract void patrimony(); // Abstract Method
}

class Parent extends GrandParent {
    public Parent() {
        System.out.println("This is a constructor of Parent class.");
    }

    void patrimony() {
        System.out.println("I am a single child of my parents, so this all patrimony will be mine.");
    }
}

class Child1 extends Parent {
    int time = 15; // Changing the value of variable declared in abstratc class

    public Child1() {
        System.out.println("This is a constructor of Child1 class.");
    }

    void patrimony() {
        System.out.println("I will take this home and a small piece of plot from patrimony.");
    }
}

class Child2 extends Parent {
    public Child2() {
        System.out.println("This is a constructor of Child2 class.");
    }

    void patrimony() {
        System.out.println("I will take a bigger portion of plot from the patrimony.");
    }
}

class AbstractClass {
    public static void main(String[] arr) {
        Parent obj = new Parent(); // Constructor calling for GrandParent and Parent classes
        obj.patrimony();
        System.out.println("This patrimony is with me for " + obj.time + " years.\n");
        Child1 obj1 = new Child1(); // Constructor calling for GrandParent, Parent and Child1 classes
        obj1.patrimony();
        System.out.println("This patrimony is with me for " + obj1.time + " years.\n");
        Child2 obj2 = new Child2(); // Constructor calling for GrandParent, Parent and Child2 classes
        obj2.patrimony();
        System.out.println("This patrimony is with me for " + obj2.time + " years.");
    }
}

Output :

This is a constructor of GrandParent class.
This is a constructor of Parent class.
I am a single child of my parents, so this all patrimony will be mine.
This patrimony is with me for 10 years.

This is a constructor of GrandParent class.
This is a constructor of Parent class.
This is a constructor of Child1 class.
I will take this home and a small piece of plot from patrimony.
This patrimony is with me for 15 years.

This is a constructor of GrandParent class.
This is a constructor of Parent class.
This is a constructor of Child2 class.
I will take a bigger portion of plot from the patrimony.
This patrimony is with me for 10 years.

Interfaces in Java

  • Like a class, an interface can have methods and variables, but methods declared in an interface are by default abstract.
  • To declare an interface, use interface keyword.
  • To implement interface in any class, use implements keyword (just like extends keyword).
  • If a class implements an interface, then it must define all the methods declared in that interface, otherwise the class must be declared abstract.
  • Like an abstract class, we can't create any object of an interface.
  • We can't change the value of a variable declared in an interface.
  • Members of a Java interface are public by default.
  • If we declares a method by default and static keyword (i.e. default and static methods) in the interface, then we can also implement it in the interface.
interface Phone { // An interface (blue print of a class)
    int quantity = 100;

    void name();

    void processorType();

    void internalStorageRAM();

    void camera();

    default void network() {
        System.out.println("This is a 4G smartphone.");
    }
}

class Phone1 implements Phone { // Implementing the interface
    int quantity = 50; // Trying to change the value of variable declared in interface

    public void name() {
        System.out.println("Realme 8s 5G (Universe Purple)");
    }

    public void processorType() {
        System.out.println("MediaTek Dimensity 810 5G Processor");
    }

    public void internalStorageRAM() {
        System.out.println("128 GB ROM expandable upto 1 TB and 8 GB RAM");
    }

    public void camera() {
        System.out.println("64MP + 2MP + 2MP and 16MP front camera");
    }

    public void network() {
        System.out.println("This is a 5G smartphone.");
    }
}

class Phone2 implements Phone { // Implementing the interface
    public void name() {
        System.out.println("Vivo V21e (Sunset Jazz)");
    }

    public void processorType() {
        System.out.println("MediaTek Dimensity 700 Processor");
    }

    public void internalStorageRAM() {
        System.out.println("128 GB ROM expandable upto 1 TB and 8 GB RAM");
    }

    public void camera() {
        System.out.println("64MP + 8MP and 32MP front camera");
    }
}

class Interfaces {
    public static void main(String[] arr) {
        Phone1 realme = new Phone1();
        realme.name();
        realme.processorType();
        realme.internalStorageRAM();
        realme.camera();
        realme.network();
        System.out.println("The total quantity of this piece is: " + Phone.quantity +
                "\n");
        Phone2 vivo = new Phone2();
        vivo.name();
        vivo.processorType();
        vivo.internalStorageRAM();
        vivo.camera();
        vivo.network();
        System.out.println("The total quantity of this piece is: " + Phone.quantity);
    }
}

Output :

Realme 8s 5G (Universe Purple)
MediaTek Dimensity 810 5G Processor
128 GB ROM expandable upto 1 TB and 8 GB RAM
64MP + 2MP + 2MP and 16MP front camera
This is a 5G smartphone.
The total quantity of this piece is: 100

Vivo V21e (Sunset Jazz)
MediaTek Dimensity 700 Processor
128 GB ROM expandable upto 1 TB and 8 GB RAM
64MP + 8MP and 32MP front camera
This is a 4G smartphone.
The total quantity of this piece is: 100


Previous

Next

Comments