Is it possible to call an overridden method outside the main class? - java

public class OverrideAnimals {
public void makeSound() {
System.out.println("animal sounds!");
}
public static void main(String[] args) throws IOException {
OverrideAnimals animals=new OverrideAnimals ();
OverrideAnimals dog=new PolyDog();
}
}
class PolyDog extends OverrideAnimals {
#Override
public void makeSound(){
System.out.println("arf!!! arf!!! arf!!!");
}
}

No. The class that overrides can, however, call "super".
class PolyDog extends OverrideAnimals {
public void superMakeSound() {
super.makeSound();
}
#Override
public void makeSound() {
// ..

Related

I have Class file which has multiple methods, Can i invoke the class from Testcase

I have a class called TestingClass and I have a one more class called TestCase .
In TestingClass I have 4 methods which are dependent on each other, Can i call the this class file from my TestCase file and invoke all the methods at once?
Yes you can if they have visibility.
In your TestCase you have to declarate a variable of TestingClass and instantiate it. Then, if your methods are public you can use them.
Here is an example:
public class TestCase {
private TestingClass testingClass = new TestingClass();
#Test
public void test1(){
testingClass.methodFromTestCase();
}
}
You can try with extend the TestingClass and overwriting those methods as like,
public class TestCase extends TestingClass {
#Override
public void method1(){
super.method1();
}
#Override
public void method2(){
super.method2();
}
#Override
public void method3(){
super.method3();
}
#Override
public void method4(){
super.method4();
}
}
There is another way to do it, refer below example:
package overridefunction;
public class SuperClass
{
public void method1()
{
System.out.println("superclass method1");
this.method2();
this.method3();
this.method4();
this.method5();
}
public void method2()
{
System.out.println("superclass method2");
}
private void method3()
{
System.out.println("superclass method3");
}
protected void method4()
{
System.out.println("superclass method4");
}
void method5()
{
System.out.println("superclass method5");
}
}
package overridefunction1;
public class SubClass extends SuperClass
{
#Override
public void method1()
{
System.out.println("subclass method1");
super.method1();
}
}
package overridefunction1;
public class Demo
{
public static void main(String[] args)
{
SubClass mSubClass = new SubClass();
mSubClass.method1();
}
}
subclass method1
superclass method1
superclass method2
superclass method3
superclass method4
superclass method5

how to call implementation class methods in run method without hardcoding

interface Myinterface
{
boolean run();
}
class MyClass implements Myintr
{
boolean run()
{
boolean status=false;
disp();//hard coded
show();//hard coded
here above two methods are hard coded how can we call without hard coding
}
public void disp()
{
System.out.println("Hello Person");
}
public void show()
{
System.out.println("Welcome");
}
}
class Mainclass
{
public static void main(String args[])
{
Class aClass=Class.forName("Myclass");
Object obj=aClass.newInstance();
Myinterface myinter=(Myinterface)obj.run();
}
}
Your question is extremely unclear, but I suspect you're looking for something like this:
MyInterface.java:
interface MyInterface {
void run();
}
Impl1.java:
class Impl1 implements MyInterface {
#Override
public void run() {
System.out.println("Hello Person");
}
}
Impl2.java:
class Impl2 implements MyInterface {
#Override
public void run() {
System.out.println("Welcome");
}
}
MainClass.java:
class MainClass {
public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class<?> aClass = Class.forName("com.mypackage.Impl1");
Object obj = aClass.newInstance();
MyInterface myinter = (MyInterface)obj;
myinter.run();
}
}

How can I call the most specific method using generics?

Having the following example:
public class Test {
public static class A {}
public static void main(String[] args) {
A a = new A();
m1(a);
}
public static <T> void m1(T t) {
// t.getClass().getSimpleName() is A
// t instanceof A is true
m2(t);
}
/* Not called */
public static void m2(A a) {
System.out.println("A");
}
public static void m2(Object o) {
// o.getClass().getSimpleName() is A
// o instanceof A is true
System.out.println("O");
}
}
I don't understand why m2(Object o) is chosen instead of m2(A a). As you can see, when m2(t) is called, t "is an A".
Output:
actual
O
expected
A
How can I use generics for the situation above so that m2(A a) is chosen?
Edit:
I'd like to have a general solution that will work even if I add a type B (similar to A).
...
public static void main(String[] args) {
A a = new A();
m1(a);
B b = new B();
m1(b);
}
...
public static void m2(B b) {
System.out.println("B");
}
...
Output:
actual
O
O
expected
A
B
You have to do:
public static <T extends A> void m1(T t) {
m2(t);
}
Otherwise the compiler cannot infer that the passed parameter is compliant with m2(A a) and with pick m2(Object o) instead.
You are looking for double dispatch which Java does not support. I do not think that generics can help here, but there's the visitor design pattern with which you can emulate it:
public class Test {
public static interface Visitable {
void accept(Visitor visitor);
}
public static class A implements Visitable {
#Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
public static class B implements Visitable {
#Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
public static interface Visitor {
void visit(A a);
void visit(B b);
}
public static class PrintingVisitor implements Visitor {
#Override
public void visit(A a) {
System.out.println("A");
}
#Override
public void visit(B b) {
System.out.println("B");
}
}
public static void main(String[] args) {
Visitable visitable = new A();
m(visitable);
visitable = new B();
m(visitable);
}
public static void m(Visitable visitable) {
visitable.accept(new PrintingVisitor());
}
}

Java Polymorphism and Dynamic Programming

I am working on a system which requires performance at its peak and i am stuck in one scenario whose solution i want to ask.
Here is my code
public interface ILoad {
public void loadData();
}
public class ClassOne implements ILoad {
#Override
public void loadData() {
System.out.println("CLASS ONE LOADING DATA ");
}
}
public class ClassTwo implements ILoad {
#Override
public void loadData() {
System.out.println("CLASS TWO LOADING DATA");
}
}
public static void main(String[] args) {
load(new ClassOne());
load(new ClassTwo());
}
public static void load(Object o){
ILoad ref = (ILoad) o;
ref.loadData();
}
As you can see in above case there are no if else or switch conditions all the code is executed using interfaces by reference which is super fast when it comes to programming.
My question is for instance my interface has two methods loadData() and loadRecord()
Now if I want to execute these two methods just as on above example how can I do this? one way is make a separate class, second way reflection and introspection. Are there any better solutions?
You are not using polymorphism properly if you have to cast.
How about something like this?
public static interface ILoadData {
public void loadData();
}
public static interface ILoadRecord {
public void loadRecord();
}
public static interface ILoadEither extends ILoadData, ILoadRecord {
}
public static class ClassOne implements ILoadData {
#Override
public void loadData() {
System.out.println("CLASS ONE LOADING DATA ");
}
}
public static class ClassTwo implements ILoadData {
#Override
public void loadData() {
System.out.println("CLASS TWO LOADING DATA");
}
}
public static class ClassThree implements ILoadRecord {
#Override
public void loadRecord() {
System.out.println("CLASS THREE LOADING RECORD");
}
}
public static class ClassFour implements ILoadRecord, ILoadData {
#Override
public void loadRecord() {
System.out.println("CLASS FOUR LOADING RECORD");
}
#Override
public void loadData() {
System.out.println("CLASS FOUR LOADING DATA");
}
}
public static class ClassFive implements ILoadEither {
#Override
public void loadRecord() {
System.out.println("CLASS FIVE LOADING RECORD");
}
#Override
public void loadData() {
System.out.println("CLASS FIVE LOADING DATA");
}
}
public static void load(ILoadData o) {
o.loadData();
}
public static void load(ILoadRecord o) {
o.loadRecord();
}
public static void load(ILoadEither o) {
o.loadRecord();
}
public static void main(String[] args) {
load(new ClassOne());
load(new ClassTwo());
load(new ClassThree());
load((ILoadData)new ClassFour());
load((ILoadRecord)new ClassFour());
load(new ClassFive());
}
Here we have multiple static load methods, each taking a parameter of a different interface but the compiler can decide at compile time which to use, unless it is ambiguous, in which case you need some other trick - I use casting here as it is simple but you would be better to use an adaptor of some sort.
Why aren't you doing this:
public static void load(ILoad o){ // both classes implement Iload. using Object here gives a chance for anything to come here.
o.loadData();
}
I'm not sure if I've understood your question, but isn't enough the following code?
public interface ILoad {
public void loadData();
public void loadRecord();
}
public class ClassOne implements ILoad{
#Override
public void loadData() {
System.out.println("CLASS ONE LOADING DATA ");
}
#Override
public void loadRecord() {
System.out.println("CLASS ONE LOADING RECORD");
}
}
public class ClassTwo implements ILoad{
#Override
public void loadData() {
System.out.println("CLASS TWO LOADING DATA ");
}
#Override
public void loadRecord() {
System.out.println("CLASS TWO LOADING RECORD");
}
}
public static void main(String[] args) {
// TODO code application logic here
load(new ClassOne());
load(new ClassTwo());
//new objects created here, instances can be created just once
loadRecord(new ClassOne());
loadRecord(new ClassTwo());
}
public static void load(ILoad l){
ref.loadData();
}
public static void loadRecord(ILoad l){
ref.loadRecord();
}

How to write an abstract main class

I need to write some importers. They need all the same initialization. So I try to write an abstract class, which does all the initialization and also has the main method, so that all sub-classes just need to implement run() to do their specific import work:
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
public static void main(String[] args) {
AbstractImporter importer = new AbstractImporter();
importer.run();
}
}
public class ConcreteClass() {
public void run() {
// Do some importing
}
}
Of course it fails to create an instance of this abstract class (new AbstractImporter()).
Does anybody has any idea how to solve that? TIA!
Obviously you need a concrete class - anonymous or otherwise.
Better to move the main method to another class and instantiate the appropriate concrete class based on data (either your domain specific or a constant) and then run it. This way each implementation can be independent of other implementations.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
}
public class ConcreteImporter1 extends AbstractImporter {
public void run() {
//do something
}
}
public class ImporterMain() {
public static void main(String[] args) {
AbstractImporter importer = createImporter(args[1]);
importer.run();
}
private static AbstractImporter createImporter(String type) {
if (type.equals("1")) {
return new ConcreteImporter1();
}
....
}
}
new AbstracterImporter() {
public void run() {
// ...
}
};
I apologize for current lack of formatting, currently on a mobile device.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
public static void main(String[] args) {
AbstractImporter importer = new AbstractImporter(){
public void run() {
System.out.println("Anonymous implementation");
}
};
importer.run();
}
}
You cannot create an instance of an abstract class.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
}
public class ConcreteClass extends AbstractImporter{
public void run(){
//Implementation
}
public static void main(String args[]){
AbstractImporter ai = new ConcreteClass();
ai.run();
}
}

Categories