Basics
Class & Object
- Class is a blueprint which defines variables and functions (properties and behaviours)
- Object is a real thing created by Class definition. It allocate space in memory (RAM)
Abstraction
Simple description of complex systems:
- We just turn the key of the door. We don’t think about lock mechanism
- We just use steering, gas and brake for drive a car. We don’t calculate gasoline ratio inside the engine’s cyclinder.
Encapsulation
Information hiding:
- We don’t know all integriteds of a capsule pill
- We don’t see all electrical circuits of a TV, mobile phone…
Inheritance
Take an existing class as a base and add new feature to it:
- Base class: Person {string name; ind ID}
- Derived class: Employe extends Person {int wage}
Employe has got existing properties and behaviours of Person in other words Employe is a Person.
Polymorhism
Different results of the same action. All animals can talk() but
- dog.talk() -> Bark Bark
- cat.talk() -> meowW
- lion.talk() ->Roar
- …
Class
Constructor
- Constructor is a method which construct the object.
- Every class has at least one contructor method even user not defined it. The default constructor is a constructor which defined by compiler.
- We can define multiple constructor for a class
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.Name = "Muhammet Esat";
person.Surname = "BUYUKBULUT";
person.ID = 123123;
System.out.println(person.Print());
Person p2 = new Person("Ahmet", "BUYUKBULUT");
System.out.println(p2.Print());
}
}
public class Person {
public String Name;
public String Surname;
public int ID;
// Constructors
Person(){
Name = "None";
Surname = "None";
}
Person(String Name, String Surname){
this.Name = Name;
this.Surname = Surname;
// this keyword gives a reference to this object
}
String Print(){
return "Name: " + Name + "\t\tSurname: " + Surname + "\t\tID: " + ID;
}
}
Inheritance
- We use extends keyword for inherit another class.
- We can use super() for call constructor of parent class.
- Parent constructor will be called even we don’t use super.
- We can use parent class properties and behaviours in child class
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Worker w1 = new Worker();
Worker w2 = new Worker("Mehmet", "KARA", 12000, "IT");
System.out.println(w1.Print());
System.out.println(w2.Print());
}
}
public class Person {
String Name;
String Surname;
int ID;
// Constructors
Person(){
Name = "None";
Surname = "None";
System.out.println("Person c1 called");
}
Person(String Name, String Surname){
this.Name = Name;
this.Surname = Surname;
System.out.println("Person c2 called");
// this keyword gives a reference to this object
}
String Print(){
return "Name: " + Name + "\t\tSurname: " + Surname + "\t\tID: " + ID;
}
}
public class Worker extends Person {
int Wage;
String Department;
Worker(){
System.out.println("Worker c1 called");
}
Worker(String Name, String Surname, int Wage, String Department ){
super(Name,Surname);
this.Wage = Wage;
this.Department = Department;
System.out.println("Worker c2 called");
}
}
Person c1 called
Worker c1 called
Person c2 called
Worker c2 called
Name: None Surname: None ID: 0
Name: Mehmet Surname: KARA ID: 0
Static
We use static keyword for common values. We don’t need an object for static values. They are initialized from beginning of the program and we can access them via class reference
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Person[] parr = new Person[5];
for (int i = 0; i < parr.length; i++) parr[i] = new Person();
for (Person p : parr) p.Print();
System.out.println("\n\n\n");
int globalId = Person.GLOBAL_ID;
System.out.println("GLOBAL_ID: " + globalId);
Person.SFunction();
}
}
public class Person {
private String Name;
private String Surname;
private int ID;
static public int GLOBAL_ID = 0;
Person(){
Name = "None";
Surname = "None";
ID = GLOBAL_ID++;
}
Person(String Name, String Surname){
this.Name = Name;
this.Surname = Surname;
ID = GLOBAL_ID++;
}
void Print(){
System.out.println("Name: " + Name + "\t\tSurname: " + Surname + "\t\tID: " + ID);
}
public static void SFunction(){
System.out.println("This is a static function.");
}
}
Static Class
Java has static nested classes but it sounds like you’re looking for a top-level static class. Java has no way of making a top-level class static but you can simulate a static class like this:
- Declare your class
final
– Prevents extension of the class since extending a static class makes no sense - Make the constructor
private
– Prevents instantiation by client code as it makes no sense to instantiate a static class - Make all the members and functions of the class
static
– Since the class cannot be instantiated no instance methods can be called or instance fields accessed - Note that the compiler will not prevent you from declaring an instance (non-static) member. The issue will only show up if you attempt to call the instance member
https://stackoverflow.com/questions/7486012/static-classes-in-java
Leave a Reply