Java Inheritance super keyword use

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
chimaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
U need to share the m3 method in the superclass.
0
chimaAuthor 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

0
dpearsonCommented:
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
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

chimaAuthor 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

0
dpearsonCommented:
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
0
chimaAuthor 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

0
dpearsonCommented:
Yes it sounds like you've figured this out for yourself.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chimaAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.