2
2
.
.
4
4
.
.
1
1
C
C
l
l
a
a
s
s
s
s
e
e
s
s
I
I
n
n
f
f
o
o
Classes
can contain Properties & Methods
can extend single Parent Class class Soldier extends Person { }
can implement multiple Interfaces class Person implements InterfaceA, InterfaceB { }
Content
Class Syntax class Test { }
Extend Class class Soldier extends Person { }
C
C
l
l
a
a
s
s
s
s
S
S
y
y
n
n
t
t
a
a
x
x
Class is declared by
using Keyword class class
followed by the Class Name Person
followed by the Class Body { ... }
which must have Constructor Person() (Method with the Class Name)
which can have Properties String name; (Variables, Constants, Enumerators,...)
which can have Methods void sayHello() { ... }
Class Syntax
class Person { }
Class Syntax
//===========================================================================================================
// CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String[] args) {
Person john = new Person("John", 20); //Create Class Instance by calling its Constructor
john.sayHello(); //Call Method: John is 20 years old
john.name = "Bill"; //Change Property
String name = john.name; //Read Property: Bill
}
}
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person {
//DECLARE PROPERTIES.
String name;
int age;
//DECLARE CONSTRUCTOR.
Person(String name, int age) {
this.name = name;
this.age = age;
}
//DECLARE METHOD.
void sayHello() {
System.out.println(name + " is " + age + " years old");
}
}
E
E
x
x
t
t
e
e
n
n
d
d
C
C
l
l
a
a
s
s
s
s
Child Class can extend single Parent Class by adding extends ParentClassName after Child Class Name.
Child Class can reference Parent's Properties and Methods as they were their own.
You can use Keyword super to reference Parent's Properties and Methods when Child has Properties and Methods with
the same name and signature like: super.name, super.displayName().
Use @Override Annotation just to indicate to Compiler your intention to override existing Parent's Method.
Syntax
class Soldier extends Person { }
Extend Class
//===========================================================================================================
// CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String[] args) {
Soldier john = new Soldier(); //Create Instance of Class Soldier
john.displayName(); //Reference Child's Method.
john.greetSoldier(); //Reference Child's Method.
john.greetPerson(); //Reference Parent's Method (inherited).
Integer age = john.age; //Reference Parent's Property (inherited).
}
}
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person {
String name = "John";
Integer age = 20;
void displayName() { System.out.println("Person " + name); }
void greetPerson() { System.out.println(name + " is Person"); }
}
//===========================================================================================================
// CLASS: Soldier
//===========================================================================================================
class Soldier extends Person {
String name = "John the killer";
@Override //Intention to override Parent's Method
void displayName () { System.out.println("Soldier " + super.name); }//Reference Parent's Property
void greetSoldier() { super.displayName(); }//Reference Parent's Method
}