SlideShare a Scribd company logo
Class Definition. Inheritance
Abstract Class & Interfaces
Class Definition
•
•
•
• Scanner keyboard;
•
• Keyboard = new String();
•
• keyboard.nextInt();
• class data
• variables. methods
int a;
int b;
float c;
a = 1;
b = 2;
c = 4.5;
a = 6;
b = 4;
c = 8.6;
Object 1
Object 2
new
new
Class
variables
method(s)
members
•
• Date object1;
• new :
• object1 = new Date();
•
• Date object1 = new Date();
• Date object2 = new Date();
String month;
int day;
int year;
writeOutput()
month = May;
day = 6;
year = 1994;
month = October;
day = 25;
year = 1990;
new
new
Date
Demo.java
•
•
•
Heading
body
最重要的是這個回傳值宣告
當method動作最後有
return ___;
取決於回傳了什麼東西
回傳 int 就要寫 int
回傳 String 就要寫 String
如沒有回傳 也要寫 void
•
• object1.month;
• object2.day;
•
• object1.writeOutput();
String month;
int day;
int year;
writeOutput();
month = May;
day = 6;
year = 1994;
month = October;
day = 25;
year = 1990;
object1
object2
new
new
Demo
•
•
•
•
•
•
•
•
•
Java & OOP Core Concept
• main void
• main
public static void main (String[] args)
{
……
}
•
•
•
public void method_a()
{
int a;
…
}
public void method_b()
{
int a;
…
}
•
•
public class Date
{
String month;
int day;
int year;
public void setDate(String month, int day, int year)
{
this.month = month;
this.day = day;
this.year = year;
}
}
Q & A ?
Information Hiding & Encapsulation
&
•
•
private
private
DateTry date = new Demo( );
(1) date.month = “January”;
(2) date.day = 1;
(3) date.year = 2008;
(4) date.writeOutput( );
(5) date.getDay( );
Another class
private
public
, private
methods
•
•
•
•
•
•
int getA( ) { return a; }
// Accessor method
void setA(int p) { a = p; }
// Mutator method
int getB( ) { return b; }
// Accessor method
void setB(int p) { b = p; }
// Mutator method
float getC( ) { return c; }
// Accessor method
void setC(float p) { c = p; }
// Mutator method
private int a;
private int b;
private float c;
class
•
•
•
•
public int getDate( )
{
}
public String getDate( )
{
}
Constructors
•
•
•
•
•
•
•
•
•
•
•
•
• false
• null
• zero
Overloaded
•
• Date object1 = new Date(5, 6, 1990);
• Date object2 = new Date(May, 6, 1990);
• Date object3 = new Date(1994);
•
• object1.Date()
Q & A ?
Static Type
•
•
(1) Round.area(radius)
(2) Round.volume(radius)
Round r = new Round();
(3) r.volume(radius)
•
•
void method1( )
{
B obj = new B( );
myobj.method2( );
}
void static method2( )
{
B.method1( );
}
void static method3( )
{
this.method1( )
}
void static method1( )
{
}
void method2( )
{
}
•
•
•
•
• Private static int myStaticVariable;
•
static int a;
int b;
Class myClass
b = 10; b = 5; b = 3; b = 1;
obj1 obj2 obj3 obj4
• private
•
• final
• public
• public static final int BIRTH_YEAR = 1994;
•
• int year = ClassName.BIRTH_YEAR;
Q & A ?
Inheritance
•
•
•
•
•
•
•
•
•
•
Java & OOP Core Concept
•
• public class HourlyEmployee extends Employee
String name;
int hireDate;
int hours;
int Salaryrate;
Employee HourlyEmployee
Inherit
•
•
•
•
•
•
Override
•
•
•
String name;
int hireDate;
public int getSalary()
{
definition
}
int hours;
int Salaryrate;
public int getSalary()
{
definition
}
Employee HourlyEmployee
Inherit
•
•
•
•
•
•
•
•
•
•
•
•
•
• private void doSomething()
• public void doSomething()
• public void doSomething()
• private void doSomething()
Override OK
Override WRONG!
• final
•
• final
•
•
•
•
• super ( )
public derivedClass(int p1, int p2, double p3)
{
super(p1, p2);
instanceVariable = p3;
}
Constructor method();
Method1();
Constructor method();
super()
Base Class
Derived Class
•
•
• this
public ClassName(type1 param1, type2 param2)
{
…
}
public ClassName()
{
this(argument1, argument2);
}
public class Employee
{
private String name;
private Date hireDate;
public Employee( )
{
}
public Employee(String theName, Date theDate)
{
}
public Employee(Employee originalObject)
{
}
}
public class HourlyEmployee extends Employee
{
private double wageRate;
private double hours;
public HourlyEmployee( )
{
this(“No name”, new Date(“January”, 1, 1000), 0, 0);
}
public HourlyEmployee(String theName, Date theDate,
double theWageRate, double theHours)
{
}
}
•
•
A class obj1
B class obj2
C class obj3
A obj1 = new A( );
B obj2 = new B( );
C obj3 = new C( );
obj1 = obj2; (1)
obj1 = obj3; (2)
obj2 = obj3; (3)
obj3 = obj1; (4)
•
private methodA()
{
…
}
public methodB()
{
methodA();
}
In Base Class
Invoke in derived class
•
•
•
• getClass()
• object1.getClass() == object2.getClass()
Q & A ?
Abstract Class
•
•
•
•
•
•
•
• Late Binding
• Dynamic Binding
•
•
•
•
• final. private. static
•
•
•
•
Employee obj1 = new Employee( );
HourlyEmployee obj2 = new HourlyEmployee( );
obj1 = obj2;
obj1.m1();
Employee obj1 = new Employee( );
HourlyEmployee obj2 = new HourlyEmployee( );
obj1.m1();
Employee & HourlyEmployee m1()
obj1.m1() m1 method
•
•
•
•
•
• private
public abstract class Employee
{
private String name;
private Date hireDate;
public abstract double getPay( );
}
public class HourlyEmployee extends Employee
{
private double payRate;
private double hours;
public double getPay( )
{
return payRate * hours;
}
}
public static void main(String args[])
{
Employee tony = new Employee( ); ERROR!!
HourlyEmployee joe = new HourlyEmployee( );
}
public class SalaryEmployee extends Employee
{
private double salary;
private double hours;
public double getPay( )
{
return salary / 12;
}
}
Q & A ?
Interface
•
•
•
• public
Java & OOP Core Concept
•
• class className extends baseclass implements Interface1, Interface2
•
•
• , methods (Override)
• , methods,
• , methods
•
•
• public. static & final
Q & A ?

More Related Content

PDF
Geek Time Janvier 2017 : Quiz Java
DOC
Main ds manual
PDF
Scheme 核心概念(一)
PDF
Jsr310
PPT
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
PPT
20100712-OTcl Command -- Getting Started
PPT
Heaps & Adaptable priority Queues
PDF
Towards hasktorch 1.0
Geek Time Janvier 2017 : Quiz Java
Main ds manual
Scheme 核心概念(一)
Jsr310
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
20100712-OTcl Command -- Getting Started
Heaps & Adaptable priority Queues
Towards hasktorch 1.0

What's hot (20)

PPTX
Java Script Overview
PDF
Java8 stream
PPT
PPT
Maps&hash tables
PDF
C# - What's Next?
PDF
Lecture 3.1 to 3.2 bt
PDF
Look Mommy, No GC! (TechDays NL 2017)
PPTX
The Great and Mighty C++
PDF
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
PDF
科特林λ學
PDF
Interaksi obyek
PDF
Gopher conbr golang e data science - oficial
DOCX
PPTX
Computer programming 2 Lesson 14
PDF
The Ring programming language version 1.2 book - Part 21 of 84
PPTX
PPTX
Python GC
PDF
Python Memory Management 101(Europython)
PDF
Scala taxonomy
PDF
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Java Script Overview
Java8 stream
Maps&hash tables
C# - What's Next?
Lecture 3.1 to 3.2 bt
Look Mommy, No GC! (TechDays NL 2017)
The Great and Mighty C++
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
科特林λ學
Interaksi obyek
Gopher conbr golang e data science - oficial
Computer programming 2 Lesson 14
The Ring programming language version 1.2 book - Part 21 of 84
Python GC
Python Memory Management 101(Europython)
Scala taxonomy
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Ad

Similar to Java & OOP Core Concept (20)

KEY
Objective-C Survives
PPTX
WEB222-lecture-4.pptx
PDF
Metaprogramming
PPTX
Programs.pptx
PDF
Node.js extensions in C++
PDF
JavaScript in 2016
PPTX
JavaScript in 2016 (Codemotion Rome)
PPTX
Angular2 for Beginners
PPTX
Hierarchical_Inheritance_Cppinheritance, hierarchal inheritance, types of inh...
PDF
Java Day-7
PPTX
Object oriented programming in C++
PDF
Introduction to web programming for java and c# programmers by @drpicox
PPTX
Chap2 class,objects contd
PPTX
Front end fundamentals session 1: javascript core
PPT
Constructor
PPTX
Object oriented programming (first)
PPTX
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
KEY
第7回みゆっき☆Think 本気で学ぶ JavaScript
KEY
みゆっき☆Think#7 「本気で学ぶJavascript」
PDF
ChainerX and How to Take Part
Objective-C Survives
WEB222-lecture-4.pptx
Metaprogramming
Programs.pptx
Node.js extensions in C++
JavaScript in 2016
JavaScript in 2016 (Codemotion Rome)
Angular2 for Beginners
Hierarchical_Inheritance_Cppinheritance, hierarchal inheritance, types of inh...
Java Day-7
Object oriented programming in C++
Introduction to web programming for java and c# programmers by @drpicox
Chap2 class,objects contd
Front end fundamentals session 1: javascript core
Constructor
Object oriented programming (first)
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
第7回みゆっき☆Think 本気で学ぶ JavaScript
みゆっき☆Think#7 「本気で学ぶJavascript」
ChainerX and How to Take Part
Ad

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Electronic commerce courselecture one. Pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
DOCX
The AUB Centre for AI in Media Proposal.docx
Encapsulation theory and applications.pdf
Understanding_Digital_Forensics_Presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
sap open course for s4hana steps from ECC to s4
Electronic commerce courselecture one. Pdf
Review of recent advances in non-invasive hemoglobin estimation
Digital-Transformation-Roadmap-for-Companies.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Spectral efficient network and resource selection model in 5G networks
Programs and apps: productivity, graphics, security and other tools
NewMind AI Weekly Chronicles - August'25 Week I
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Diabetes mellitus diagnosis method based random forest with bat algorithm
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Unlocking AI with Model Context Protocol (MCP)
The AUB Centre for AI in Media Proposal.docx

Java & OOP Core Concept