Concepts of OOPS in Java
Java, oops, abstraction, polymorphism, inheritance
Size: 2.28 MB
Language: en
Added: Oct 12, 2024
Slides: 16 pages
Slide Content
Object Oriented Programming concepts
Concept Definition : Class is a blue print (Abstraction) Class defines State (Characteristics/Attributes) Behaviour Java The basic unit of object-orientation in Java is Class. Class – basis of all computation State Behaviour
Concept Have specific identity (Not a blueprint) Have specific attributes (state) and behavior. Java Instance of a class Objects State Behaviour
public class Car { /* Attributes that define the state of the class are declared here*/ /* These variables are called instance variables (or) member variables */ public String make ; public String model ; public String colour ; public float mileage ; public float distance ; /*Methods that define the behavior of the class are declared here*/ Class in Java State public class Car Class: Car Member variables (or) Instance variables Member methods Access Modifier Keyword Class name (identifier)
//Methods that define the behavior of the class are declared here. Only these methods can access the instance //variables or instance variables. public void setMileage (){ //Code to set the current mileage of the car } public void chgDistance (){ //Code to update the distance travelled so far } public void dispDetails (){ //Code to view the details of the car } } // End of Class Class in Java (Code continues…) Behaviour
p ublic static void main(){ Car c2 = new Car (“BMW”,”x3”,”Alphine-White”,15,0); Car c3 = new Car(“Ferrari”,” GTC4Lusso ”,” Giallo Modena ”,4,0); } Objects in Java Car c1 = new Car( “Hyuindai”,” Creta ”,”Typhoon-silver”, 19,0 ); Class name Object name ‘new’ - keyword for creating objects Constructor - method to initialize an object Parameters for constructor objects Constructor It is a special type of method in the name of the class that initializes the object. We will discuss this later(1)
Class and its objects C1 Brand: Hyuindai Model: Creta Colour : Typhoon-Silver Mileage : 19 Distance covered : C2 Brand: BMW Model: X3 Colour : Alphine -white Mileage : 15 Distance covered : C3 Brand: Ferrari Model: GTC4Lusso Colour : Giallo Modena Mileage : 4 Distance covered : Class: Car
Terms used in Object O riented Programming (OOP) Data encapsulation Ability to combine state & behavior (member variables and member methods) is called data encapsulation. Data abstraction Act of representing essential features without including the background details or explanation. (Generalization or Blue print) Class: Car Member variables (or) Instance variables Member methods
Terms used in Object O riented Programming (OOP) Class Class is a blueprint of a set of objects that have common structure and behavior. Object An entity with specific identity, specific characteristics and specific behavior. An instance of a class. Class: Car Member variables (or) Instance variables Member methods
Terms used in Object O riented Programming (OOP) Polymorphism Ability to take ‘many forms’ of the same method. Inheritance Derive part or all of the information from one entity to another entity We will discuss this later We will discuss this later
Special methods in Java that are called when an object is created. Used to initialize the objects. Name of the method is same as that of the class No explicit return type specified. There are two types Default constructor (Called when no constructor is defined in the class) Parameterized constructor Constructor methods in Java
Constructor methods in Java public class Car { //declare instance variables here //define member methods here Car (String mke , String mdl, String clr , float mile, float dist ){ make= mke ; model= mdl; colour = clr ; mileage= mile; distance= dist ; } Car (String mke , String mdl, String clr ){ make= mke ; model= mdl; colour = clr ; mileage= 0; distance= 0; } } public static void main() { Car c1 = new Car (); //default constructor Car c2 = new Car(“BMW”,”x3”,”Alphine-White”,15,0); //Parameterized constructor-1 Car c3 = new Car(“Ferrari”,” GTC4Lusso ”,” Giallo Modena ”); //Parameterized constructor-2 } Polymorphism – Three constructor functions with same name.
Levels of access Public Accessible to any other class anywhere Protected Accessible to package classes and any subclasses that are in packages Default Accessible to classes in the same package Private Accessible only within the class Package-1 Class-1 Class-2 Class-3 Package-2 Class-A extends Class-1 Class-B Class-C Class level Package level Other package – Subclass level Other package – Non subclass level public void setMileage () public void chgDistance () Access modifier private default* protected public *default is not a keyword
OOP concepts Data encapsulation (or) Data hiding Data abstraction Polymorphism Inheritance Things to remember
Class is a blueprint of objects. Object is an instance of class. Member variables (or) instance variables in a class Member methods in a class Constructor methods Creating objects Things to remember