JAVA Class Fundamentals

class Fundamentals

Class Fundamentals of Java

Classes have been utilised in Java ever since its inception. But up until now, a class has only been utilised in its most basic form. The major purpose of the classes developed in the previous chapters was to merely contain the main () method, which was used to illustrate the fundamentals of Java grammar. Classes are significantly more powerful than the few ones that have been shown so far, as you will see.

Class supports many  fundamentals feature such Polymorphism ,Inheritance, Encapsulation, Abstraction 

This article covers all the details you need to know about Java class fundamentals.

syntax

 class 
{
//Class body containing objects, variables and methods
}

Code example of class

Run

public class Employee
{
    String name, city, state;
    int age, phn_num;
            void display()
    {
                //body of the code;
     }
}

Some important features of java

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism
fundamentals of java

Encapsulation :

Encapsulation in java

The definition of encapsulation is the grouping of data into a single unit. It is the method that connects the code to the data that it works with. Encapsulation can also be viewed as a barrier that stops code from the outside of the barrier from accessing the data.

Encapsulation’s key benefits are giving you control over your data, data hiding. You can implement the code inside the constructor function if you wish to set an ID value that must be higher than 100 alone. You can program the setter methods with logic that prevents the storage of negative integers.

Run 

public class Main
{
//ENCAPTULATION EXAMPLE CODE WITH GETTER AND SETTER METHOD
private String name;
private int age;
private String address;
private String phone;
private String email;
public static void main (String[]args)
  {
Main obj = new Main ();
obj.setAge (20);
obj.setName ("Rahul");
System.out.println (obj.getName ());
System.out.println (obj.getAge ());
} 
//getter method
public String getName ()
  {
return name;
}
//setter method
public void setName (String name)
  {
this.name = name;
} 
public int getAge ()
  {
return age;
}
public void setAge (int age)
  {
this.age = age;
}
public String getAddress ()
  {
return address;
}
public void setAddress (String address)
  {
this.address = address;
} 
public String getPhone ()
  {
return phone;
}
public void setPhone (String phone)
  {
this.phone = phone;
} 
public String getEmail ()
  {
return email;
}
public void setEmail (String email)
  {
this.email = email;
} 
}

Output

Rahul
20

Abstraction:

Abstraction in java

Abstraction is the technique of hiding implementation specifics from the user and only displaying functionality.
Data Another definition of abstraction is the process of focusing on an object’s necessary features while discarding its unimportant elements.

An object’s characteristics and actions set apart from other entities of the same type and aid in categorising the objects.

According to the dictionary, the ability to deal with concepts rather than occurrences is known as abstraction. When you think about the situation of email, for instance, complicated aspects like what occurs when you send an email and the protocol your email server uses are concealed from the user.

Therefore, all you need to do to send an email is compose the message, include the recipient’s address, and click “Send.”

Run

public class Main 
{
//ABSTRACTION EXAMPLE CODE for car 
  public static void main (String[]args) 
  {
Car obj = new Car ();
obj.start ();
obj.stop ();
obj.accelerate ();
obj.brake ();
} 
} 
//car 
  abstract class Vehicle 
{
abstract void start ();
abstract void stop ();
abstract void accelerate ();
abstract void brake ();
} 
//car
  class Car extends Vehicle 
{
void start () 
  {
System.out.println ("Car started");
} 
void stop () 
  {
System.out.println ("Car stopped");
} 
void accelerate () 
  {
System.out.println ("Car accelerated");
} 
void brake () 
  {
System.out.println ("Car braked");
} 
}

Output :

Car started
Car stopped
Car accelerated
Car braked

Inheritance :

Inheritance in java

One object can acquire all of a parent object’s properties and actions through the concept of inheritance in Java.
A new class that inherits from a base classes is indicated by the extends keyword. The word “extends” means to make something more functional.
The derived class can add new features its own without altering the base class’s attributes, and it inherits all of the base class’s properties.
In the derived class, protected components can be easily accessed but private members from the base class cannot.

Run
public class Main {
    //INHERITANCE EXAMPLE CODE of animal
    public static void main(String[] args) {
        Animal obj = new Animal();
        obj.eat();
        obj.sleep();
        obj.walk();
       
    }

}
//human
class Animal {
    void eat() {
        System.out.println("Animal is eating");
    }
    void sleep() {
        System.out.println("Animal is sleeping");
    }
    void walk() {
        System.out.println("Animal is walking");
    }
}
//animal
class Human extends Animal {
    void eat() {
        System.out.println("Human is eating");
    }
    void sleep() {
        System.out.println("Human is sleeping");
    }
    void walk() {
        System.out.println("Human is walking");
    }
}

Output :

Animal is eating
Animal is sleeping
Animal is walking

Polymorphism :

Polymorphism in java 

The concept of polymorphism in Java enables us to carry out a single action in various ways. Greek terms poly and morphs are the origins of the word polymorphism. Poly means multiple, and morphs means forms. Polymorphism entails variety of forms.

One of the key components of object-oriented programming is polymorphism. We are able to carry out a single activity in various ways because to polymorphism.

It is crucial to understand that a reference variable is the only method for retrieving an object. Only one type of reference variable is allowed. A reference variable’s type cannot be changed once it has been declared.

Run
public class Main {
    //POLYMORPHISM EXAMPLE CODE of bike
    public static void main(String[] args) {
        Bike obj = new Bike();
        obj.run();
        obj.run(10);
        obj.run(10, 20);
    }

}
//bike
class Bike {
    void run() {
        System.out.println("Bike is running");
    }
    void run(int speed) {
        System.out.println("Bike is running at " + speed + "kmph");
    }
    void run(int speed, int distance) {
        System.out.println("Bike is running at " + speed + "kmph for " + distance + "km");
    }
}

Output :

Bike is running
Bike is running at 10kmph
Bike is running at 10kmph for 20km