object-oriented principles

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

what 2 ways to achieve abstraction.

1. abstract class (0 to 100%) 2. interface (100%)

public class Dog { String breed; int age; String color; void barking() { } void hungry() { } void sleeping() { } } The code above is a sample of a ___1___ Dog with ___2___ and ___3___.

1. class 2. attributes 3. behavior

what are the three rules while creating a constructor?

1. constructor name must be the same as its class name 2. a constructor must have no explicit return type 3. a java constructor cannot be abstract, static, final, and synchronized

what two types of constructors?

1. default constructor (no parameters) 2. parameterized constructor

3 rules of method overriding

1. must have the same name as in the parent class 2. must have the same parameter as in the parent class 3. must be IS-A relationship (inheritance)

what are the five types of inheritance?

1. single 2. multilevel 3. hierarchical 4. multiple 5. hybrid

two usages of method overriding

1. specific implementation of a method 2. used for runtime polymorphism

class definition

A class is a group of objects which have common properties. blueprint for objects.

what is an abstract method?

A method which is declared as abstract and does not have implementation

abstract void printStatus(); //no method body and abstract What is this?

Abstract method

inheritance

one object acquires all the properties and behaviors of a parent object

object

Any entity that has state and behavior is known as an object. For example a chair, pen, table, keyboard, bike, etc. It can be physical or logical.

abstract class Bike{ // Abstract class abstract void run(); // Abstract method } class Honda4 extends Bike{ void run(){System.out.println("running safely");} public static void main(String args[]){ Bike obj = new Honda4(); obj.run(); } } what is this?

Example of Abstract Class with an Abstract Method

the process of extracting shared characteristics from two or more classes, and combining them into a generalized superclass. what is this?

Generalization

why can't static methods be overriden?

It is because the static method is bound with class whereas instance method is bound with an object

is this a method overloading or overriding? Return type can be same or different. But you must have to change the parameter.

overloading

class Student4{ int id; String name; Student4(int i,String n){ id = i; name = n; } What is the constructor?

Student4(int i,String n){ id = i; name = n; }

subclass definition and what is it also called?

Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.

superclass definition and what is it also called?

Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.

Classes don't consume any space. T or F

T

class Subclass-name extends Superclass-name { //methods and fields added here } 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. T or F

T

Abstraction lets you focus on what the object does instead of how it does it. T or F

True

An abstract class must be declared with an abstract keyword. T or F

True

If members have default constructors or constructor without parameter then these constructors are called automatically T or F

True

If there is no constructor in the class, the compiler adds a default constructor T or F

True

If you implement any constructor then you no longer receive a default constructor. T or F

True

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the object is created, and memory is allocated for the object. T or F

True

It is called constructor because it constructs the values at the time of object creation T or F

True

OOPs makes development and maintenance easier whereas in a procedure-oriented programming language it is not easy to manage if code grows as project size increases. T or F

True

OOPs provides data hiding whereas in a procedure-oriented programming language a global data can be accessed from anywhere. T or F

True

an abstract class can have abstract and non-abstract methods. T or F

True

an abstract class cannot be instantiated. T or F

True

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); } } The relationship between the two classes is Programmer IS-A Employee. It means that Programmer is a type of Employee. T or F

True

parameterized constructors can be called using initializer list T or F

True

a no-arg constructor contains a default call to super( ); T or F

True, this is default behavior

when would a constructor be called?

When an object is created

classes

a blueprint from which you can create an individual object.

It is a special type of method which is used to initialize the object. what is it?

a constructor

encapsulation

a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines.

shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don't know the internal processing about the message delivery. what is this?

abstraction

A class which is declared as abstract is known as what?

an abstract class

package com.revature; public class Student{ //private data member private String name; //getter method for name public String getName(){ return name; } //setter method for name public void setName(String name){ this.name=name } } //A Java class to test the encapsulated class. package com.revature; class Test{ public static void main(String[] args){ //creating instance of the encapsulated class Student s=new Student(); //setting value in the name member s.setName("vijay"); //getting value of the name member System.out.println(s.getName()); } } What is this?

an example of encapsulation of a class called Student that has only one field with its setter and getter methods.

what can be shared characteristics?

attributes, associations, or methods

what is method overriding in java?

child class has the same method declared in the parent class

what four terms are used in inheritance?

class sub class/child class super class/parent class reusablility

templates that are used to create objects, and to define object data types and methods. what are these called?

classes

Is this a constructor or a method? The Java compiler provides a default constructor if you don't have any constructor in a class.

constructor

Is this a constructor or a method? is invoked implicitly.

constructor

Is this a constructor or a method? must not have a return type.

constructor

Is this a constructor or a method? the name must be same as the class name.

constructor

Is this a constructor or a method? used to initialize the state of an object.

constructor

polymorphism

he ability of an object to take on many forms

class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ // Hierarchical Inheritance void bark(){System.out.println("barking...");} } class Cat extends Animal{ // Hierarchical Inheritance void meow(){System.out.println("meowing...");} } class TestInheritance3{ public static void main(String args[]){ Cat c=new Cat(); c.meow(); c.eat(); //c.bark(); //Error }} what level of inheritance is this?

hierarchical

what is also known as a parent-child relationship or IS-A relationship in oop?

inheritance

what are the four pillars of oop?

inheritance polymorphism abstraction encapsulation

//A Java class which has only getter methods. public class Student{ //private data member private String college="AKG"; //getter method for college public String getCollege(){ return college; } } why is this a read-only class?

it has only a getter to access the college name

Is this a constructor or a method? invoked explicitly.

method

Is this a constructor or a method? must have a return type.

method

Is this a constructor or a method? not provided by the compiler in any case.

method

Is this a constructor or a method? the name may or may not be same as class name

method

Is this a constructor or a method? used to expose the behavior of an object.

method

is this a method overloading or overriding? example of compile time polymorphism.

method overloading

is this a method overloading or overriding? parameters must be different.

method overloading

is this a method overloading or overriding? performed within class.

method overloading

is this a method overloading or overriding? used to increase the readability of the program.

method overloading

If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as

method overriding

is this a method overloading or overriding? Return type must be same or covariant

method overriding

is this a method overloading or overriding? occurs in two classes that have IS-A (inheritance) relationship.

method overriding

is this a method overloading or overriding? parameters must be same.

method overriding

is this a method overloading or overriding? used to provide the specific implementation of the method that is already provided by its super class.

method overriding

advantages of inheritance are

method overriding code reusablility

is this a method overloading or overriding? example of run time polymorphism.

method overridng

class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ //Level 1 - Inheritance void bark(){System.out.println("barking...");} } class BabyDog extends Dog{ //Level 2 - Inheritance void weep(){System.out.println("weeping...");} } class TestInheritance2{ public static void main(String args[]){ BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); }} what level of inheritance is this?

multilevel

public Bicycle() { gear = 1; cadence = 10; speed = 0; } is this a no-arguement constructor or an parameterized constructor?

no arguement constructor

can a static method be overriden?

no, a static method cannot be overriden

what is no-args constructor?

no-argument constructor

what does OOPS stand for?

object oriented programming and systems

abstraction

process of hiding the implementation details and showing only functionality to the user.

why is reusability used?

reusing 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.

class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ //Single Inheritance void bark(){System.out.println("barking...");} } class TestInheritance{ public static void main(String args[]){ Dog d=new Dog(); d.bark(); d.eat(); }} what level of inheritance is this?

single

creating new subclasses from an existing class. If it turns out that certain attributes, associations, or methods only apply to some of the objects of the class, a subclass can be created. what is this called?

specialization

An abstract class can have constructors and static methods also. T or F

true

In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object. T or F

true

It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't have any. T or F

true

Objects can communicate without knowing the details of each other's data or code. T or F

true

Static belongs to the class area, and an instance belongs to the heap area. T or F

true

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. T or F

true

abstract classes can have final methods which will force the subclass not to change the body of the method. T or F

true

what is the most common use of polymorphism in oop?

when a parent class reference is used to refer to a child class object.

can an object can be defined as an instance of a class

yes

does a compiler make sure that constructors for all of its subobjects (it's member and inherited objects) are called?

yes

does object contain an address and takes up some space in memory

yes

is a default constructor a no argument constructor?

yes

is this an advantage of encapulation? By providing only a setter or getter method, you can make the class read-only or write-only

yes

is this an advantage of encapulation? It is a way to achieve data hiding

yes

is this an advantage of encapulation? It provides you the control over the data.

yes

is this an advantage of encapulation? easy to test. So, it is better for unit testing.

yes

class ABC{ //Overridden method public void disp() { System.out.println("disp() method of parent class"); } } class Demo extends ABC{ //Overriding method public void disp(){ System.out.println("disp() method of Child class"); } public void newMethod(){ System.out.println("new method of child class"); } public static void main( String args[]) { ABC obj = new ABC(); obj.disp(); ABC obj2 = new Demo(); // Covariance with reference types obj2.disp(); } } Does this follow the three rules of method overriding?

yes, it's named and parameterized the same in the subclass as in the parent class, and the keyword extends make it a IS-A relationship.


Ensembles d'études connexes

Principle of web design chapter 1

View Set

Com Arts Midterm II, Ch. 6, 8, 16, 17

View Set

Chapter 28 - NUR 240 Review Questions

View Set

implementing secure protocols and application Security Solutions

View Set

ESS 106 - Living with Volcanoes: Mt St Helens 1

View Set