Charmo University OOP I Lecture 4 PDF

Document Details

FeasibleJacksonville5404

Uploaded by FeasibleJacksonville5404

Charmo University

Soran Rahman

Tags

object-oriented programming java inheritance computer science programming

Summary

This document is a lecture on Object-Oriented Programming (OOP) focusing on inheritance in Java. The lecturer, Soran Rahman, covers concepts like classes, objects, and different types of inheritance in OOP.

Full Transcript

Charmo University COLLEGE OF SCEINCE Department of Computer Third Stage - OOP I - Lecture 4 Soran Rahman Sunday, November 24, 2024 Object-Oriented Programming I...

Charmo University COLLEGE OF SCEINCE Department of Computer Third Stage - OOP I - Lecture 4 Soran Rahman Sunday, November 24, 2024 Object-Oriented Programming I (OOP I) Inheritance in Java Soran Rahman OOP-Third Stage Computer Dept. What is object-oriented programming? OOP is a computer programming model that organizes software design around data or paradigm to design a program using classes and object. OOP concepts: 1. Class. 2. Object. 3. Inheritance. 4. Polymorphism. 5. Abstraction(Interface). 6. Interface. 7. information hiding (Encapsulation) Soran Rahman OOP 3rd stage 3 Inheritance in Java Inheritance Types of Inheritance Why multiple inheritance is not possible in Java in case of class? Soran Rahman OOP 3rd stage 4 Inheritance in Java Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system). The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship. Soran Rahman OOP 3rd stage 5 Inheritance in Java Super Class Child Class Soran Rahman OOP 3rd stage 6 Why use inheritance in java? For Method Overriding (so runtime polymorphism can be achieved). For Code Reusability. The syntax of Java Inheritance class Subclass-name extends Superclass-name { //methods and fields } Soran Rahman OOP 3rd stage 7 Terms used in Inheritance Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class. Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class. The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality. Soran Rahman OOP 3rd stage 8 Java Inheritance Example As displayed in the above figure, Programmer is the subclass and Employee is the superclass. Relationship between two classes is Programmer IS-A Employee. It means that Programmer is a type of Employee. Java Inheritance Example class Employee{ float salary=40000; } class Programmer extends Employee{ int bonus=10000; public static void main(String args[]){ Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); } } In the above example, Programmer object can access the field of own class as well as of Employee class i.e. code reusability. Soran Rahman OOP 3rd stage 10 Types of inheritance in java On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical. Soran Rahman OOP 3rd stage 11 Types of inheritance in java When a class extends multiple classes i.e. known as multiple inheritance. For Example: In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later. Soran Rahman OOP 3rd stage 12 Single Inheritance class Animal{ void eat(){System.out.println("eating...");} When a class inherits another class, it is } known as a single inheritance. In the example class Dog extends Animal{ given below, Dog class inherits the Animal void bark(){System.out.println("barking...");} class, so there is the single inheritance. } class TestInheritance{ File: TestInheritance.java public static void main(String args[]){ Dog d=new Dog(); d.bark(); d.eat(); } } Soran Rahman OOP 3rd stage 13 Multilevel Inheritance class Animal{ void eat(){System.out.println("eating...");} When there is a chain of inheritance, it is } known as multilevel inheritance. As you class Dog extends Animal{ can see in the example given below, void bark(){System.out.println("barking...");} BabyDog class inherits the Dog class } which again inherits the Animal class, so class BabyDog extends Dog{ there is a multilevel inheritance. void weep(){System.out.println("weeping...");} } Example: class TestInheritance2{ public static void main(String args[]){ BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); }} Soran Rahman OOP 3rd stage 14 class Animal{ Hierarchical Inheritance void eat(){System.out.println("eating...");} } class Dog extends Animal{ When two or more classes inherits a void bark(){System.out.println("barking...");} single class, it is known as hierarchical } inheritance. class Cat extends Animal{ void meow(){System.out.println("meowing...");} In the example given below, Dog and Cat } classes inherits the Animal class, so there class TestInheritance3{ is hierarchical inheritance. public static void main(String args[]){ Cat c=new Cat(); c.meow(); c.eat(); //c.bark();//C.T.Error }} Soran Rahman OOP 3rd stage 15 Why multiple inheritance is not supported in java? To reduce the complexity and simplify the language, multiple inheritance is not supported in java. Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class. Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now. Soran Rahman OOP 3rd stage 16 Why multiple inheritance is not supported in java? class A{ void msg(){System.out.println("Hello");} } class B{ void msg(){System.out.println("Welcome");} } class C extends A,B{ //suppose if it were public static void main(String args[]){ C obj=new C(); obj.msg(); //Now which msg() method would be invoked? } } Soran Rahman OOP 3rd stage 17 Thank You Do everything in your power. Soran Rahman OOP 3rd stage 18

Use Quizgecko on...
Browser
Browser