How to call another classes super class? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Lets say I have three classes, A,B,C. B inherits A, is there a way to access the getVal method of Class A, in C?
class A {
getVal method
}
class B extends A {
}
Class C {
main() {
B x = new B
x.getVal?
}

Yes. Assuming the classes remain in the same package - x.getVal will work.
class A {
String getVal(){
return "from a";
}
}
class B extends A {
}
public class C {
public static void main(String [] args) {
B x = new B();
x.getVal();
}
}
It works - because of the default access modifier. Use the protected access modifier for inheritance.

Related

How to create a child class that takes in a parent class and several other objects? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Given these:
Mark firstStudent = new Mark(...)
Mark firstStudentChecked = new VerifiedMark(firstStudent, object a, object b)
Object a and b can be any other thing. Above is just a sample code to express my idea. I am aware that VerifiedMark is probably a child class of Mark so it can be labelled as "VerifiedMark". But how can we construct the constructor of child class VerifiedMark? Usually, we would make use of super(...), but the parameter here for the child class is an object of parent.
You can define another version of constructor for the parent class (which acts like a copy-constructor). For example,
class Mark {
Object member;
public Mark(Object m) {
// this is regular constructor
member = m;
}
public Mark(Mark m) {
// this is copy constructor
member = m.member;
}
}
Now you can define VerifiedMark somewhat like below:
class VerifiedMark extends Mark {
Object memberA, memberB;
public VerifiedMark(Mark m, Object a, Object b) {
super(m); // calling copy-constructor
memberA = a;
memberB = b;
}
}
I'm guessing VerifiedMark acts as a wrapper for Mark. In this case the subclass should hold a reference to the superclass like this:
class VerifiedMark extends Mark {
Mark m;
Object a, b;
public VerifiedMark(Mark m, Object a, Object b) {
this.m = m;
this.a = a;
this.b = b;
}
}
Given OP's code, the VerifiedMark constructor doesn't take the parameters the Mark constructor takes, therefore a call to super() is redundant (it is automatically called).

Variable Setting to All Subclasses [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to insantiate a variable that encompasses all subclasses of a class? From what I've read so far we must state what type the variable is before setting it equal to something:
Example:
ExampleObject1 object = reference to the object
But what if we wanted to make it so that we could set the variable to any instance or subclass of that object?
Yes, you can already do that.
A variable of type T (as long as T is a class/interface/enum/annotation) can hold a reference to any instance of the class T, or any instance of a class that extends or implements T.
For example, this works:
class MyClass1 {
// ... stuff goes here ...
}
class MyClass2 extends MyClass1 {
// ... stuff goes here ...
}
class Main {
public static void main(String[] args) {
MyClass1 object = new MyClass2();
}
}

What kind of values we can add to List<B> type? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Class A{
public static void main(String args[])
List<B> list=new ArrayList<B>();
list.add("what type of value should we add here");
}
Class B{
int a;
boolean b;
String c;
}
You can add:
references of type B
references of any class which is a subclass of B
null
However, if you get a reference out of the list, its compile-time type will always be of type B -- even if the runtime type is actually a subclass, or null.
you can add objects of type B only
Class B{
int a;
boolean b;
String c;
public B(int a , boolean b , String C){
this.a =a;
this.b =b;
this.c =c;
}
}
B Badd = new B(2,true,"ADD");
list.add(Badd);
You can only add Object of B or Object of its child class in other word Object which satisfy obj instantof B can be added

Program flow from main [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose
public class A{
public void doSomethingInMethodA(){
int a =0;
System.out.println("value of a :"+a);
}
public static void main(String[] args) {
new A().doSomethingInMethodA();
new B().doSomethingInMethodB();
}
}
class B{
public void doSomethingInMethodB(){
int b =0;
System.out.println("value of b :"+b);
C c = new C();
c.doSomethingInMethodC();
}
}
class C{
public void doSomethingInMethodC(){
int c =0;
System.out.println("value of c :"+c);
}
}
Now how can I know the flow of the program programatically.
Like If I provide the name of class A(class with main method) to some parser class then the parser class should tell me the flow of the program like
Edit:
A(class with main)
-> doSomethingInMethodA()(Class A)
-> doSomethingInMethodB()(Class B) ->doSomethingInMethodC()(Class C)
program flow ends
where -> means it calls
I guess through reflection it is not possible because you can't ask a java.lang.reflect.Method which methods do you call?
Is it possible with StackTraceElement? I even heard of AspectJ doing something like this
So how to achieve it?
If you want to generically record the execution flow for any given program, you can use bytecode instrumentation. Consider using java.lang.instrument and ASM to achieve your goal. Take a look at this tutorial.
It's not easy by any means, but it's certainly doable.

Is it possible to access child class variable through parent class reference? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can someone pls let me know the output of this code?
public Class A {
public int count = 5;
public void test() {
// some code
}
}
public Class B extends A {
public int count = 10;
public void test() {
//some code
}
}
public Class Check {
A a = new A();
A b = new B();
public void myTestMethod() {
a.count; //should call A?
a.test; //should call A?
b.count; //which count is called here? compiler error?
b.test; //should call B?
}
}
a.count; => yes you can
a.test; => yes you can
b.count; => count = 5; its didn't shows the compiler error its return the 5.
b.test; => Yes
Java inheritance lets the extending test over-ride or hide methods from the class it extends. So if B extends A and both have the same method then when you call b.method() it will call that method in B.
Inside B you can choose which method is called by doing either method() or super.method() to specify whether to call the super implementation or not.

Categories

Resources