Syntax of declaring class, variable, constructor and method:
Class declaration syntax:
class A {}
<abstract> class A {}
<public> <final> class A <extends ABC> <implements XYZ, MNO> {}
syntax:
class Hello extends Hai implements A, B, C { }
access_specifier access_modifier class ClassName extends SuperClass implements SuperInterface1, SuperInterface1 {
}
access_specifier:
private (NA):
- class members such as v, co, m are declared as private and they cannot be accessible outside of the class. But there is meaning in declaring class itself as private.
default (A):
- default access specifier classes can be used only within the dir though package is declared.
package pack1;
class A { }
public class B { }
protected (NA):
- members can be accessible up to sub classes outside the package. But class cannot be declared as protected.
public (A):
- can be accessible anywhere in the system. If want to be accessible then package declaration and classpath up-to package parent directory updation is must.
access_modifier:
abstract:
- abstract means unclear or not fully implemented. If class contains abstract method(s) - unimplemented methods - only methods declared, then it must be declared as abstract.
- But it is not mandatory that abstract class must contain abstract methods.
- Classes which do not want to be instantiated are also declared as abstract.
- abstract class methods are implemented in sub classes.
- abstract class cannot be instantiated, but can only be inherited.
final:
- final is quite oppose to abstract. final class can be instantiated but cannot be inherited. That means no sub class hierarchy exist for this class.
Note:
- a class cannot be declared as both abstract and final at a time.
Ex:
public abstract class AbstractClass {}
public final class FinalClass {}
- How to declare class as abstract?
abstract class AbstractClass { }
- When to declare class as abstract?
If class contains abstract method then declare class as abstract. abstract method means unimplemented method. Just only declared method.
abstract class & method example:
// Before abstraction
class EmpPayroll {
double calSal(int eid) {
return now*pds;
}
}
class EPClient {
p s v m() {
EmpPayroll ep=new EmpPayroll();
double sal=ep.calSal(eid);
}
}
// After abstraction
abstract class EmpPayroll {
abstract void calSal(int eid);
}
class AdminEmpPayroll extends EmpPayroll {
void calSal(int eid) {
now*pds;
}
}
class SalesEmpPayroll extends EmpPayroll {
void calSal(int eid) {
now*pds+sales_amt*perc;
}
}
class ProdEmpPayroll extends EmpPayroll {
void calSal(int eid) {
now*pds+othrs*amt
}
}
// Wrong
Client
AdminEmpPayroll aep=new AdminEmpPayroll();
aep.calSal(eid);
SalesEmpPayroll sep=new SalesEmpPayroll();
sep.calSal(eid);
ProdEmpPayroll pep=new ProdEmpPayroll();
pep.calSal(eid);
class EPFactory {
static EmpPayroll getEmpPayroll(String empType) {
if(empType.equalsIgnoreCase("Admin"))
return new AdminEmpPayroll();
else if(empType.equalsIgnoreCase("Sales"))
return new SalesEmpPayroll();
else
return new ProdEmpPayroll();
}
}
class EPClient {
public static void main(String rags[]) {
// sub class object can be assigned to super class reference
EmpPayroll ep=EPFactory.getEmpPayroll(rags[0]);
ep.calSal(1); // achieve "Run Time Polymorphism"
}
}
java EPClient Admin 1
Admin 2
Admin 3
Sales 4
Sales 5
Sales 6
Prod 7
Prod 8
Prod 9
- Abstraction benefits to achieve Runtime Polymorphism (RTP).
- What happens if class is declared as abstract?
abstract class cannot be instantiated. It can be only inherited. Inherited sub class must implement all the super class abstract methods.
- Is it mandatory that abstract class must contain abstract methods?
No need. Without abstract methods also sometimes class will be declared as abstract.
- Then without abstract methods why a class need to be declared as abstract?
Most generic classes who provide common functionality are declared as abstract class. Such class dont want to be instantiated directly. They allow their methods to be called via sub classes.
final class:
- final can be only instantiated, can't be inherited. Class which don't want its methods to be overridden in sub class are declared as final. When there is no inheritance, there is no overriding.
Note: In Java all wrapper classes are final.
java.lang.String
java.lang.Integer
java.lang.Double
java.lang.Long
class NonFinalClass {
void m1() {}
void m2() {}
final void m3() {}
final void m4() {}
}
class NonFinalClassSubClass extends NonFinalClass {
// m1() and m2() can be overriden
void m1() {}
void m2() {}
// m3() and m4() cannot be overridden because of final methods
/* if overrides so, we will get compilation error saying final methods cannot be overridden */
void m3() {}
}
If NonFinalClass want all its methods don't want to be overridden in sub class, then we are having two solutions:
i) declare all 4 methods as final
then NonFinalClass can be still inherited, but its methods cannot be overridden
ii) instead of declaring methods as final, declare class itself as final
then final class cannot be inherited, when there is no inheritance, there is no overriding
0 Comments