19 Aug 2017

CONCEPT OF OOP ANDROID JAVA

Concept.    of OOP applied to Android Java coding.
Object Oriented Programming.
Short form is OOP.
It says program should contain such code that resembles real world objects.
Real world objects are proper nouns while their category is a common noun. So common nouns are CLASSES. Proper nouns are OBJECTS.
Write a program that accepts two nos. , adds the two nos. & prints the result.
Now from point of OOP, we must reformulate it. Let's write a  question which has 3 nos. And asks for inputting two of them , has a method or function which adds the two nos. & displays result.
In OOP Language my program code overview snippet is as under.
myQuestion.ask();
myQuestion.sum();
myQuestion.answer();
To achieve above we need a class. A class of question which got space to accommodate 3 nos. Two are input and third is output. It also got to accommodate the code to sum the two nos. called a method or function.
Filename Questions. java
package com.maths.quiz;
Above means that byte code compiled .class files will be kept in subdirectory com/math/quiz.
class Questions
{
  int num1,num2,result;
  void ask();
  void sum();
 interface void answer();
}
answer is just displaying result to user. any future class objects of question type physics or chemistry  quiz might also want to display answer.
Rather than again writing answer code, we better make it as interface method.
So it's code will be kept in separate file for reuse directly without unnecessary increasing size by adding heavy class.
Questions myQuestion;
myQuestion is a definite unique object of class of Questions.
To compile, use command prompt in Windows.
C:/> javac Questions.Java
This creates Questions.class file.
And Questions$answer.class file.
As we see above we can directly use Questions.class byte code. Also we can use Questions.Answer interface method byte code.
Example:-
class HistoryQuestions extends Questions
Or,
class HistoryQuestions implements Questions.Answer
To make a jar package. Java archieve.
Goto root directory.
Type
  Jar cfm Questions.jar manifest  ./com/maths/quiz
Now delete sub directories.
Just keep jar.
Set classpath=.;Questions.jar
Meaning reusable class files path is virtual subdirectory .jar type. As well as current directory shown by the period.
Semicolon just separates the current directory symbol period and virtual directory jar file.
javac myprog.java
To make an aar package Android archive.

No comments:

Post a Comment