Adv Java
Programming

1. What Is the Java Technology 2. Points to keep in mind for pyramid. 3. Java Pyramid Program on type 1 4. Java Pyramid Program on type 2 5. Java Pyramid Programs on Type 3 6. Java Pyramid Programs on type 4 7. Java Pyramid Programs on type 5 8. Classes and Objects 9. Recursive functions 10. Constructors in Java 11. Method Overloading 12. Constructor Overloading 13. Inheritance in Java 14. Interface in Java 15. Method Overriding and Hiding Method 16. The final keyword 17. Exception Handling 18. Properties, System & Runtime Class in Java 19. Building Java GUI 20. Layout Managers 21. Menu Bar 22. Applets 23. Threading 24. Java Netwoking 25. Programs for Science and Engineer 26. Prg to demonstrate pre and post increment 27. Prg to convert Farenheit to Celsius temperature 28. Prg to convert kilogram to different units 29. Prg to calculate surface area and volume of sphere 30. Prg to calculate frequency 31. Prg to determine co-ordinate points 32. Prg to determine Equilateral, Isosceles or Scalene triangle 33. Prg to demonstrate conditional operator 34. Prg to calculate Final and initial interest amount 35. Prg to separate entered number 36. Prg to examine while loops and conditions 37. Prg to calculate number of year 38. Prg to find Armstrong number or not 39. Prg to find all Armstrong number 40. Prg to find limited Armstrong number 41. Prg to find Armstrong number using user defined function 42. Prg to find frequency of student 43. Prg to find maximum and minimum number 44. Prg to print number triangle 45. Prg to print vertical number triangle 46. Prg to print number triangle 47. Prg to print binary right angle triangle 48. Prg to print * triangle 49. Prg to to print rhombus 50. Prg to print entered number triangle 51. Prg to to find nCr and nPr 52. Prg to enter an element in array 53. Prg to sort number using array 54. Prg to find the position of element in array 55. Prg to find the position of element in array using boolean flag 56. Prg to add one extra element in an array 57. Prg to find the equation of line 58. Prg to find the common element from two array 59. Prg to find the highest scorer 60. Prg to create a square of number 61. Prg to create Pascal triangle 62. Prg to subtraction, multiplication and division using methods and classes 63. Prg to determine salary grade 64. Prg for complex number 65. Prg to calculate area 66. Prg for Greatest Common Divisior 67. Prg for displaying studenta mark sheet 68. Prg for demonstrating inheritance 69. Prg to calculate area of triangle 70. Prg for calculating volume of sphere or hemisphere 71. Prg for demonstrate classes 72. Prg for employee details 73. Prg for demonstrating super and base class 74. Prg for Bank Account 75. Prg for professor and student details 76. Prg for Addition, Multiplication and finding Transpose of two matrix 77. Prg for counting number of words in a string 78. Prg to count number of vowels and consonants in a string 79. Prg to fine number of upper case, lower case, white spaces and digit in a string 80. Prg to fine number of upper case, lower case, white spaces and digit in a string without inbuilt function 81. Prg to arrange a string in alphabetic order 82. Prg to count the number of character occurrence in a string 83. Prg to find the string length and character position 84. Prg for converting first character of word into upper case 85. Prg for demonstrating vectors for manipulating records 86. Prg for manupulting items record using vectors 87. Prg for bank transaction 88. Prg for counting number of days between any two different date 89. Prg for exception handling 90. Prg for throwing exception 91. Prg for exception 92. Prg to throw exception when number is invalid 93. Prg for renaming the current thread 94. Prg for demonstrating strat method of thread 95. Prg to demonstrate sleep and and join method of thread 96. Prg to demonstrate yield method of thread 97. Prg for demonstrating thread in class inheritance 98. Threading program using runnable interface 99. Thread program using sleep 100. Java Applet program for creating fancy round eye 101. Java Applet program for creating fancy face 102. Java Applet program for drawing string 103. Java Applet program for creating fancy face 104. Java Applet program for displaying fancy fonts 105. Java Applet program for drawing and filling rectangle 106. Java Applet program for creating house 107. Java Applet program to show lifecycle of applet 108. Java Applet program for creating polygon 109. Java Applet program for creating rectangle 110. Java Applet program for drawing line 111. Java Applet program for getting applet width and height 112. Interview Question & Answer part 1 113. Interview Question & Answer part 2 114. Interview Question & Answer part 3 115. Interview Question & Answer part 4 116. Interview Question & Answer part 5 117. Interview Question & Answer part 6 118. Interview Question & Answer part 7 119. Interview Question & Answer part 8 120. Interview Question & Answer part 9 121. Interview Question & Answer part 10 122. Interview Question & Answer part 11
Pr.Pg Next Pg

Inheritance in Java tutorials

  • In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.

  • Inheritance is a compile-time mechanism in Java that allows you to extend a class called the base class or superclass with another class called the derived class or subclass.

  • In Java, inheritance is used for two purposes:

    1. Class Inheritance

    2. Interface Inheritance

  1. Class Inheritance: Create a new class as an extension of another class, primarily for the purpose of code reuse. That is, the derived class inherits the public methods and public data of the base class. Java only allows a class to have one immediate base class, i.e., single class inheritance.

 

  1. Interface Inheritance: create a new class to implement the methods defined as part of an interface for the purpose of subtyping. That is a class that implements an interface “conforms to” or is constrained by the type of the interface. Java supports multiple interface inheritance.

 

  • In Java, these two kinds of inheritance are made distinct by using different language syntax.

  • For class inheritance, Java uses the keyword extends and for interface inheritance Java uses the keyword implements.

 

Single inheritance

  • When one class is derived from one base class it is called as single inheritance.

  • Java supports single inheritance.

 

Syntax for class inheritance:

<modifier> class <class_name> extends <base_class_name>

{

//Derived class methods

}

 

Syntax of interface inheritance:

<modifier> class <class_name> implements <interface_name>

{

//Class provides the implementation for the methods which are declared in interface

}


 

Example: Java program for demonstrating inheritance

 Sam Madam

//Program name Inheritance.java

//Java program for demonstrating inheritance

class A //superclass

{

int num1; //member of superclass

int num2; //member of superclass

void setVal(int no1, int no2) //method of superclass

{

num1 = no1;

num2 = no2;

}

}

class B extends A //subclass B

{

int multi; //member of subclass

void mul() //method of subclass

{

multi = num1*num2; //accessing member of superclass from subclass

}

}

public class Inheritance {

public static void main(String[] args) {

B objb = new B();

objb.setVal(10,6); //calling superclass method throgh subclass object

objb.mul();

System.out.println("Multiplication is " + objb.multi);

 

}

}

Output

Multiplication is 60

 


 

Example: Java program for demonstrating inheritance

 Sam Madam

//Program name InheritanceDemo.java

// Java program for demonstrating inheritance

import java.util.Scanner;

class AA //superclass

{

int num1; //member of superclass

int num2; //member of superclass

void setVal(int no1, int no2) //method of superclass

{

num1 = no1;

num2 = no2;

}

}

class BB extends AA //subclass B

{

int multi,div,add,sub; //member of subclass

void mul() //method of subclass

{

multi = num1*num2; //accessing member of superclass from subclass

}

void division()

{

div=num1/num2; //accessing member of superclass from subclass

}

void addition()

{

add=num1+num2; //accessing member of superclass from subclass

}

void subtraction()

{

sub=num1-num2; //accessing member of superclass from subclass

}

}

public class InheritanceDemo {

public static void main(String[] args) {

BB objb = new BB();

int i,j;

Scanner scan =new Scanner(System.in);

System.out.print("Enter number1 : ");

i=scan.nextInt(); //asking user defined number1

System.out.print("Enter number2 : ");

j=scan.nextInt(); //asking user defined number2

objb.setVal(i,j); //calling superclass method throgh subclass object

objb.mul();

objb.addition();

objb.subtraction();

objb.division();

System.out.println("Addition is :"+objb.add);

System.out.println("Subtraction is :"+objb.sub);

System.out.println("Multiplication is :"+objb.multi);

System.out.println("Division is :"+objb.div);

 

}

}

Output

Enter number1 : 4

Enter number2 : 4

Addition is :8

Subtraction is :0

Multiplication is :16

Division is :1

 

 

Example: Java program for demonstrating Constructor channing

 Sam Madam

//Program name ConstructorChaining.java

// Java program for demonstrating Constructor channing

class DemoChild extends ConstructorChaining

{

int num3;

int num4;

DemoChild()

{

num3 = 3;

num4 = 4;

System.out.println("Inside the Constructor of Child");

}

public void display()

{

System.out.println("Number1 === "+num1);

System.out.println("Number2 === "+num2);

System.out.println("Number1 === "+num3);

System.out.println("Number2 === "+num4);

}

}

class ConstructorChaining

{

int num1;

int num2;

ConstructorChaining()

{

num1 = 1;

num2 = 2;

System.out.println("Inside First Parent Constructor");

}

ConstructorChaining(int a)

{

num1 = a;

System.out.println("Inside Second Parent Constructor");

System.out.println("This is called due to super :"+a);

}

public void display()

{

System.out.println("Number1 === "+num1);

System.out.println("Number2 === "+num2);

}

public static void main(String args[])

{

DemoChild d1 = new DemoChild();

d1.display();

}

}

Output

Inside First Parent Constructor

Inside the Constructor of Child

Number1 === 1

Number2 === 2

Number1 === 3

Number2 === 4

 

Note: Now just make one change in the above program as below and notice the output.

  • You may observe the constructor of the parent class ConstructorChaining is overridden.

  • What is you want to call the overridden constructor ConstructorChaining(int a) instead of the default constructor ConstructorChaining() when your child object is created ?

  • In such cases you can use the keyword “super” to call overridden constructors of the parent class.

 

 

Example: Java program for demonstrating Constructor channing

 Sam Madam

//Program name ConstructorChaining.java

//Java program for demonstrating Constructor channing

class DemoChild extends ConstructorChaining

{

int num3;

int num4;

DemoChild()

{

super(5); // to call overridden constructors of the parent class

num3 = 3;

num4 = 4;

System.out.println("Inside the Constructor of Child");

}

public void display()

{

System.out.println("Number1 === "+num1);

System.out.println("Number2 === "+num2);

System.out.println("Number1 === "+num3);

System.out.println("Number2 === "+num4);

}

}

class ConstructorChaining

{

int num1;

int num2;

ConstructorChaining()

{

num1 = 1;

num2 = 2;

System.out.println("Inside First Parent Constructor");

}

ConstructorChaining(int a)

{

num1 = a;

System.out.println("Inside Second Parent Constructor");

System.out.println("This is called due to super :"+a);

}

public void display()

{

System.out.println("Number1 === "+num1);

System.out.println("Number2 === "+num2);

}

public static void main(String args[])

{

DemoChild d1 = new DemoChild();

d1.display();

}

}

Output

Inside Second Parent Constructor

This is called due to super :5

Inside the Constructor of Child

Number1 === 5

Number2 === 0

Number1 === 3

Number2 === 4

 

 

Multilevel Inheritance

  • Class can derive from other class.

  • Class can be derived from other classes as well.

  • When we derive classes from derived classes we get levels of inheritances which is called as multilevel inheritance.

 

 

Example: Java program to create three classes and perform multilevel inheritance among them

 Sam Madam

// Java program to create three classes and perform multilevel inheritance among them

import java.util.Scanner;

class student

{

String name;

int rno;

void input()

{

System.out.print("Enter the name of the student : ");

Scanner scanner=new Scanner(System.in);

name =scanner.nextLine();

System.out.print("Enter the roll no of the student : ") ;

rno=scanner.nextInt();

}

void displays()

{

System.out.println("Name of the student : "+name);

System.out.println("Roll no : "+rno);

}

}

 

 

class degree extends student

{

String deg;

void getdegree()

{

System.out.print("Enter the degree in which the student studied : ");

Scanner scan=new Scanner(System.in);

deg=scan.nextLine();

}

void displayd()

{

System.out.println("Degree in which the student studied : "+deg);

}

}

 

class post_deg extends degree

{

String degr;

void getpdegree()

{

System.out.print("Enter the post graduation degree in which the student studies : ");

Scanner scan=new Scanner(System.in);

degr=scan.nextLine();

}

void displayp()

{

System.out.println("Post graduation Degree in which the student studies : "+degr);

}

}

 

 

public class MultiLeve extends post_deg

{

public static void main(String[] args)

{

post_deg p1=new post_deg();

p1.input();

p1.getdegree();

p1.getpdegree();

p1.displays();

p1.displayd();

p1.displayp();

 

}

}

Output

Enter the name of the student : Jack

Enter the roll no of the student : 13

Enter the degree in which the student studied : BS

Enter the post graduation degree in which the student studies : MS

Name of the student : Jack

Roll no : 13

Degree in which the student studied : BS

Post graduation Degree in which the student studies : MS

Pr.Pg border                                              Next Pg