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:
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
xcode unknown class in interface builder filexcode 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 differentJava
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 fileclass file How to create
class file in jsf easily
class fileclass 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 JavaAbstract
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
classclass Is a
class a subclass of itself
file classfile 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 classInterface 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 classRun 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 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 Java class fileJava
class file How do i open a java
class file using eclipse
overriding in derived class from abstract classoverriding 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
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 MagazineAbstract
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 classRandom 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 interfaceDifference 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 PHP Interface ClassPHP
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 classUse 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 classSuper
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 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 Problem to get connection from DAO class to JDBCProblem 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 BeginnersConvert
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
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 Beginnersmain 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