Unformatted text preview:

Object Oriented Programming in Java Object Oriented Programming OOP is a programming paradigm that allows you to organize code into reusable objects that contain data and methods that operate on that data In Java OOP is achieved through the use of classes which are user defined data types that encapsulate data and functions into a single unit The concept explains the four fundamental principles of OOP encapsulation inheritance polymorphism and abstraction Encapsulation is the practice of hiding the internal implementation of an object from the outside world exposing only the necessary methods and properties to interact with it This allows for greater flexibility and maintainability of code Inheritance is the ability of a class to inherit the properties and methods of another class This promotes code reuse and organization Polymorphism is the ability of an object to take on multiple forms or behaviors This is achieved in Java through the use of interfaces and abstract classes Abstraction is the process of simplifying complex systems by modeling them at a high level ignoring unnecessary details Then how to create classes and objects in Java with examples and step by step calculations Here s a summarized example of creating a Circle class with a constructor and methods public class Circle Define a private instance variable private double radius Define a constructor that initializes the radius public Circle double radius this radius radius Define a method that calculates the area of the circle public double getArea return Math PI radius radius Define a method that calculates the circumference of the circle public double gercumference return 2 Math PI radius Create a new Circle object with a radius of 5 Circle cir new Circle 5 Calculate the area and display it dou area circle getArea System out println Area area alculate the circumference and display it double circumference circle getCircumference System out println Circumference circumference The topic also recovered as Interfaces A set of method signatures that a class must implement Interfaces allow for polymorphism by enabling objects of different classes to be treated as if they were of the same type Abstract classes A class that cannot be instantiated but can be subclassed Abstract classes can contain both abstract methods methods with no implementation and concrete methods methods with an implementation Final keywords The final keyword can be used to prevent a class from being subclassed a method from being overridden or a variable from being changed In this chapter we will explore the fundamental concepts of OOP and see how to put them into practice through engaging examples and code samples First let s start with a brief overview of OOP OOP is a programming paradigm based on the concept of objects which contain data and methods that operate on that data This approach has several advantages over other programming paradigms including Encapsulation OOP allows you to encapsulate data and methods within objects hiding the implementation details and preventing unauthorized access Inheritance OOP enables you to create new classes that inherit properties and behaviors from existing classes promoting code reuse and reducing duplication Polymorphism OOP allows you to write generic code that can operate on objects of different types making your programs more flexible and maintainable Now let s see how these concepts are implemented in Java In the topics we learned about classes and objects A class is a blueprint for creating objects while an object is an instance of a class For example we can define a Car class as follows public Car String make String model int year public class Car private String make private String model private int year this make make this model model this year year public String getMake return make public String getModel return model public int getYear return year This class defines three private fields make model and year and three public methods getMake getModel and getYear that return the values of these fields We can create an object of this class as follows Car myCar new Car Toyota Corolla 2020 This creates a new Car object with the make Toyota the model Corolla and the year 2020 Next let s talk about inheritance In Java you can create a new class that inherits properties and behaviors from an existing class using the extends keyword For example we can create a SportsCar class that inherits from the Car class as follows public SportsCar String make String model int year int topSpeed public class SportsCar extends Car private int topSpeed super make model year this topSpeed topSpeed public int getTopSpeed return topSpeed This class defines a new private field topSpeed and a new public method getTopSpeed It also uses the super keyword to call the constructor of the Car class and initialize its fields We can create an object of this class as follows SportsCar mySportsCar new SportsCar Ferrari 488 2019 330 This creates a new SportsCar object with the make Ferrari the model 488 the year 2019 and the top speed of 330 km h Finally let s discuss polymorphism In Java you can write generic code that operates on objects of different types using interfaces and abstract classes An interface is a collection of method signatures that define a contract between a class and its users For example we can define a Drivable interface as follows This interface defines a single method drive that specifies the behavior of driving We can then create a Car class that implements this interface as follows public interface Drivable void drive Explain public class Car implements Drivable existing code public void drive System out println Driving this getMake Encapsulation is a fundamental concept in object oriented programming that helps to keep the data and methods within a class hidden from the outside world and protected from unauthorized access Imagine you have a class called Calculator that can perform various arithmetic operations Encapsulation would allow you to hide the implementation details of the calculator and expose only the necessary methods to the outside world Here s an example public class Calculator encapsulated field private int result public method to add two numbers public int add int num1 int num2 result num1 num2 return result public method to get the result public int getResult return result Calculator calc new Calculator int sum calc add 5 10 System out println Sum sum int result calc getResult System out println


View Full Document

Anna GE 6151 - Object-Oriented Programming in Java

Documents in this Course
Load more
Download Object-Oriented Programming in Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object-Oriented Programming in Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Programming in Java and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?