OOPs - Class, Object, Methods in java
Object Oriented Programming System/ Structure
OOPs is a programing paradigm/ methodology. (Paradigm - method to solve some problem ).
Types of paradigm
- Object oriented paradigm
- procedural paradigm
- functional paradigm
- logical paradigm
- structural paradigm.
Piller of OOPs
- class
- Object and method
- inheritance
- Abstraction
- Polymorphism
- Encapsulation
Class and Object -
- Class is a collection of Object.
- Class is a template / class is a blueprint of object, it not a real entity ( animal is not real but dog is real )
Syntax -
access-modifier class ClassName{ - methods - constructors - fields - blocks - nested class }Methods - set of code perform a particular task. ( uses - code resusability, code optimization )
Syntax -
access-modifier return-type methodName(list of Parameter){}Object -
- is an instance of class
- it is real world entity ( dog, cat ).
- it occupies memory.
object consist of - ( ex Dog)
- Identity - name
- state / Attribute ( color, bread, age )
- Behavior - ( eat, run )
HOw to create Object
- using new keyword.
- newInstance() method ( it is predefined method)
- clone ( ) method
- deserialization method
- Factory method
Syntax -
Animal tommy; //declaration method tommy = new; // instantiation method tommy = new Animal() // initialisation Animal tommy = new Animal()How to call method though object -
tommy.run()Example
class Animal { public void eat(){ System.out.println("i am eating"); } public static void main(String [] arg){ Animal tommy = new Animal(); // object tommy.eat(); } }How to initialize object
- by reference Veriable -
class Animal // No access modifier so it take default{ String color; int age; public static void main(String [] age){ Animal tommy = new Animal(); tommy.color = "black"; tommy.age = 10; System.out.println(tommy.color +" " + tommy.age ); } }- by using Method -
class Animal // No access modifier so it take default{ String color; int age; public static void main(String [] age){ void dogInfo(String color, int age ) { color = color; age = age; } void display(){ System.out.print(Color + " " + age); } Animal tommy = new Animal(); tommy.dogInfo("black", 10); tommy.display(); } }
Post a Comment