티스토리 뷰
Overview
Abstract classes are classes that contain abstract methods. Abstract methods are methods that contain no implementation. A class that contains all abstract methods is called an interface. It is not possible to create an instance of an abstract class or an interface, due to the fact that some or all of their methods are not complete.
Reasoning
Abstract classes and interfaces exist for the purpose of organization. If you want to model an abstract concept without being able to instantiate it, an abstract class or interface may be used. For example, think about food: “Food represents the abstract concept of what we can eat. It doesn't make sense for an instance of food to exist.”* This idea of thinking about concepts abstractly allows for much more freedom when writing and sharing code. Also, abstraction increases the power of polymorphism. This is the basis behind abstraction.
Implementation: Abstract Classes
An abstract class contains one or more abstract methods. Generally you want to create an abstract class when you are creating several objects that will share some method functionality, and differ in some method functionality. The methods that function the same way for every object will be implemented in the abstract superclass, whereas the methods that differ in functionality for each object will be declared abstract
in the abstract superclass, and implemented in each of the subclasses.
Take for example, an abstract class that models electronics. public abstract class Electronic
{
boolean powerOn = false;
public boolean isOn()
{
return powerOn;
}
public void turnOn()
{
powerOn = true;
System.out.println("Turned on");
}
public void turnOff()
{
powerOn = false;
System.out.println("Turned off");
}
public abstract void operate();
}
Notice the abstract
keyword in the declaration of class Electronic
and method operate()
. Also notice the semicolon after the declaration of operate()
.This is the proper syntax for defining abstract classes and methods.
Every subclass of Electronic
will inherit the methods isOn()
, turnOn()
, and turnOff()
, and each of those methods will function in the same way. However, the method operate()
will function differently in each subclass of Electronic
.
Take now a subclass of Electronic, class Toaster. Notice that the Now look at class Both class Toaster and class An interface is basically an abstract class that contains all abstract methods. That is to say, there is no implementation in the class whatsoever. All the class contains are several method headers. Its purpose is similar to that of an abstract class, only in this case, none of the classes that ‘implement’ the interface share the same code. They only share the same method names.
To create an interface, use the Interfaces are not extended, they are implemented by other classes. This is done through the Notice that each of these classes implements the methods specified in the public class Toaster extends Electronic
{
boolean hasBread = false;
public Toaster(boolean bread)
{
hasBread = bread;
}
public boolean checkForBread()
{
return hasBread;
}
public void addBread()
{
hasBread = true;
}
public void operate()
{
if (isOn() && checkForBread())
{
System.out.println("Bread toasted");
}
else if (!isOn())
{
System.out.println("Turn power on");
}
else if (!checkForBread())
{
System.out.println("Must insert bread before toasting");
}
}
}
operate()
method is implemented in a way specific to this class.
iPod
, another subclass of Electronic
. public class iPod extends Electronic
{
String currentSong = null;
public iPod(String song)
{
currentSong = song;
}
public String getCurrentSong()
{
return currentSong;
}
public void changeSong(String newSong)
{
currentSong = newSong;
}
public void operate()
{
if (currentSong != null && isOn())
{
System.out.println("Now playing: " + currentSong);
}
else if (!isOn())
{
System.out.println("Turn power on");
}
else if (currentSong == null)
{
System.out.println("Please choose a song to play");
}
}
}
iPod
have different operate()
methods, but use the methods implemented in class Electronic
in the same way. By grouping these methods in the abstract class Electronic
, and extending it to Toaster
and iPod
, we have saved ourselves from reproducing the same code in each class. Thus far reducing redundancy and increasing efficiency within the code.
Implementation: Interfaces
interface
keyword. None of the methods in the interface need to be declared abstract, because it is obvious that they are abstract if they are within an interface.
public interface Animal
{
String talk();
int nuberOflegs();
int nuberOfarms();
boolean hasFur();
}implements
keyword. Take a look at the following four classes that implement the Animal interface. public class Cat implements Animal
{
public Cat(){}
public String talk()
{
return "Meow";
}
public int numberOfLegs()
{
return 4;
}
public int numberOfArms()
{
return 0;
}
public boolean hasFur()
{
return true;
}
}
public class Dog implements Animal
{
public Dog(){}
public String talk()
{
return "Woof";
}
public int numberOfLegs()
{
return 4;
}
public int numberOfArms()
{
return 0;
}
public boolean hasFur()
{
return true;
}
}
public class Human implements Animal
{
public Human(){}
public String talk()
{
return "Hello";
}
public int numberOfLegs()
{
return 2;
}
public int numberOfArms()
{
return 2;
}
public boolean hasFur()
{
return false;
}
}
public class TRex implements Animal
{
public TRex(){}
public String talk()
{
return "Roar!";
}
public int numberOfLegs()
{
return 2;
}
public int numberOfArms()
{
return 2;
}
public boolean hasFur()
{
return false;
}
}
Animal
interface in a different way. That is to say, there are no methods with common functionality among all of the classes that implement Animal
. Thus is why Anima
l is an interface, and not an abstract class.
*quoted from http://java.sun.com/docs/books/tutorial/java/javaOO/abstract.html
'IT > 프로그래밍' 카테고리의 다른 글
가변인자(...) va_start, va_arg, va_end (0) | 2007.11.18 |
---|---|
Observer pattern - Wikipedia, the free encyclopedia (0) | 2007.07.31 |
Java Guide - Polymorphism (0) | 2007.07.31 |
Java Guide - Polymorphism (0) | 2007.07.31 |
Strategy pattern - From Wikipedia, the free encyclopedia (0) | 2007.07.31 |
- Total
- Today
- Yesterday
- 지루박멸연구센타
- 열정의 힘을 믿는다
- Le4rN TO Cr4cK
- 디버깅에관한모든것(DebugLab)
- sysinternals
- FoundStone
- hashtab
- 보안-coderant
- 디바이스드라이버 개발자 포럼
- dualpage.muz.ro
- osronline.com - 드라이버 관련 정보 사이트
- NtInternals - NativeAPI Refere…
- pcthreat - spyware 정보 제공
- rootkit.com - 루트킷 관련 정보
- www.ntinternals.net
- WINE CrossRef. - source.winehq…
- tuts4you
- hex-rays
- idapalace
- idefense
- immunityinc
- threatexpert
- hdp.null2root.org
- www.crackstore.com
- crackmes.de
- www.who.is
- www.cracklab.ru
- community.reverse-engineering.…
- video.reverse-engineering.net
- SnD
- 클레이 키위
- reversengineering.wordpress.co…
- www.openrce.org
- www.woodmann.com
- PEID.Plusins.BobSoft
- roxik.com/pictaps/
- regexlib.com
- spyware-browser.com
- www.usboffice.kr
- regulator
- www.txt2re.com
- ietab.mozdev.org
- zesrever.xstone.org
- www.heaventools.com/PE-file-he…
- www.heaventools.com
- www.innomp3.com
- 울지않는벌새
- exetools.com-forum
- exetools.com
- utf8 conv
- robtex - IP trace
- onsamehost - same IP sites
- JpopSuki
- jsunpack.jeek.org
- wepawet.iseclab.org
- www.jswiff.com
- www.hackeroo.com
- winesearcher.co.kr
- khpga.org
- malwareurl.com
- anubis.iseclab.org
- www.crummy.com-eautifulSoup
- malwarebytes.org/forums
- bbs.janmeng.com
- blackip.ustc.edu.cn
- eureka.cyber-ta.org
- exploit-db.com
- ROA
- 전세매매지수
- 주택구매력지수
- 다올저축은행
- 실시간트래이딩
- 주식
- 주식트래이딩
- SBI저축은행
- 사회간접자본
- Pivot
- CriticalSection
- 매매가격지수
- 레고랜드
- 공공인프라
- O365
- 맥쿼리인프라
- systemd
- hai
- 군함도
- logrotate
- 피봇
- 미국주식
- PIR
- 시스템트래이딩
- ubuntu
- ChatGPT
- 자동트래이딩
- 신한저축은행
- INVOICE
- ElasticSearch
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |