- Inheritance means getting properties from one class object to another.
- So in sub class we can use or access super class variables and method : re usability
- There are some interesting and important points to discuss about inheritance
- In major java interviews and java online test on core java there is more chance of getting programming questions from inheritance
- So let us see some java test questions. java test online to practice
- If you want explanations then visit below topic
- Top 16 Java Inheritance Interview questions for freshers and experienced
5 Points to know about inheritance:
Program #1: Java test on inheritance:
- Creating object for super class and calling super class methods and accessing super class variables.
- package interviewprograms.instanceofjava;
- public class Super{
- int a, int b;
- void show(){
- System.out,println("Inside Show method");
- }
- }
- package interviewprograms.instanceofjava;
- public class Sub extends Super{
- int a, int b;
- public static void main(String[] args) {
- Super obj= new Super();
- obj.a=10;
- System.out,println(obj.a);
- obj.show();
- }
- }
Program #2: Core Java online test on inheritance:
- Creating object for sub class and calling sub class methods and accessing sub class variables.
- package interviewprograms.instanceofjava;
- public class Super{
- int a, int b;
- void show(){
- System.out,println("Inside Show method");
- }
- }
- package interviewprograms.instanceofjava;
- public class Sub extends Super{
- int x;
- void show(){
- System.out,println("Inside Sub class Show method");
- }
- public static void main(String[] args) {
- Sub obj= new Sub ();
- obj.x=10;
- System.out,println(obj.x);
- obj.show();
- }
- }
Program #3: core java online test for beginners and experienced on inheritance
- Creating object for sub class accessing super class members and sub class members.
- package interviewprograms.instanceofjava;
- public class Super{
- int a, int b;
- void show(){
- System.out,println("Inside Show method");
- }
- void print(){
- System.out,println("Inside super class print method");
- }
- }
- package interviewprograms.instanceofjava;
- public class Sub extends Super{
- int x;
- void show(){
- System.out,println("Inside Sub class Show method");
- }
- void msg(){
- System.out,println("Inside Sub class msg method");
- }
- public static void main(String[] args) {
- Sub obj= new Sub ();
- obj.x=10;
- System.out,println(obj.x);
- obj.show();
- obj.print();
- }
- }
Program #4: core java online test for beginners and experienced on inheritance
- Creating object for sub class and assigning to super class reference.
- package interviewprograms.instanceofjava;
- public class Super{
- int a, int b;
- void show(){
- System.out,println("Inside Show method");
- }
- void print(){
- System.out,println("Inside super class print method");
- }
- }
Program #5: core java online test for beginners and experienced on inheritance
- Creating object for sub class and assigning to super class reference and calling sub class method.
- package interviewprograms.instanceofjava;
- public class Super{
- int a, int b;
- void show(){
- System.out,println("Inside Show method");
- }
- void print(){
- System.out,println("Inside super class print method");
- }
- }
- package interviewprograms.instanceofjava;
- public class Sub extends Super{
- int x;
- void show(){
- System.out,println("Inside Sub class Show method");
- }
- void msg(){
- System.out,println("Inside Sub class msg method");
- }
- public static void main(String[] args) {
- Super obj= new Sub ();
- obj.msg();
- }
- }
EnJoY LeArNinG WitH Us...
No comments