Posts

Inheritance in java

               Inheritance is the process of getting properties from base class to child class. A simple implementation of inheritance is given as follows. Base class : Fruit Child class : Apple Two functions: shape() taste() create a object for child class Apple. Access the two functions using this child class object. // Parent class class Fruit {     void shape() {         System.out.println("Apple is round");     } } // Child class class Apple extends Fruit {     void taste() {         System.out.println("Apple is sweet");     } } public class sampleinheritance {     public static void main(String[] args) {         Apple a1 = new Apple();         a1.shape();   // Method inherited from ...

How to implement fundamental OOPs concept (Polymorphism) in java??

       Poly means many. Polymorphism in java defines a single super class and that can be used for any sub classes. Even the class definition can be overridden and the methods can be overloaded with another definition. The method overriding and method overloading java implementations are given below…. Java Program to implement Method overriding:               Overriding is the process of redefining a function. This program overrides a function definition. Steps: ·        Create a super class “Fruit” with a member function “color()” with a output statement. ·        Next, create a sub class “Apple” extends from super class “Fruit” with the function “color()”. Override the function with another definition. ·        Write a class “samplePoly” with main function. ·        Fruit m...

How to implement fundamental OOPs concept (Encapsulation) in java???

                                           Encapsulation is wrapping up of data and member functions into a single unit🎁. The single unit is defined as a class.   ·        Create a class “Customer”. ·        Add two members as private. One is name. another one is id. ·        Write four member functions. Two for getting name,id. Two for setting the name and id. ·        Create a main class as”Main9” and save this class as “Main9.java” ·        Define a public static void main() with String[] Args. ·        Create an object for Customer class. ·        Call the set functions to set the value. ·        Using print statements, prin...

How to implement fundamental concepts of OOPs in java – Abstraction:

            Fundamental concepts of java includes the following. ·        Abstraction ·        Inheritance ·        Polymorphism ·        Encapsulation Each one is coded with examples. Abstraction:               It is the concept of hiding the internal information and displays the necessary details of object. First, create an abstract class vehicle with one abstract method without any definition. Define a display() method with a printing statement. Create two child classes for vehicle class as Car and Bus. Write the definition for abstract method info() Now, create a class “sampleabstract” which has the main method. Create the objects for Car and Bus classes. Call the functions by objects. abstract class vehicle {     // Abstract ...

How to implement OOPs concepts in java???

     ‘OOP’-Object Oriented Programming. It is the way of creating a paradigm in efficient manner. It also secures the code. In java, this is important part of programming. What are the core concepts of java? The   core concepts of java is listed below… Class : Class   declares data and member functions. It is a blueprint of object. Object : It is a real world entity. Eg: ‘Apple’ Abstraction : It hides the implementation. Encapsulation : It wraps the data and functions into a single unit. Polymorphism: Same member function have more than one definition. Inheritance : It creates parent-child relationship. How to implement OOPs concepts in java? ·        First, create a class “fruit”.   Add an attribute   “fname” and create an method “display()”. ·        Next, create a public class “fruitcls” and save this file as “fruitcls.java” ·        Add t...

Java fundamental programs to practice: Looping

              A set of statements are executed until a condition is met. This concept is called looping. There are three types of loops used in java. 1.        While loop 2.        Do-while loop 3.        For loop Java program to illustrate while loop:               While loop deals with a condition followed by set of statements. The syntax is given below. While(condition) { Statements…….. } Eg: java program to display the odd numbers upto ‘n’.   import java.util.Scanner; public class whileEg {   public static void main(String args[])   {    Scanner scanner = new Scanner(System.in);    System.out.print("Enter the number:");    int n= scanner.nextInt();    int i=1;    while(n>0)    {...

Java fundamental programs to practice: Conditional statement- switch:

               Switch is one of the conditional statements which is useful in selecting one among in multiple options. Syntax: Switch(Option) {   Case 1: statements…..                break; Case 2: statements…..                break; --------------------------- --------------------------- Case n: statements…..                break; } Example for switch conditional statement is given below. Java Program to print the month using switch case: ·        Create a class SwitchMonth with main() function. ·        Get the input from user. Using the switch statement,pass the input. ·        Based on the input given, case stateme...