Yes, it is allowed, but it is considered poor design to do so. The problem is not that your subclass contains a specialised, non-generic method, it is only in how you are accessing it. When you hold a variable, pointer or reference to a base class then you are expected to treat that class generically. If you cannot do this, then you should seriously rethink your design. Always ask yourself why you are attempting to extort non-generic behaviour from a generic class.
That said, it is sometimes the case that the base class design is outwith your control. It is still the result of poor design but in these cases you have little option but to make use of dynamic downcasts in order to extort specific behaviour from your subclass. The only alternative is to veto the base class completely and write your own, but if this requires a major rewrite then a dynamic downcast might be the better option in the short-term.
Another option would be to use an intermediate subclass, but this is only suitable when you have two or more subclasses that share a common interface, but where that interface is not generic enough for the base class itself. This won't resolve the problem of extorting non-generic behaviour when you hold a reference to the base class, but it can help reduce the number of dynamic downcasts to a minimum (the ideal situation is no dynamic downcasts, but the option is there if you really must use it).
Percolating the specific implementation into the base class can often be a simpler option. Although you should generally avoid this, if you are designing a closed hierarchy then the cost of the reduced encapsulation may be considered a more preferable option to the additional expense of a dynamic downcast.
We can't call (i.e, execute) an abstract method in java because these methods don't contain any code to execute!In some special cases like when an abstract method is overridden in a subclass, and when we are using super class reference variable( which is referring that subclass object), it appears that we are calling abstract method in super class. But actually the code in the subclass method is being executed.Example:abstract class SuperClass{abstract void show(); //abstract method in super class}class SubClass extends SuperClass{void show(){ //show() of SuperClass overridden in SubClassSystem.out.println("SubClass Method");}}class Example{public static void main(String... args){SuperClass sup=new SubClass();sup.show(); //SubClass show() will be executed !!!}}
False. A method with the same signature in both the superclass and its subclass is known as method overriding, and is a valid concept in Java.
False.Any method declared as final cannot be overridden by any subclasses.You also cannot technically override a private method. While your subclass can have a method with the same definition as a private method in the superclass, it does not actually override that method.
The super variable is not a reference to a subclass. It is a reference to the superclass. class MyClass { void printType() { System.out.println("This is a MyClass"); } // MySubClass is a subclass of MyClass. Within this class, the super keyword // refers to MyClass. static class MySubClass extends MyClass { void printType() { // Tell Java we also want to call the printType method of the super class super.printType(); System.out.println("This is a MySubClass"); } } }
No. Because, what is the guarantee that when the super class code is being executed there will always be a sub class? But, the other way round - sub class object accessing a super class variable is possible because, if a sub class uses inheritance to extend from another class, then it is 100% sure that the parent class is going to be around. So a sub class can access the super class variable.
We can't call (i.e, execute) an abstract method in java because these methods don't contain any code to execute!In some special cases like when an abstract method is overridden in a subclass, and when we are using super class reference variable( which is referring that subclass object), it appears that we are calling abstract method in super class. But actually the code in the subclass method is being executed.Example:abstract class SuperClass{abstract void show(); //abstract method in super class}class SubClass extends SuperClass{void show(){ //show() of SuperClass overridden in SubClassSystem.out.println("SubClass Method");}}class Example{public static void main(String... args){SuperClass sup=new SubClass();sup.show(); //SubClass show() will be executed !!!}}
No; you may decide to change the behaviour of a method in the subclass, or to add additional functionality.
False. A method with the same signature in both the superclass and its subclass is known as method overriding, and is a valid concept in Java.
False.Any method declared as final cannot be overridden by any subclasses.You also cannot technically override a private method. While your subclass can have a method with the same definition as a private method in the superclass, it does not actually override that method.
True
Subtype polymorphism in Java allows objects of a subclass to be treated as objects of their superclass. This means that a subclass can be used wherever its superclass is expected, allowing for more flexibility and reusability in code. This is achieved through inheritance and method overriding, where a subclass can provide its own implementation of methods defined in its superclass.
The super variable is not a reference to a subclass. It is a reference to the superclass. class MyClass { void printType() { System.out.println("This is a MyClass"); } // MySubClass is a subclass of MyClass. Within this class, the super keyword // refers to MyClass. static class MySubClass extends MyClass { void printType() { // Tell Java we also want to call the printType method of the super class super.printType(); System.out.println("This is a MySubClass"); } } }
what are the variable
No. Because, what is the guarantee that when the super class code is being executed there will always be a sub class? But, the other way round - sub class object accessing a super class variable is possible because, if a sub class uses inheritance to extend from another class, then it is 100% sure that the parent class is going to be around. So a sub class can access the super class variable.
It is called a local variable since it only exists inside the method.
Finalize method is used when a variable becomes unreachable or of no use. it is finalize and garbage collection will free the memory used by it, which can be then reclaimed for some other variable
In Java, or in any object oriented language such as C++, a method defined in super (parent) class does not need to be defined in a subclass, because that is the primary purpose of inheritance. Object oriented programming allows you to define and declare a class that implements the behavior for an object. Inheritance allows you to refine, or subclass, that class by "reusing" all of the functionality of the parent class into the sub class, adding additional definition and declaration for the sub class. If the subclass needs to change a parent class method, it can overload that method. This is called abstraction.