Avatar of chima
chima
 asked on

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
ProgrammingJava

Avatar of undefined
Last Comment
chima

8/22/2022 - Mon
gurpsbassi

U need to share the m3 method in the superclass.
chima

ASKER
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

dpearson

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
chima

ASKER
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

dpearson

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
chima

ASKER
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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
dpearson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
chima

ASKER
thanks