This question already has answers here:
When do I use super()?
(11 answers)
Closed 6 years ago.
public class polymorphism {
public void show()
{
System.out.println();
}
public void show(int i)
{
System.out.println("6");
}
public class B extends polymorphism
{
}
/** * #param args the command line arguments */
public static void main(String[] args)
{
// TODO code application logic here B obj=new B(); obj.show();
}
}
To call a method in an object's parent class, you can use the 'super' keyword.
Ex. super.show();
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();
}
This question already has answers here:
Why default constructor is required in a parent class if it has an argument-ed constructor?
(12 answers)
Closed 4 years ago.
public class ConStru
{
public ConStru(int bc)
{
}
}
public class MainClass {
public static void main(String[] args)
{
ConStru conStru = new ConStru();
}
}
it is not working. giving the error.
You should pass an integer to the constructor as
ConStru conStru = new ConStru(1);
Also remove public from "ConStru" class,
Add another constructor:
public ConStru()
{
}
Or create object with an int parameter:
ConStru conStru = new ConStru(1234);
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.
This question already has answers here:
What is the difference between method overloading and overriding? [duplicate]
(2 answers)
Closed 7 years ago.
Just trying to clarify the difference between overloaded & overridden methods... Consider the scenario below;
Say I have a class, let's say 'class Master'. Suppose I have sub class, 'class Apprentice', such that the two classes are related by inheritance.
public class Apprentice extends Master
Hypothetically considering that the master class contains 2 void methods each named attack, one takes a Single string argument, the other a String and an Int argument.
public class Master{
void attack(String bodyPart){
//code for attacking
}
void attack(String bodyPart, int Damage){
//code for specific attack
}
If the Apprentice class has 2 methods named the exact same which takes exactly similar arguments, would the attack Method defined in the Master class be overloaded or overridden?
Wouldn't it be both overridden and overloaded!?
Overriding:
class A {
public void do() { /* ... */ }
}
class B extends A {
#Override
public void do() { /* ... */ } // have an other definition than the one in A
}
Overloading:
class C {
public void do(E e) { /* ... */ }
public void do(E e, F f) { /* ... */ } // same name but different arguments
}
They are two different concepts.
This question already has answers here:
How to create objects from a class with private constructor?
(6 answers)
Closed 7 years ago.
I want to create object of Base class in another class but Base class constructor is defined as private
Here is my Code
public class Main
{
public static void main(String ... args)
{
//Base objBase = new Base();
//objBase.show();
}
}
class Base
{
private Base()
{
}
public void show()
{
System.out.println("Base Class Show() Method");
}
}
Code inside Base is still allowed to call the constructor, which means it's possible to create objects in a static method:
class Base
{
private Base()
{
}
public void show()
{
System.out.println("Base Class Show() Method");
}
public static Base createBase() {
return new Base();
}
}
and then call the method to create an object:
Base objBase = Base.createBase();
objBase.show();
You cannot create objects of classes if they have private constructors. Objects can be constructed, but only internally. That is how it is.
There are some common cases where a private constructor can be useful:
Singletons
Classes containing only static methods
Classes containing only constants
Type safe enumerations
Hoping this helps.