Solved

Java Inheritance super keyword use

Posted on 2017-04-14
8
92 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 28

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 28

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 28

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

623 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