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:
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
class file
class file How to create
class file in jsf easily
class file
class file how to convert java classfile into java sourcefile
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
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
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
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
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
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
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
Class
is like a template
from which an instance (object) of a
class is created at run... a template
from which an
instance of that
class is created at run time.
After defining... is outside of the
main class and define a
method "display" in it. Now
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
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
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