We help IT Professionals succeed at work.

Java Inheritance super keyword use

chima
chima asked
on
272 Views
Last Modified: 2017-04-14
Hello,
Without showing the entire code, I hope to explain what I'm expecting to get answered.
I have initialized a and b, in the parent class.  In the sub class "classB" it is extended by classA.
In classB, as shown below, I can re-initialize a and b (at least I think it is the a and b from classA), if they are from classA.
 I would not want to re-initialize a and b in classB, but I have no choice (error occurs if I do not initialize them).  what I would like to see from super.m3(), in the println statement; are the values given to a and b within the parent class (classA).
Might you know how to do this;
class classB extends classA{
	
		public int c;  public int d;
		public void m3(){
			super.b=4;
			super.m3();
			super.a=5;
			
			System.out.println("This is classB super m3" +  " " + "a & b are initilized in ca: " + a + b + c + d + "\n");		
		}

Open in new window


thanks
Comment
Watch Question

Top Expert 2015

Commented:
U need to share the m3 method in the superclass.

Author

Commented:
Parent class is;
class classA{	
		public int a; 	public int b;  
		public void m1(){
			System.out.println("This is classA m1" +  " " + a + b + "\n");
		}
		
		public void m3(){
			//super.m3();
			System.out.println("This is classA m3:" + " " + a + " " + b + "\n");			
		}
	}

Open in new window

CERTIFIED EXPERT

Commented:
If you don't initialize them you should be fine to still call m3() and they will print out as having value 0 - which is the default value for an int that you have not explicitly set.

		public void m3() {
                        // Don't initialize
			//super.b = 4;
			//super.a = 5;

			super.m3();

			System.out.println("This is classB super m3" + " " + "a & b are initilized in ca: " + a + b + c + d + "\n");
		}

Open in new window


You should get:

This is classA m3: 0 0

This is classB super m3 a & b are initilized in ca: 0000

As the output.

Doug

Author

Commented:
Doug, correct, if I do not initialize a & b, I do get 0 0.  The situation is that I do initialize a & b, and when I get to classB extended by cla
classA, I want to display a & b of the parent class; which should be a=1, and b=2;

	class classB extends classA{
	
		public int c;  public int d;
		public void m3(){
			//super.a=8;			
			//super.b=9;
			super.m3();

The code below is after public static void main(String args[])
                         ca.a=1;
			ca.b=2;
			ca.m1(); // 1 2
			ca.m3(); // 1 2
			
			cb.c=3;
			cb.d=4;
			cb.m3();

Open in new window

CERTIFIED EXPERT

Commented:
You need to give us an example of the problem and a clear statement of what you're expecting to happen.  I'm still a bit confused.

E.g. Here is a full working example that you can run:

public class ClassTest {
	public static void main(String[] args) {
		classB ca = new classB() ;
		ca.a = 1 ;
		ca.b = 2 ;
		ca.m3();
	}

	public static class classA{
		public int a; 	public int b;
		public void m1(){
			System.out.println("This is classA m1" +  " " + a + b + "\n");
		}

		public void m3(){
			//super.m3();
			System.out.println("This is classA m3:" + " " + a + " " + b + "\n");
		}
	}

	public static class classB extends classA {

		public int c;
		public int d;

		public void m3() {
			//super.b = 4;
			//super.a = 5;
			super.m3();

			System.out.println("This is classB super m3" + " " + "a & b are initilized in ca: " + a + b + c + d + "\n");
		}
	}
}

Open in new window


This code outputs:

This is classA m3: 1 2

This is classB super m3 a & b are initilized in ca: 1200

Is this the code you want to run?  Is this the output you expect?

Please modify this code (i.e. a full working example) and explain what part of this isn't what you're looking for and we can probably help explain it to you.

Doug

Author

Commented:
Doug, I apologize.  Here is what I just figured out, and it is probably what you have been telling me.
This is the entire code.  What I learned is that public  int a, b, c, and b declared within the Class but not inside the method, cannot be displayed from the super.method.  They can be displayed within its own method.  To display the variable from a super.method, the variable has to be initialized within the method.  The super.method can be overloaded.
Let me know if I did not explain this to your understanding;

	class classA{	
		public int a; 	public int b;  // Can't get to these variables
		public void m1(){
			a=14; b=15; 
			System.out.println("This is classA m1: " +  " " + a + " "+ b + "\n");
		}
		
		public void m3(){
			a=12; b=13;
			//classA.this.a;
			//System.out.println("This is classA m3:" + " " + a + " " + b + "\n");			
		}
		
		// Overloading
		public void m3(int aa, int bb){
			a=aa; b=bb;
			System.out.println("This is classA m3:" + " " + a + " " + b + "\n");			
		}
	}

	class classB extends classA{	
		public int c;  public int d;
		public void m3(){
			//super.a=8;			
			//super.b=9;
			//this.a;
			
			super.m3();
			System.out.println("This is classB super m3" +  " " + "a & b are initilized in ca: " + this.a + " " + b + " " + c +" "+ d + "\n");		
		}

		public void m4(){
			//super.m3();
			//System.out.println("This is classB m4 SUPER m3" + " " + a + " " + b + "\n");	

		}
	}
	
	// Multi Inheritance
	class classTest extends classB{
		public void m1(){
			super.m1();
			//System.out.println("This is classTest m1,m2,m3,m4" + " " + super.a + this.b + this.c + this.d + "\n");
		}
		
		public void m5(){
			//System.out.println("This is classTest m5" + " " + classTest.this.a + b + c + d + "\n");
		}
	}
	
	public class Inheritance {	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
			classA ca=new classA();
			classB cb=new classB();
			classTest ct=new classTest();
			
			ca.a=1;
			ca.b=2;
			ca.m1(); // 1 2
			ca.m3(); // 1 2
			ca.m3(20,30);
			
			cb.c=3;
			cb.d=4;
			cb.m3();
			
			ct.m1();
			//ct.clasTest();			
	}

}

Open in new window

CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
thanks