Sunday, December 2, 2018

Java Inheritance Programs

In this post we will learn inheritance programs in java for freshers and students. These inheritance programs makes you confident when you attending technical interview and written test. Let's see some of the most important inheritance programs.

1.Java simple Inheritance Example

class A
{
int i,j;
void show();
{
System.out.println("i and j:"+i+ " " +j);
}
class B extends A
{
int k;
void display()
{
System.out.println("k:"+k);
}
void sum()
{
System.out.println("i+j+k:"+(i+j+k));
}
class InheritanceExample
{
public static void main(String args[])
{
A sup=new A();
B sub=new B();
sup.i=10;
sup.j=20;
sup.show();
sub.i=7;
sub.j=8;
sub.k=9;
sub.show();
sub.display();
sub.sum();
}
}
Output:
i and j: 10 20
i and j: 7 8
k: 9
i+j+k=24

2. Java Program of method overloading

class Calculation {
void sum(int a,int b){
System.out.println(a+b);
}
void sum(int a,int b,int c){
System.out.println(a+b+c);}
public static void main(String args[]){
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
}
}

Output:
30
40


3. Java Program of Method Overriding

class Bank{
int getRateOfInterest(){
return 0;}
}
class SBI extends Bank{
int getRateOfInterest()
{
 return 7;}
}
class ICICI extends Bank{
int getRateOfInterest(){
return 8;}
}
class AXIS extends Bank{
int getRateOfInterest(){
return 9;}
}
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}


Output:

SBI Rate of Interest: 7
ICICI Rate of Interest: 8
AXIS Rate of Interest:9

4. Java Program using super keyword at method level

class Test
{
void print()
{
System.out.println(" WELCOME");
}
}
class Demo extends Test
{
void print()
{
System.out.println("WELCOME TO JAVA");
}
void display()
{
print();//it invoke sub class print() method
super.print();//it invoke super class print() method
}
public static void main(String args[])
{
Demo d=new Demo();
d.display();
}
}

Output:
Javac Demo.java
java Demo
WELCOME TO JAVA
WELCOME

5.java program super at constructor level

class Test
{
Test()
{
System.out.println("this is default constructor");
}
}
class Demo extends Test
{
Demo()
{
System.out.println("this is parameterized constuctor");
}
}
class SuperConstructor
{
public static void main(String args[])
{
Demo d=new Demo();
}
}

Output:
 javac SuperConstructor.java
java SuperConstructor 

this is default constructor
this is parameterized constructor

6 Java program parameterized constructor 

class Test
{
int a;
Test(int a)
{
this.a=a;
}
}
class Demo extends Test
{
int a;
Demo(int i,int j)
{
super(i);
i=j;
}
void display()
{
System.out.println("sub class="+a);
System.out.println("sub class a="+super.a);
}
}
class SuperDemo
{
public static void main(String args[])
{
Demo d=new Demo(10,20);
d.display();
}
}

Output:
javac SuperDemo.java
java SuperDemo
sub class=20
sub class a=10 

7. Java Program for Polymorphism

class Shape
{
 void draw()
{
System.out.println("Drawing  Shape");
}
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("Drawing Rectangle");
}
}
class Triangle extends Shape
{
void draw()
{
System.out.println("Drawing Triangle");
}
}
class Poly
{
public static void main(String args[])
{
Shape s=new Shape();
s.draw();
Rectangle r=new Rectangle();
s=r;
s.draw();
Triangle t=new Triangle();
s=t;
s.draw();
}
}
Output:
Drawing Shape
Drawing Rectangle
Drawing Triangle


8. Java Program for Abstract Example

abstract class Shape
{
 abstract void draw();
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("Drawing Rectangle");
}
}
class Triangle extends Shape
{
void draw()
{
System.out.println("Drawing Triangle");
}
}
class Poly2
{
public static void main(String args[])
{
Shape s;
Rectangle r=new Rectangle();
s=r;
s.draw();
Triangle t=new Triangle();
s=t;
s.draw();

Output:
Drawing Rectangle
Drawing Triangle

9. Java Program for final class example

final class A
{
void display()
{
System.out.println("A display()");
}
}
class B
{
void display()
{
System.out.println("B display()");
}
}
class FinalClassTest
{
public static void main(String args[])
{
A a=new A();
B b=new B();
a.display();
b.display();
}
}
Output:
A display()
B display()

10 Java Program for final method example

class A
{
final void display()
{
System.out.println(" A display()");
}
}
class B extends A
{
void show()
{
System.out.println("B display()");
}
}
class FinalMethod
{
public static void main(String args[])
{
B b=new B();
b.display();
b.show();
}
}
Output;
A display()
B display()

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...