Friday, 22 April 2016

How to use final keyword in java



Final keyword:-If we use final before class then we cannot extend the class.
We cannot reuse the method by using final keyword.

class Test{
final void show()
{
System.out.println("Hello Friends");
}
}
class Main extends Test{
void show();
public static void main(String args[]){
final int x=10;
System.out.println("Before swapping :- "+x);
int y=20;
x=y;
System.out.println("After swapping :- "+x);
}
}

OUTPUT:-
Compile Time Error

Thursday, 7 April 2016

How to create super keyword in java



Super keyword:- Super keyword is used for accessing super class methods and variables. And for calling super class constructors.

class A{
void show(int x){
System.out.println("Parent show");
}
}
class Main extends A{
void show(int x){
super.show(20);
System.out.println("Child show");
}
public static void main(String args[]){
Main m=new Main();
m.show(10);
}
}

OUTPUT:-
C:\Users\Satyendra\Desktop>javac main.java
C:\Users\Satyendra\Desktop>java Main
Parent show
Child show

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