Solved

Java Inheritance super keyword use

Posted on 2017-04-14
8
73 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
0
Comment
Question by:chima
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:gurpsbassi
ID: 42093254
U need to share the m3 method in the superclass.
0
 

Author Comment

by:chima
ID: 42093343
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
 
LVL 27

Expert Comment

by:dpearson
ID: 42093371
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:chima
ID: 42093516
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
 
LVL 27

Expert Comment

by:dpearson
ID: 42093536
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
 

Author Comment

by:chima
ID: 42093586
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
 
LVL 27

Accepted Solution

by:
dpearson earned 500 total points
ID: 42093598
Yes it sounds like you've figured this out for yourself.
0
 

Author Closing Comment

by:chima
ID: 42093653
thanks
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Print Rhino Java Array in Javascript 1 58
How to log java errors in tomcat 8 39
Getting Variable not defined error in Python 1 82
Oracle programming for starter 14 76
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question