Class and interface in same file and accessing the class from a main class in different package

Ads
 

Class and interface in same file and accessing the class from a main class in different package

Hello everyone;

I was going through some design pattern videos on YouTube, however I have a small doubt on some basic Java concept. I tried searching for solution, but was unable to find one. Below is my query.

I have some classes Animal.java, Dog.java, an interface Fly.java which also has a class named CantFly in same file. A main method CheckAnimal.java. Below is the code

Animal.java

package com.classification.pojo;

public class Animal {

    public Fly flyingType;

    public String tryToFly() {
        return flyingType.fly();
    }

    public void setFlyingAbility(Fly newFlyType) {
        flyingType = newFlyType;
    }

}

Dog.java

package com.classification.pojo;

public class Dog extends Animal {
    public Dog() {
        super();
        flyingType = new CantFly();
    }

    public void digHole() {
        System.out.println("I am digging hole!");
    }
}

Fly.java

package com.dessignpattern.strategy;

public interface Fly {
    String fly();
}

class CantFly implements Fly {

    public String fly() {
        return "Can't fly";
    }

}

CheckAnimal.java

package com.designpattern.main;

import com.classification.pojo.Animal;
import com.classification.pojo.Dog;
import com.classification.pojo.Fly;

public class CheckAnimals {

    public static void main(String[] args) {


        Animal doggy = new Dog();

        doggy.setFlyingAbility(new Fly.CantFly());

    }

}

In CheckAnimal.java, for doggy object to invoke setFlyingAbility() method correctly, Animal.java, Dog.java and Fly.java needs to be in same package. If I keep Fly.java in different package, I cannot access CantFly() method. I hope I have made my point clear.

- Ishan

View Answers

Ads









Related Tutorials/Questions & Answers:
Class and interface in same file and accessing the class from a main class in different package
Class and interface in same file and accessing the class from a main class... a class named CantFly in same file. A main method CheckAnimal.java. Below is the code Animal.java package com.classification.pojo; public class Animal
Class and interface in same file and accessing the class from a main class in different package
Class and interface in same file and accessing the class from a main class... a class named CantFly in same file. A main method CheckAnimal.java. Below is the code Animal.java package com.classification.pojo; public class Animal
Advertisements
xcode unknown class in interface builder file
xcode unknown class in interface builder file  What is the unknown file in xib or how can i find and remove such kind of exception from my file.. "xcode unknown class in interface builder file
Can I have multiple main() methods in the same class?
Can I have multiple main() methods in the same class?  Hi, Can I have multiple main() methods in the same class?   Hi, No, We cann't use multiple main() method within the same class. If we do this then the program
class file
class file  How to create class file in jsf easily
file class
file class  Hi Friend, I have a file class it lets me extract all the data from a csv file but I need to align the data properely, seperating... java.util.*; public class ReadCSV{ public ReadCSV (){ } public static
Class
and Rectangle  by calling the different functions from the different classes... there are two classes has been used except the main class in which the main function... and rectangle class are calling with different - different arguments two times
class file
class file  how to convert java classfile into java sourcefile
Interface and Abstract class
and Abstract class? Give some example   Difference Between Interface and Abstract Class 1)Main difference is methods of a Java interface are implicitly...)Members of a Java interface are public by default. A Java abstract class can have
Abstract class and interface in Java
Abstract class and interface in Java  What is the difference between abstract class and interfaces in Java?   Differences between an interface and an abstract class: At the same time multiple interfaces can
Run a .exe from a class
Run a .exe from a class  how to lauch a .exe file from a class in Java and to check the success ful execution of the same
class
is a class to represent a country in medal tally. It has an attribute of country... medals. In this class, you should also define constructors, and assessor, mutator methods. Task 2 MedalTally.java is a class to model a medal tally, containing
How to close a frame on action event of a button from a different class ?
How to close a frame on action event of a button from a different class ?  How to close a frame on action event of a button from a different class ? I have a designed a control-frame on 1 class which has jbutton components, I
about the base class of file
about the base class of file  what is the basic class of file
Java class file
Java class file  How do i open a java class file using eclipse
overriding in derived class from abstract class
overriding in derived class from abstract class  why should override base class method in derived class? Instead of override abstract class method, we can make method implementation in derived class itself know
Interface vs abstract class
Interface vs abstract class  Hai, Is there any possible to defining abstract methods in Interface
Discuss the Number Class in the java.lang package
Discuss the Number Class in the java.lang package   Discuss the Number Class in the java.lang package
Random Access File class
Random Access File class  Write a short note on Random Access File class.   Please visit the following link: Java Random Access File
What are the different types of property and class mappings?
What are the different types of property and class mappings?  Hi, What are the different types of property and class mappings? thanks
Difference between abstract class and an interface
Difference between abstract class and an interface The difference between abstract class and interface is one of the most popular question in the interview... between abstract class and interface in java. Abstract class is a class
validate jtextfield in two different class use keylistener
validate jtextfield in two different class use keylistener  validate jtextfield in java swing will use two class, design part in one class and method calling in one class will use Keylisteners or change listeners
Class names don't identify a class - Java Tutorials
ClassLoader to load the different class instances at the same time... to this class loader, then it loaded the classes from their. Consider a simple example... declaration is different. The general way to use this class at run time instead
.jar file keeps giving me " could not find the main class". Program will exit.
.jar file keeps giving me " could not find the main class". Program will exit... netbeans's project properties clearly sets testing2.hihi as my Main class and I have... click on it, it gives me the message" could not find the main class. Program
Use varibles from another class
Use varibles from another class  **I have two classes. class 1:** public class askfigures extends JFrame { method from class1: public void... in another class. another class: public class Askvalues extends JFrame
how to pass variable from simple java main class(not servlet) to the jsp page?
how to pass variable from simple java main class(not servlet) to the jsp page?  I have a simple Java class in which I invoke a call to a JSP page... from the simple Java class to the JSP page. How can I do that? Here is my
Class Loader
a file of that name from the file system. Class loader does not create class object... about files and file systems because of class loaders. Delegation provides... class is the most popular type of objects contained in a Jar file. A class can
Class Loader
a file of that name from the file system. Class loader does not create class object... about files and file systems because of class loaders. Delegation provides... class is the most popular type of objects contained in a Jar file. A class can
.class and .tld
.class and .tld  how to create .class and .tld file
Problem to get connection from DAO class to JDBC
Problem to get connection from DAO class to JDBC  package controller; import java.sql.Connection; import java.sql.DriverManager; public class...; } } and interface was package controller; public interface Provider
_OBJC_CLASS_$_MFMailComposeViewController", referenced from:
_OBJC_CLASS_$_MFMailComposeViewController", referenced from:   While configuring the email feature in my ipad application, i get the following error message. "OBJCCLASS$MFMailComposeViewController", referenced from: Can

Ads