This question already has answers here:
Calling super super class method
(12 answers)
Closed 7 years ago.
I have faced a question in an interview whether we can access the method display() of class ABC from EDC as given below
class ABC {
public void display() {
System.out.println("from ABC");
}
}
class CBD extends ABC {
public void display() {
System.out.println("From CBD");
}
}
class EDC extends CBD {
public void display() {
System.out.println("From EDC");
}
}
I would like to know if we can access the method of ABC from class EDC other than an object creation of ABC. I know the answer is very straight and simple that we can access only the super class method of EDC i.e; display() of CBD through super.display(), but I am feeling whether I am missing any approach here to access the display() of ABC from EDC.
I think one of the possible approaches is as below
class ABC {
public void display()
{
System.out.println("from ABC");
}
public static void main(String args[])
{
ABC obj=new EDC();
obj.display();
}
}
class CBD extends ABC {
public void display()
{
super.display();
}
}
class EDC extends CBD {
public void display()
{
super.display();
}
}
No, it is not possible. You can only go one level up with super.
You could have a method that calls super() from CBD and call that method from EDC using super(), i.e. chain the calls.
Related
This question already has answers here:
Call a method of subclass in Java
(7 answers)
Closed 2 years ago.
Have a below code snippet.
public class Character {
private static String type;
public void doMainSomething() {
System.out.println("Doing Main Something");
}
public static class Gorgon extends Character implements Monster {
public int level;
#Override
public int getLevel() { return level; }
public Gorgon() {
Character.type = "Gorgon";
}
public void doSomething() {
System.out.println("Doing Something");
}
}
public static void main(String[] args) {
Character.Gorgon gor = new Character.Gorgon();
Monster mon = new Character.Gorgon();
mon.doSomething(); -> Error
}
}
How can I access inner class's Gorgon method doSomething using mon ? Is there any specific way, so that we could access class's method using Interface's ref type ?
Proper way is to declare the doSomething() method on Monster interface. If a method needs to be called on interface type, then that method needs to be on the interface or it's parent.
If that is not doable, then you can safely cast the mon to Character.Gorgon
if (mon instanceof Character.Gorgon) {
((Character.Gorgon) mon).doSomething();
}
I'm a beginner in java,so it's a bit difficult for me to understand some examples at times.I want to know which speed method will be implemented among the code and the print statement of which speed() method from which class will be printed in the output?
class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Maruti extends Car{
public Maruti()
{
System.out.println("Class Maruti");
}
public void brand()
{
System.out.println("Brand: Maruti");
}
public void speed()
{
System.out.println("Max: 90Kmph");
}
}
public class Maruti800 extends Maruti{
public Maruti800()
{
System.out.println("Maruti Model: 800");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
It seems you wanted to ask which speed() method will be called (not implemented) when you execute the code in your main method. Since the type of obj is Maruti800 (because of new Maruti800()), then the speed method from the Maruti800 class will be called and it will print Max: 80Kmph. To compare, if you declared your object like Maruti obj = new Maruti(); then obj.speed() would call method speed from the Maruti class.
This question already has answers here:
How to call the overridden method of a superclass?
(12 answers)
Closed 9 years ago.
This question was asked to me in an interview. I have an overridden method in my sub-class. Using an instance of the subclass, I want to call the method of the super class. Bear in mind that the method is overridden.
The output of the following code is NO NO PRINT HI.
What if I want to print Print Hello using the object of overriding class? How do I do that?
class OverrideSuper {
public void printHello() {
System.out.println("Print Hello");
}
}
public class Overriding extends OverrideSuper {
public void printHello() {
System.out.println("NO NO PRINT HI");
}
public static void main(String[] args) {
//OverrideSuper obj1 = new OverrideSuper();
Overriding obj2 = new Overriding();
obj2.printHello();// this calls printHello() of class Overriding.
//I want to call printHello() of OverrideSuper using obj2. How do I do that???
}
}
I think the interviewer expected you to write another method that calls super.printHello():
public class Overriding extends OverrideSuper {
public void printHello(){
System.out.println("NO NO PRINT HI");
}
public void superHello() { // You can make this private or protected, too
super.printHello();
}
public static void main(String[] args) {
//OverrideSuper obj1 = new OverrideSuper();
Overriding obj2 = new Overriding();
obj2.superHello();// this calls printHello() of class OverrideSuper .
}
}
Essentially, he wanted you to tell him that once a method is overriden, there is no way to call through to the method of the base class from the outside. The only way around it is to make a calling path into the superclass on the inside of the overriding class, which is what I did above by defining a superHello method.
[The interviewer] said "I am not convinced"
Another possibility could be that the interviewer was looking for you to provide a reflection-based solution, which is possible only in Java 7+. You need to use MethodHandle, call findSpecial with the base class, and then invoke the method that you get back.
Here is a link to an answer that explains the process, and shows an example.
i think this will fulfil your requirement!! try this
when you call obj2.printHello(); execution will move to Overridings printHello() and in that method i ve used super.printHello(); this means first invoke super class's printHello() and compiler prints Print Hello then execution again come to Overridings printHello() and print NO NO PRINT HI
class OverrideSuper {
public void printHello() {
System.out.println("Print Hello");
}
}
public class Overriding extends OverrideSuper {
public void printHello() {
super.printHello();
System.out.println("NO NO PRINT HI");
}
public static void main(String[] args) {
// OverrideSuper obj1 = new OverrideSuper();
Overriding obj2 = new Overriding();
obj2.printHello();// this calls printHello() of class Overriding.
// I want to call printHello() of OverrideSuper using obj2. How do I do
// that???
}
}
output
Print Hello
NO NO PRINT HI
You can use the super keyword to access the parent method.
class OverrideSuper{
public void printHello(){
System.out.println("Print Hello");
}
}
public class Overriding extends OverrideSuper {
public void printHello(){
super.printHello();
//System.out.println("NO NO PRINT HI");
}
public static void main(String[] args) {
//OverrideSuper obj1 = new OverrideSuper();
Overriding obj2 = new Overriding();
obj2.printHello();// this calls printHello() of class Overriding.
//I want to call printHello() of OverrideSuper using obj2. How do I do that???
}
}
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
This is a sample program
class abc {
void show() {
System.out.println("First Class");
}
}
class def {
void show() {
System.out.println("Second Class");
}
}
class demo {
public static void main(String args[]) {
abc a1 = new abc();
def a2 = new def();
a1.show();
a2.show();
}
}
Now what I want to ask is there are two different classes but they have same method name and same parameter,
What is this concept called in JAVA?
What is this concept called in JAVA?
This is not a concept , you have named the method same in two un-related classes . If one of the class was sub class of another then depending on the method signature , it would have been overriding or overloading.
You would have implemented run time polymorphism , if one of the class was a subclass of another :
class abc {
void show() {
System.out.println("First Class");
}
}
// def extends from abc
class def extends abc {
// show() method was overridden here
void show() {
System.out.println("Second Class");
}
}
class demo {
public static void main(String args[]) {
// Use the reference type abc, object of type abc
abc a1 = new abc();
// Use the reference type abc, object of type def
abc a2 = new def();
// invokes the show() method of abc as the object is of abc
a1.show();
// invokes the show() method of def as the object is of def
a2.show();
}
}
Now what I want to ask is there are two different classes but they have same method name and same parameter, What is this concept called in Java?
There's no "concept" here - they're just unrelated methods. To relate them, the method would have to be declared in a superclass or an interface that both classes implemented. That would then allow the method to be called polymorphically, with the caller only being aware of the interface.
Now what I want to ask is there are two different classes but they
have same method name and same parameter, What is this concept called
in JAVA?
This is no special OOPS or Java feature. Any independent(not connected through some hierarchy) classes can have same method signatures.
For your knowledge, Method in a child class with the same signature as of parent is called overridden method. That is a OOPS method overriding concept.
Another OOPS concept is method overloading, which is within a class having methods with same name but different input arguments.
It is not a concept in java
If a class inherits a method from its super class, then there is a chance to override the method provided that it is not marked final.
For example
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and run");
}
}
public class TestDog{
public static void main(String args[]){
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();// runs the method in Animal class
b.move();//Runs the method in Dog class
}
}
Overloading methods within a class can have the same name and they have different parameter lists
public class DataArtist {
...
public void draw(String s) {
...
}
public void draw(int i) {
...
}
public void draw(double f) {
...
}
public void draw(int i, double f) {
...
}
}
Reference links : http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
If one of the class was sub class of another then , it would have been overridding or overloading.
Can call a method by class name in same package without create object of class or without inheritance in java
public class BoxWeight /*extends Box*/{
public static void main(String[] args) {
/*BoxWeight myCat = new BoxWeight();*/
/*Box myAnimal = myCat;*/
Box.testClassMethod();<------------ why this possible
/* myAnimal.testInstanceMethod();*/
}
}
public class Box /*extends Bicycle*/{
public static void testClassMethod() {
System.out.println("The class" + " method in Box.");
}
public void testInstanceMethod() {
System.out.println("The instance " + " method in Box.");
}
}
my question is not this as you seems my qestion is this "Can call a method by class name in same package without create object of class or without inheritance in java" but i have fix this i want to confirm is this possible or not
You can call a method with the syntax ClassName.methodName() if the method is declared static, eg
class ClassName {
static void methodName() {
//...//
}
}
More info about static class members can be found in the Java Tutorials.
Make your method static then you can call that method by class name.
class A {
static void display(){
System.out.println("Called..");
}
}
In same package you can call it as .
A.display();