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 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









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
Interface Vs Abstract Class
Interface Vs Abstract Class       There are three main differences between an interface and an abstract class: At the same time multiple interfaces can be implemented, but only
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
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
Java class name and file name are different
Java class name and file name are different  Can we run a Java program having class name and file name different?   Hi Friend, No, you...[]) { System.out.println("This is a program with class name and file name are different
class file
class file  How to create class file in jsf easily
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
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
class  Is a class a subclass of itself
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
Interface and Abstract class
Interface and Abstract class  hello,, Can some body tell me what is the difference between an Interface and an Abstract class?   hi,ADS_TO_REPLACE_1 Very Good Question Abstract class is a class which contain one
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
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
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
abstract class and interface - Java Beginners
? when should we use an abstract class? when should we use interface instead... from the abstract class (base class). Abstract classes are not instantiated...() { System.out.println ("Moo! Moo!"); } } Why not declare an abstract class as an interface
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
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
Java file class
Java file class  What is the purpose of the File class
bad class file - JSP-Servlet
api.jar file that you have downloaded from class path.Find servlet-api.jar from...bad class file  (from the JAVA SERVLET PROGRAMMING - O'Reilly) and got a "bad class file...): myservlet.java:9: cannot access HttpServlet bad class
Abstract class and Interface - Java Magazine
Abstract class and Interface  Dear Sir, Please anyone help me........I wane exact difference between Abstract class and Interface.what... abstract class AbstractExample { public AbstractExample(){ System.out.println
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
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
What’s the difference between accessing a class method via -> and via ::?
What?s the difference between accessing a class method via -> and via ::?  What?s the difference between accessing a class method via -> and via
What are the difference between abstract class and interface?
What are the difference between abstract class and interface?  What are the difference between abstract class and interface
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
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
PHP Interface Class
PHP Interface Class: PHP does not support multiple inheritance directly, to implement this we need Interface. It is much similar to Interface of Java... implement keyword to extend this kind of class, at the same time we can
Uses of abstract class & interface - Java Beginners
classes from different types of hierarchy. Abstract Class: Any class... to java. I knew the basic concepts of interface and the abstract class. But i dont... into these classes. Abstract class prevents from instantiating a class
Class
is like a template from which an instance (object) of a class is created at run... is like a template from which an instance of that class is created at run time... we create a main class "Classtest" and also create a class "
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. (adsbygoogle = window.adsbygoogle || []).push
Super class of an Exception class
Super class of an Exception class  hello,,, What is super class of an Exception class?   hii,,ADS_TO_REPLACE_1 the super class of an exception is throwable class. and the super class of an throwable class
.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
Class names don't identify a class - Java Tutorials
with the different directory will run at the same time, The output of the above class... to this class loader, then it loaded the classes from their. Consider a simple example... Class"; } } As you can see the two classes is completely different
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
could not find the main class programm will exit - error
could not find the main class programm will exit - error  when i... main class programm will exit. class Car { String name: String Color...); System.out.println("Number of the Car is: " + num); } public static void main
_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
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
Convert Class File to .java File - Java Beginners
Convert Class File to .java File  Sir, How To Convert .class file to .java File. Please Help me  Use cavaj software ,which is freeware, to convert .class to .java  Use jd-gui to convert ur .class to .java
Is there a data structure like a class that doesn't need to be defined as a class in it's own file?
Is there a data structure like a class that doesn't need to be defined as a class in it's own file?  I am new to java, and I am trying to make... like a class without having to make a whole new file to define it, so I could
.class and .tld
.class and .tld  how to create .class and .tld file
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
Node class
Node class   hii, What is a node class?   hello,ADS_TO_REPLACE_1 A node class is a class that has added new services or functionality beyond the services inherited from its base class
main function defnition in class - Java Beginners
main function defnition in class  why do we use public infront of a main function   Hi friend, The main method is similar to the main... and will subsequently invoke all the other methods required by your program. The main method

Ads