Encapsul_Inherit.pdf
Document Details
Uploaded by AppreciableOnyx
Tags
Full Transcript
What are the main principles of OOP? Ja va OOP Concepts with Exa mples (howtodoinjava .com) Encapsulation Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Another way to think about encapsulation is,...
What are the main principles of OOP? Ja va OOP Concepts with Exa mples (howtodoinjava .com) Encapsulation Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Another way to think about encapsulation is, that it is a protective shield that prevents the data from being accessed by the code outside this shield. We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it. Encapsulation in Java - GeeksforGeeks What are the main principles of OOP? Encapsulation. This principle states that all important information is contained inside an object and only select information is exposed. The implementation and state of each object are privately held inside a defined class. Other objects do not have access to this class or the authority to make changes. They are only able to call a list of public functions or methods. This characteristic of data hiding provides greater program security and avoids unintended data corruption. Encapsulation – more definitions Encapsulation in Java: •Restricts direct access to data members (fields) of a class •Fields are set to private •Each field has a getter and setter method •Getter methods return the field •Setter methods let us change the value of the field Encapsulation is a fundamental concept in object-oriented programming (OOP) that refers to the bundling of data and methods that operate on that data within a single unit, which is called a class in Java. Encapsulation is a way of hiding the implementation details of a class from outside access and only exposing a public interface that can be used to interact with the class. Enca psulation i n Java - GeeksforGeeks Us i ng OOP concepts to write high-performance Ja va code (2023) · Raygun Blog What are the main principles of OOP? Inheritance It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class. In addition, you can add new fields and methods to your current class as well. Inheritance in Ja va - GeeksforGeeks Inheritance – example 1 Source: Chat GPT Inheritance – example 1 cont. Source: Chat GPT Inheritance – HomeAnimals Source: Chat GPT Inheritance – HomeAnimals Will this code work? Source: Chat GPT Inheritance – HomeAnimals Source: Chat GPT Inheritance – HomeAnimals Inheritance in Java Explained (freecodecamp.org) Inheritance in Java (Part 1 Inheriting Methods) YouTube, 7min Inheritance in Java Example | Java Tutorial YouTube, 3min Source: Chat GPT Inheritance – Types Java does not support multiple inheritances Inheritance in Ja va - GeeksforGeeks How to Sol ve the Problem of Multiple Inheritance i n Ja va (freecodecamp.org) What are the main principles of OOP? Why Do We Need Java Inheritance? Inheritance •Code Reusability: The code written in the Superclass is common to all subclasses. Child classes can directly use the parent class code. •Method Overriding: Method Overriding is achievable only through Inheritance. It is one of the ways by which Java achieves Run Time Polymorphism. •Abstraction: The concept of abstract where we do not have to provide all details is achieved through inheritance. Abstraction only shows the functionality to the user. Important Terminologies Used in Java Inheritance •Class: Class is a set of objects which shares common characteristics/ behavior and common properties/ attributes. Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created. •Super Class/Parent Class: The class whose features are inherited is known as a superclass(or a base class or a parent class). •Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods. •Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class. Inheritance in Ja va - GeeksforGeeks What are the main principles of OOP? Method Overriding: In Java, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, the same parameters or signature, and the same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Inheritance Abstraction: is the property by virtue of which only the essential details are displayed to the user. The trivial or the non-essential units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components. Inheritance in Ja va - GeeksforGeeks