23. وراثت
یک در را موجود کالس کد از مجدد استفاده ی اجازه شما به جاوا در وراثت
دهد می دیگر کالس.
ج کالس به که کرد مشتق موجود کالس یک از را جدید کالس یک توان میدید
derived classشده بری ارث آن از که کالسی به وsuper classمی گفته
کالس های ویژگی جدید کالس و شودsuperبرد می ارث به را.
کلیدی کلمه ی بوسیله جاوا درextendsشود می انجام بری ارث.
مستندات ازoracle:
A subclass inherits all the members (fields, methods, and nested classes) from
its superclass. Constructors are not members, so they are not inherited by
subclasses, but the constructor of the superclass can be invoked from the
subclass.
33. اینترفیس سازی پیاده
کنیم می سازی پیاده زیر کالس در را قبل مثال اینترفیس:
class Automobile implements Car
{
public void start(){
system.out.println(“Car is started.”);
}
public void run(){
system.out.println(“Car is running.”);
}
public void trunOff(){
system.out.println(“Car is turned off.”);
}
}
34. سایت وب مستندات ازOracleاینترفیس مورد در:
Implementing an interface allows a class to become
more formal about the behavior it promises to provide.
Interfaces form a contract between the class and the
outside world, and this contract is enforced at build
time by the compiler. If your class claims to implement
an interface, all methods defined by that interface
must appear in its source code before the class will
successfully compile.
46. Runnable
سازی پیاده با ترد یک ایجادRunnable:
public class HelloRunnable implements Runnable
{
public void run() {
System.out.println("Hello from a thread!");
}
}
47. SubClass Thread
کالس از بری ارث با ترد یک ایجادThread:
public class HelloThread extends Thread
{
public void run()
{
System.out.println("Hello from a thread!");
}
}
48. ریسمان اجرای
ترد اجرای مثالHelloRunnable:
HelloRunnable helloRunnableA = new HelloRunnable ();
Thread t = new Thread( helloRunnableA );
t.start();
ترد اجرای مثالHelloThread:
HelloThread t = new HelloThread ();
t.start();
49. Runnable یا Thread
1) Implementing Runnable is the preferred way to do it. Here, you’re not really specializing
or modifying the thread’s behavior. You’re just giving the thread something to run. That
means composition is the better way to go.
2) Java only supports single inheritance, so you can only extend one class.
3) Instantiating an interface gives a cleaner separation between your code and the
implementation of threads.
4) Implementing Runnable makes your class more flexible. If you extend thread then the
action you’re doing is always going to be in a thread. However, if you extend Runnable it
doesn’t have to be. You can run it in a thread, or pass it to some kind of executor service,
or just pass it around as a task within a single threaded application.
5) By extending Thread, each of your threads has a unique object associated with it, whereas
implementing Runnable, many threads can share the same runnable instance.