Thursday 17 March 2016

How to create hierarical level inheritance in java

3.Hierarchical level Inheritance:-
 When more than two classes using only one class properties is called hierarchical level.


class GrandFather

{

public void property1()

{

System.out.println("Full property of Grandfather");

}
}
class Father extends GrandFather
{
public void property2()
{
System.out.println("Property of house");
}
}
class Son extends GrandFather
{
public void property3()
{
System.out.println("Property of car");
}
}
class Main
{
public static void main(String args[])
{
Father f=new Father();
f.property2();
f.property1();

Son s=new Son();
s.property3();
s.property1();
}
}

OUTPUT:-
C:\Users\Satyendra\Desktop>javac Main.java
C:\Users\Satyendra\Desktop>java Main
Property of house
Full property of Grandfather
Property of car
Full property of Grandfather

How to create multi level inheritance in java

2.Multi level Inheritance:-
When more than two classes we use extends from one to another in a chain process.


class Mobile

{

public void nokia()

{

System.out.println("Phone is ringing");
}
}
class Laptop extends Mobile
{
public void acer()
{
System.out.println("Beautiful laptop");
}
}
public class Main extends Laptop
{
public void connect()
{
System.out.println("It connects the both classes");
}
public static void main(String args[])
{
Main m = new Main();
m.nokia();
m.acer();
m.connect();
}
}

OUTPUT:-
C:\Users\Satyendra\Desktop>javac Main.java
C:\Users\Satyendra\Desktop>java Main
Phone is ringing
Beautiful laptop
It connects the both classes

Tuesday 15 March 2016

How to create single level inheritance in java



Inheritance:-Inheritance is the process by which one object acquires the properties of another object.

There are 3 types of inheritance in java
  1. Single level
  2. Multi level
  3. Hierarchical level 
1. Single level Inheritance:-
 When class uses another class then we use extends or when class uses inheritance of another class then we use implements.

class Inher{
void mobile(){
System.out.println("Samsung");
}
}
class Main extends Inher{
void laptop(){
System.out.println("Acer");
}
public static void main(String args[]){
Main m=new Main();
m.laptop();
m.mobile();
}
}

OUTPUT:-
C:\Users\Satyendra\Desktop>javac Main.java
C:\Users\Satyendra\Desktop>java Main
Acer
Samsung

Monday 14 March 2016

How to create method overloading in java


Method overloading:-When multiple methods in a class with same name but different parameters is called a method overloading.

class Over{
void sum(int a,int b){
System.out.println(a+b);
}
void sum(double a,double b){
System.out.println(a+b);
}
public static void main(String args[]){
Over obj=new Over();
obj.sum(11.5,11.5);
obj.sum(5,5);
}
}

OUTPUT:-
C:\Users\Satyendra\Desktop>javac 1.java
C:\Users\Satyendra\Desktop>java Over
23.0

10