JAVA 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.
Code example of class
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
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.
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.”
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.
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
Polymorphism in Java refers to the ability of a class to take on multiple forms. It allows a subclass to provide a specific implementation of a method that is already defined in its superclass. Polymorphism is one of the four fundamental principles of object-oriented programming (OOP), along with encapsulation, inheritance, and abstraction.
There are two types of polymorphism in Java :
- Compile-time Polymorphism (also known as method overloading): This occurs when multiple methods in the same class have the same name but different parameter lists (number or type of parameters). The correct method is determined at compile-time based on the method signature.
class MathOperations { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }
- Run-time Polymorphism (also known as method overriding): This occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The method in the subclass overrides the method in the superclass.
class Animal { void makeSound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override void makeSound() { System.out.println("Dog barks"); } }
Code Example :
// Superclass Animal class Animal { void makeSound() { System.out.println("Animal makes a sound"); } } // Subclass Dog class Dog extends Animal { @Override void makeSound() { System.out.println("Dog barks"); } } // Subclass Cat class Cat extends Animal { @Override void makeSound() { System.out.println("Cat meows"); } } // Main class public class Main { public static void main(String[] args) { Animal animal1 = new Dog(); Animal animal2 = new Cat(); animal1.makeSound(); animal2.makeSound(); } }
Output :
Dog barks Cat meows
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment