You are on page 1of 30

Some basic programmes of

Java
As we know that Java is having some predefined classes in itself and those
predefined classes are inside the Java packages.

In Java language for taking the input from the user , Java program itself have a
predefined class known to be Scanner class. So first we have to import that
package.

So here let us understand this by the following example, in which we just take
the input from the user and than return back the addition, subtraction,
multiplication and division of those inputs…
Java Program - How to take the input from the
user...
import java. util. Scanner; //here we just import the Scanner class (the predefined
class) in which the input from the user method is hidden

class Saurabh {

public static void main (String args[]) // main method which is the
infrastructure to run any Java program

int a,b; // we just declare here the value for 'a' and 'b' will be the
integer one.

Scanner s =new Scanner(System.in); /* now we have created


Output of this program
Now as soon as the user put its value to whom he/she wants to addition,
subtraction,multiply,divide .So at the time of compiling our Java compiler will go
through each functions and operate them and reflect back the desired value to the
user.
Inheritance
Inheritance is the main property of OOPs (Object Oriented Programming
Language) by which we can follow the properties of the parent class into child or
base classes.

And we can again reuse the methods of those parent class into our new classes.
And even you can add new functions,methods to your new classes , by which
runtime polymorphism you can achieved.
Syntax Of Inheritance
The “extends” keyword we use here just to describe in the Java that the new class
which we have created is derived from the existing class.

So here in the following example our Superclass-name is the parent class and
Subclass-name is extending the Superclass-name...
TYPES OF INHERITANCE

There are basically 5 main types of Inheritance which are as follows :-

1) Single Inheritance
2) Multilevel Inheritance
3) Hierarchical Inheritance
4) Multiple Inheritance
5) Hybrid Inheritance

So Let them discuss one by one :-


Single Inheritance
In this type of Inheritance we generally derived any one class from the parent
class , so that new built class also have the properties of the parent class .Let us
see by the following program…

class Birds

void fly() {System.out.println("Flying...");}

class Zoo extends Birds

//So here we have extend the Zoo class into the Bird class
Output of this program
So here in the above program, we just created the main object of the extending
class and as it is been derived from the parent class , so from the main object of
the extending class we can call both the methods...
Multilevel Inheritance
In this type of Inheritance we can generally derived more class from the parent
class ,and even from those derived classes we can derived more classes, so that
new built classes also have the properties of the parent class .Let us see by the
following program...

class Birds

void fly() {System.out.println("Flying...");}

class Pigeon extends Birds

// so here we are extending the Pigeon class into Bird class


Output of this program
So here in the above program, we just created the main object of the extending
classes and as it is been derived from the parent class , so from the main object of
the extending class we can call other methods from that same object…
Hierarchical Inheritance
In this type of Inheritance we can generally derived more classes from the parent
class , so that new built classes also have the properties of the parent class .Let
us see by the following program...

class Birds

void fly() {System.out.println("Flying...");}

class Pigeon extends Birds

// so here we are extending the Pigeon class into Bird class


Output of this program

So here as we have created the main object of extending class (Zoo) and from
that object we can very well call the method of parent class (Bird) ,but as we know
that the (Zoo) class is derived from the (Bird) class, so it only defines the method
of (Bird) class not of (Pigeon) class...
Java Does not support Multiple Inheritance

class Birds {

void fly() {System.out.println("Flying...");}

}class Pigeon // So here we have taken new class Pigeon

void eat() {System.out.println("Eating...");}

class Zoo extends Birds ,Pigeon; // So here we are extending both Birds and
Pigeon class
Output of this program

So here in the above example , as we just want to inherit the properties of


(Pigeon) and (Birds) class into the (Zoo) class, So our Java compiler is giving us
the error because Java does not support the Multiple Inheritance ...
Hybrid Inheritance
If we are having (Bird) class which is extending (Pigeon) class , and our (Pigeon)
class is extending (Peacock) class , (Peacock) class is extending (Koyal) class
and (Koyal) class is extending to (Bird) class , so from the main method of (Koyal)
class we can define the method of every other class. Let us understand this by
diagrammatically...
Let us take an example
Here through a program we will first take a class (Bird) which used to extend the
(Pigeon) class , and our (Pigeon) class used to extend (Peacock) class ,
(Peacock) class used to extend (Koyal) class and (Koyal) class used to extend the
(Bird) class

class Birds

void fly() {System.out.println("Flying...");}

class Peacock extends Birds


Output of this program

So here our main method of (Koyal) class is deriving all other class methods
because anyhow all the classes are derived from its base class which is (Bird)
class...
Overriding
Subclass or child class provide a specific implementation of a method that is
already provided, by one of its super- classes or parent classes .When subclass
have the same name ,same parameters and same return type method of
super –class method is called Overriding.

class Birdss {

public void fly(){System.out.println("Flying.... ");

class Peacock extends Birdss{


Output of this Program
Overloading

Overloading in java same type of name but there parameters different type
number of parameters ,different type of parameter,or both).This method is called
of method overload and this feature called method overloading…

public class Sum{

public int sum(int x, int y) // Overloaded two sum type int parameters

return(x+y);

public int sum(int x,int y,int z) //Overloaded three sum type int parameters
Output of this Program
Polymorphism

Polymorphism consist of two word ‘Poly’ which means ‘many’ and ‘Morphism’
means ‘work’ . So basically in Java the Polymorphism means from the same
method if we are calling many functions.
Super Keyword

Here in the Java ‘Super’ keyword is used to give reference of its own class as well
as its parent class. The function which we implement inside the ‘Super’ keyword
from that function we can call both the methods of its own class as well as of its
Parent class

package thre;

class Birds{

void eat () {System.out.println("Eating...");} // here in just eat function we


want to print Eating...

}
Output of this program
So here we just from one method call both the parent class and its own class or
child class...
Final Keyword

Final keyword in Java we use for assigning final value to any variable , method
and class. Let we understand this by the following example…

package thre;

class Birds{

final void eat () {System.out.println("Eating...");}

}
Output of this Program
So here as we have assigned the final value to the method variable so we cant
able to access the other function

You might also like